BAEL-5777 - Mocking a singleton with Mockito
This commit is contained in:
@@ -53,6 +53,12 @@
|
||||
<version>${system-stubs.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<version>${mockito-inline.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -103,6 +109,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<mockito-inline.version>4.8.0</mockito-inline.version>
|
||||
<jacoco.version>0.8.6</jacoco.version>
|
||||
<system-rules.version>1.19.0</system-rules.version>
|
||||
<system-lambda.version>1.0.0</system-lambda.version>
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class CacheManager {
|
||||
private final HashMap<String, Object> map;
|
||||
|
||||
private static CacheManager instance;
|
||||
|
||||
private CacheManager() {
|
||||
map = new HashMap<>();
|
||||
}
|
||||
|
||||
public static CacheManager getInstance() {
|
||||
if(instance == null) {
|
||||
instance = new CacheManager();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public <T> T getValue(String key, Class<T> clazz) {
|
||||
return clazz.cast(map.get(key));
|
||||
}
|
||||
|
||||
public Object setValue(String key, Object value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
public class Product {
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
public Product(String name, String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
public class ProductDAO {
|
||||
public Product getProduct(String productName) {
|
||||
|
||||
return new Product(productName, "description");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
public class ProductService {
|
||||
|
||||
private final ProductDAO productDAO;
|
||||
private final CacheManager cacheManager;
|
||||
|
||||
public ProductService(ProductDAO productDAO) {
|
||||
this.productDAO = productDAO;
|
||||
this.cacheManager = CacheManager.getInstance();
|
||||
}
|
||||
|
||||
public ProductService(ProductDAO productDAO, CacheManager cacheManager) {
|
||||
this.productDAO = productDAO;
|
||||
this.cacheManager = cacheManager;
|
||||
}
|
||||
|
||||
public Product getProduct(String productName) {
|
||||
Product product = cacheManager.getValue(productName, Product.class);
|
||||
if (product == null) {
|
||||
product = productDAO.getProduct(productName);
|
||||
}
|
||||
|
||||
return product;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ProductServiceTest {
|
||||
|
||||
@Test
|
||||
void givenValueExistsInCache_whenGetProduct_thenDAOIsNotCalled() {
|
||||
ProductDAO productDAO = mock(ProductDAO.class);
|
||||
CacheManager cacheManager = mock(CacheManager.class);
|
||||
Product product = new Product("product1", "description");
|
||||
ProductService productService = new ProductService(productDAO, cacheManager);
|
||||
|
||||
when(cacheManager.getValue(any(), any())).thenReturn(product);
|
||||
|
||||
productService.getProduct("product1");
|
||||
|
||||
Mockito.verify(productDAO, times(0)).getProduct(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenValueExistsInCache_whenGetProduct_thenDAOIsNotCalled_mockingStatic() {
|
||||
ProductDAO productDAO = mock(ProductDAO.class);
|
||||
CacheManager cacheManager = mock(CacheManager.class);
|
||||
Product product = new Product("product1", "description");
|
||||
|
||||
try (MockedStatic<CacheManager> cacheManagerMock = mockStatic(CacheManager.class)) {
|
||||
cacheManagerMock.when(CacheManager::getInstance).thenReturn(cacheManager);
|
||||
when(cacheManager.getValue(any(), any())).thenReturn(product);
|
||||
ProductService productService = new ProductService(productDAO);
|
||||
productService.getProduct("product1");
|
||||
Mockito.verify(productDAO, times(0)).getProduct(any());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
mock-maker-inline
|
||||
Reference in New Issue
Block a user