diff --git a/testing-modules/mockito-2/README.md b/testing-modules/mockito-2/README.md new file mode 100644 index 0000000000..08ce9910b5 --- /dev/null +++ b/testing-modules/mockito-2/README.md @@ -0,0 +1,5 @@ +## Mockito 2 + +This module contains articles about Mockito + +### Relevant Articles: diff --git a/testing-modules/mockito-2/pom.xml b/testing-modules/mockito-2/pom.xml new file mode 100644 index 0000000000..6ce6e8fca0 --- /dev/null +++ b/testing-modules/mockito-2/pom.xml @@ -0,0 +1,30 @@ + + + + testing-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + mockito-2 + + + 8 + 8 + UTF-8 + 4.8.1 + + + + + org.mockito + mockito-inline + ${mockito-inline.version} + test + + + + \ No newline at end of file diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/CacheManager.java b/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/CacheManager.java new file mode 100644 index 0000000000..51aea926b4 --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/CacheManager.java @@ -0,0 +1,29 @@ +package com.baeldung.singleton; + +import java.util.HashMap; + +public class CacheManager { + private final HashMap map; + + private static CacheManager instance; + + private CacheManager() { + map = new HashMap<>(); + } + + public static CacheManager getInstance() { + if(instance == null) { + instance = new CacheManager(); + } + + return instance; + } + + public T getValue(String key, Class clazz) { + return clazz.cast(map.get(key)); + } + + public Object setValue(String key, Object value) { + return map.put(key, value); + } +} \ No newline at end of file diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/Product.java b/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/Product.java new file mode 100644 index 0000000000..58905e3488 --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/Product.java @@ -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; + } +} diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/ProductDAO.java b/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/ProductDAO.java new file mode 100644 index 0000000000..ae9562561b --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/ProductDAO.java @@ -0,0 +1,8 @@ +package com.baeldung.singleton; + +public class ProductDAO { + public Product getProduct(String productName) { + + return new Product(productName, "description"); + } +} diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/ProductService.java b/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/ProductService.java new file mode 100644 index 0000000000..382087d96d --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/singleton/ProductService.java @@ -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; + } +} diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/singleton/ProductServiceTest.java b/testing-modules/mockito-2/src/test/java/com/baeldung/singleton/ProductServiceTest.java new file mode 100644 index 0000000000..792d472f94 --- /dev/null +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/singleton/ProductServiceTest.java @@ -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 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()); + } + } +} \ No newline at end of file diff --git a/testing-modules/mockito-2/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/testing-modules/mockito-2/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 0000000000..ca6ee9cea8 --- /dev/null +++ b/testing-modules/mockito-2/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline \ No newline at end of file diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 15d5637728..f237d2d6fc 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -49,6 +49,7 @@ testng-command-line xmlunit-2 zerocode + mockito-2 \ No newline at end of file