BAEL-6221 Get Mocks from MockedConstruction in Mockito (#14776)
Co-authored-by: jcook02 <jonathan.paul.cook@ext.esa.int>
This commit is contained in:
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.construction;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mockConstruction;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedConstruction;
|
||||
|
||||
class CoffeeMachineUnitTest {
|
||||
|
||||
@Test
|
||||
void givenNoMockedContructor_whenCoffeeMade_thenRealDependencyReturned() {
|
||||
CoffeeMachine machine = new CoffeeMachine();
|
||||
assertEquals("Finished making a delicious Espresso made with Guatemalan beans", machine.makeCoffee());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMockedContructor_whenCoffeeMade_thenMockDependencyReturned() {
|
||||
try (MockedConstruction<WaterTank> mockTank = mockConstruction(WaterTank.class); MockedConstruction<Grinder> mockGrinder = mockConstruction(Grinder.class)) {
|
||||
|
||||
CoffeeMachine machine = new CoffeeMachine();
|
||||
|
||||
WaterTank tank = mockTank.constructed()
|
||||
.get(0);
|
||||
Grinder grinder = mockGrinder.constructed()
|
||||
.get(0);
|
||||
|
||||
when(tank.isEspresso()).thenReturn(false);
|
||||
when(grinder.getBeans()).thenReturn("Peruvian");
|
||||
|
||||
assertEquals("Finished making a delicious Americano made with Peruvian beans", machine.makeCoffee());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMockedContructorWithArgument_whenCoffeeMade_thenMockDependencyReturned() {
|
||||
try (MockedConstruction<WaterTank> mockTank = mockConstruction(WaterTank.class, (mock, context) -> {
|
||||
int mils = (int) context.arguments().get(0);
|
||||
when(mock.getMils()).thenReturn(mils);
|
||||
});
|
||||
MockedConstruction<Grinder> mockGrinder = mockConstruction(Grinder.class)) {
|
||||
|
||||
CoffeeMachine machine = new CoffeeMachine(100);
|
||||
|
||||
Grinder grinder = mockGrinder.constructed()
|
||||
.get(0);
|
||||
|
||||
when(grinder.getBeans()).thenReturn("Kenyan");
|
||||
assertEquals("Finished making a delicious Americano made with Kenyan beans", machine.makeCoffee());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.construction;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.MockedConstruction;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.mockConstruction;
|
||||
|
||||
class FruitUnitTest {
|
||||
|
||||
@Test
|
||||
void givenMockedContructor_whenFruitCreated_thenMockIsReturned() {
|
||||
assertEquals("Apple", new Fruit().getName());
|
||||
assertEquals("Red", new Fruit().getColour());
|
||||
|
||||
try (MockedConstruction<Fruit> mock = mockConstruction(Fruit.class)) {
|
||||
|
||||
Fruit fruit = new Fruit();
|
||||
when(fruit.getName()).thenReturn("Banana");
|
||||
when(fruit.getColour()).thenReturn("Yellow");
|
||||
|
||||
assertEquals("Banana", fruit.getName());
|
||||
assertEquals("Yellow", fruit.getColour());
|
||||
|
||||
List<Fruit> constructed = mock.constructed();
|
||||
assertEquals(1, constructed.size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMockedContructorWithNewDefaultAnswer_whenFruitCreated_thenRealMethodInvoked() {
|
||||
try (MockedConstruction<Fruit> mock = mockConstruction(Fruit.class, withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS))) {
|
||||
|
||||
Fruit fruit = new Fruit();
|
||||
|
||||
assertEquals("Apple", fruit.getName());
|
||||
assertEquals("Red", fruit.getColour());
|
||||
|
||||
List<Fruit> constructed = mock.constructed();
|
||||
assertEquals(1, constructed.size());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user