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:
Abhinav Pandey
2022-11-07 10:17:03 +05:30
committed by GitHub
parent c57284d9c9
commit 113c42199a
9 changed files with 167 additions and 0 deletions
@@ -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,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());
}
}
}