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

View File

@ -0,0 +1,5 @@
## Mockito 2
This module contains articles about Mockito
### Relevant Articles:

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>testing-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mockito-2</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mockito-inline.version>4.8.1</mockito-inline.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito-inline.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.singleton;
public class ProductDAO {
public Product getProduct(String productName) {
return new Product(productName, "description");
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}
}

View File

@ -49,6 +49,7 @@
<module>testng-command-line</module>
<module>xmlunit-2</module>
<module>zerocode</module>
<module>mockito-2</module>
</modules>
</project>