add all file to git

This commit is contained in:
2024-04-30 12:21:36 -04:00
parent a4683ce0d6
commit c18eb6d2ed
43 changed files with 1032 additions and 0 deletions
@@ -0,0 +1,22 @@
package com.baeldung.constructor;
public class PaymentProcessor {
private final PaymentService paymentService;
public PaymentProcessor(PaymentService paymentService) {
this.paymentService = paymentService;
}
public PaymentProcessor() {
this.paymentService = new PaymentService();
}
public PaymentProcessor(String paymentMode) {
this.paymentService = new PaymentService(paymentMode);
}
public String processPayment(){
return paymentService.processPayment();
}
}
@@ -0,0 +1,19 @@
package com.baeldung.constructor;
public class PaymentService {
private final String paymentMode;
public PaymentService(String paymentMode) {
this.paymentMode = paymentMode;
}
public PaymentService() {
this.paymentMode = "Cash";
}
public String processPayment(){
// simulate processing payment and returns the payment mode
return this.paymentMode;
}
}
@@ -0,0 +1,9 @@
package com.baeldung.nullmatcher;
class Helper {
String concat(String a, String b) {
return a + b;
}
}
@@ -0,0 +1,11 @@
package com.baeldung.nullmatcher;
class Main {
Helper helper = new Helper();
String methodUnderTest() {
return helper.concat("Baeldung", null);
}
}
@@ -0,0 +1,7 @@
package com.baeldung.onemethodmultipleparams;
public class ExampleService {
public int getValue(int arg) {
return 1;
}
}
@@ -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,9 @@
package com.baeldung.wantedbutnotinvocked;
class Helper {
String getBaeldungString() {
return "Baeldung";
}
}
@@ -0,0 +1,14 @@
package com.baeldung.wantedbutnotinvocked;
class Main {
Helper helper = new Helper();
String methodUnderTest(int i) {
if (i > 5) {
return helper.getBaeldungString();
}
return "Hello";
}
}
@@ -0,0 +1,63 @@
package com.baeldung.constructor;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
class PaymentServiceUnitTest {
@Test
void whenConstructorInvokedWithInitializer_ThenMockObjectShouldBeCreated(){
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class,(mock,context)-> when(mock.processPayment()).thenReturn("Credit"))){
PaymentProcessor paymentProcessor = new PaymentProcessor();
Assertions.assertEquals(1,mockPaymentService.constructed().size());
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
}
}
@Test
void whenConstructorInvokedWithoutInitializer_ThenMockObjectShouldBeCreatedWithNullFields(){
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class)){
PaymentProcessor paymentProcessor = new PaymentProcessor();
Assertions.assertEquals(1,mockPaymentService.constructed().size());
Assertions.assertNull(paymentProcessor.processPayment());
}
}
@Test
void whenConstructorInvokedWithParameters_ThenMockObjectShouldBeCreated(){
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class,(mock, context) -> when(mock.processPayment()).thenReturn("Credit"))){
PaymentProcessor paymentProcessor = new PaymentProcessor("Debit");
Assertions.assertEquals(1,mockPaymentService.constructed().size());
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
}
}
@Test
void whenMultipleConstructorsInvoked_ThenMultipleMockObjectsShouldBeCreated(){
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class)){
PaymentProcessor paymentProcessor = new PaymentProcessor();
PaymentProcessor secondPaymentProcessor = new PaymentProcessor();
PaymentProcessor thirdPaymentProcessor = new PaymentProcessor("Debit");
when(mockPaymentService.constructed().get(0).processPayment()).thenReturn("Credit");
when(mockPaymentService.constructed().get(1).processPayment()).thenReturn("Online Banking");
Assertions.assertEquals(3,mockPaymentService.constructed().size());
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
Assertions.assertEquals("Online Banking", secondPaymentProcessor.processPayment());
Assertions.assertNull(thirdPaymentProcessor.processPayment());
}
}
@Test
void whenDependencyInjectionIsUsed_ThenMockObjectShouldBeCreated(){
PaymentService mockPaymentService = Mockito.mock(PaymentService.class);
PaymentProcessor paymentProcessor = new PaymentProcessor(mockPaymentService);
when(mockPaymentService.processPayment()).thenReturn("Online Banking");
Assertions.assertEquals("Online Banking", paymentProcessor.processPayment());
}
}
@@ -0,0 +1,40 @@
package com.baeldung.nullmatcher;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
class MainUnitTest {
@Mock
Helper helper;
@InjectMocks
Main main;
@BeforeEach
void openMocks() {
MockitoAnnotations.openMocks(this);
}
@Test
void whenMethodUnderTest_thenSecondParameterNull() {
main.methodUnderTest();
Mockito.verify(helper)
.concat("Baeldung", null);
}
@Test
void whenMethodUnderTest_thenSecondParameterNullWithMatchers() {
main.methodUnderTest();
Mockito.verify(helper)
.concat(any(), isNull());
}
}
@@ -0,0 +1,63 @@
package com.baeldung.onemethodmultipleparams;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class OneMethodMultipleParamsUnitTest {
@Mock
ExampleService exampleService;
@Test
public void givenAMethod_whenStubbingForMultipleArguments_thenExpectDifferentResults() {
when(exampleService.getValue(10)).thenReturn(100);
when(exampleService.getValue(20)).thenReturn(200);
when(exampleService.getValue(30)).thenReturn(300);
assertEquals(100, exampleService.getValue(10));
assertEquals(200, exampleService.getValue(20));
assertEquals(300, exampleService.getValue(30));
}
@Test
public void givenAMethod_whenUsingThenAnswer_thenExpectDifferentReults() {
when(exampleService.getValue(anyInt())).thenAnswer(invocation -> {
int argument = (int) invocation.getArguments()[0];
int result;
switch (argument) {
case 25:
result = 125;
break;
case 50:
result = 150;
break;
case 75:
result = 175;
break;
default:
result = 0;
}
return result;
});
assertEquals(125, exampleService.getValue(25));
assertEquals(150, exampleService.getValue(50));
assertEquals(175, exampleService.getValue(75));
}
@Test
public void givenAMethod_whenUsingConsecutiveStubbing_thenExpectResultsInOrder() {
when(exampleService.getValue(anyInt())).thenReturn(9, 18, 27);
assertEquals(9, exampleService.getValue(1));
assertEquals(18, exampleService.getValue(1));
assertEquals(27, exampleService.getValue(1));
assertEquals(27, exampleService.getValue(1));
}
}
@@ -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());
}
}
}
@@ -0,0 +1,38 @@
package com.baeldung.wantedbutnotinvocked;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
class MainUnitTest {
@Mock
Helper helper;
@InjectMocks
Main main = new Main();
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void givenValueUpperThan5_WhenMethodUnderTest_ThenDelegatesToHelperClass() {
main.methodUnderTest(7);
Mockito.verify(helper)
.getBaeldungString();
}
// Uncomment the next line to see the error
// @Test
void givenValueLowerThan5_WhenMethodUnderTest_ThenDelegatesToGetBaeldungString() {
main.methodUnderTest(3);
Mockito.verify(helper)
.getBaeldungString();
}
}