BAEL-5777 - Mocking a singleton with Mockito (#12977)
* BAEL-5777 - Mocking a singleton with Mockito * BAEL-5777 - Mocking a singleton with Mockito - changing test class name * BAEL-5777 - Mocking a singleton with Mockito - moving to new module
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ProductServiceUnitTest {
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
mock-maker-inline
|
||||
Reference in New Issue
Block a user