BAEL-4756 - Mockito MockSettings (#10423)
* BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson * BAEL-4756 - Mockito MockSettings Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
parent
1e2aa09bf7
commit
b9c81b0ced
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.mockito.mocksettings;
|
||||||
|
|
||||||
|
public abstract class AbstractCoffee {
|
||||||
|
|
||||||
|
protected String name;
|
||||||
|
|
||||||
|
protected AbstractCoffee(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.mockito.mocksettings;
|
||||||
|
|
||||||
|
public class SimpleService {
|
||||||
|
|
||||||
|
public SimpleService(SpecialInterface special) {
|
||||||
|
Runnable runnable = (Runnable) special;
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.mockito.mocksettings;
|
||||||
|
|
||||||
|
public interface SpecialInterface {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.baeldung.mockito.mocksettings;
|
||||||
|
|
||||||
|
import static org.mockito.Answers.RETURNS_SMART_NULLS;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.Answers.CALLS_REAL_METHODS;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.mockito.Mockito.withSettings;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.exceptions.verification.SmartNullPointerException;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import com.baeldung.mockito.fluentapi.Pizza;
|
||||||
|
import com.baeldung.mockito.fluentapi.PizzaService;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class MockSettingsUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = SmartNullPointerException.class)
|
||||||
|
public void whenServiceMockedWithSmartNulls_thenExceptionHasExtraInfo() {
|
||||||
|
PizzaService service = mock(PizzaService.class, withSettings().defaultAnswer(RETURNS_SMART_NULLS));
|
||||||
|
Pizza pizza = service.orderHouseSpecial();
|
||||||
|
pizza.getSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenServiceMockedWithNameAndVerboseLogging_thenLogsMethodInvocations() {
|
||||||
|
PizzaService service = mock(PizzaService.class, withSettings().name("pizzaServiceMock")
|
||||||
|
.verboseLogging());
|
||||||
|
|
||||||
|
Pizza pizza = mock(Pizza.class);
|
||||||
|
when(service.orderHouseSpecial()).thenReturn(pizza);
|
||||||
|
|
||||||
|
service.orderHouseSpecial();
|
||||||
|
|
||||||
|
verify(service).orderHouseSpecial();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenServiceMockedWithExtraInterfaces_thenConstructorSuccess() {
|
||||||
|
SpecialInterface specialMock = mock(SpecialInterface.class, withSettings().extraInterfaces(Runnable.class));
|
||||||
|
new SimpleService(specialMock);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMockSetupWithConstructor_thenConstructorIsInvoked() {
|
||||||
|
AbstractCoffee coffeeSpy = mock(AbstractCoffee.class, withSettings().useConstructor("expresso")
|
||||||
|
.defaultAnswer(CALLS_REAL_METHODS));
|
||||||
|
|
||||||
|
assertEquals("Coffee name: ", "expresso", coffeeSpy.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue