BAEL-7002 - Drawbacks of Singleton Pattern (#14979)
* BAEL-7002 - Drawbacks of Singleton Pattern * Add mockito-inline dependency
This commit is contained in:
parent
140974c9bf
commit
73e58da770
|
@ -12,4 +12,13 @@
|
|||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class Logger {
|
||||
private static volatile Logger instance;
|
||||
|
||||
private PrintWriter fileWriter;
|
||||
|
||||
public static Logger getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (Logger.class) {
|
||||
if (instance == null) {
|
||||
instance = new Logger();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Logger() {
|
||||
try {
|
||||
fileWriter = new PrintWriter(new FileWriter("app.log"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void log(String message) {
|
||||
String log = String.format("[%s]- %s", LocalDateTime.now(), message);
|
||||
fileWriter.println(log);
|
||||
fileWriter.flush();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
public class SingletonDemo {
|
||||
|
||||
public int sum(int a, int b) {
|
||||
int result = a + b;
|
||||
Logger logger = Logger.getInstance();
|
||||
logger.log("The sum is " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
|
||||
class SingletonUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoValues_whenSum_thenReturnCorrectResult() {
|
||||
SingletonDemo singletonDemo = new SingletonDemo();
|
||||
int result = singletonDemo.sum(12, 4);
|
||||
Assertions.assertEquals(16, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMockedLogger_whenSum_thenReturnCorrectResult() {
|
||||
Logger logger = mock(Logger.class);
|
||||
|
||||
try (MockedStatic<Logger> loggerMockedStatic = mockStatic(Logger.class)) {
|
||||
loggerMockedStatic.when(Logger::getInstance).thenReturn(logger);
|
||||
doNothing().when(logger).log(any());
|
||||
|
||||
SingletonDemo singletonDemo = new SingletonDemo();
|
||||
int result = singletonDemo.sum(12, 4);
|
||||
Assertions.assertEquals(16, result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue