BAEL-6221 Get Mocks from MockedConstruction in Mockito (#14776)
Co-authored-by: jcook02 <jonathan.paul.cook@ext.esa.int>
This commit is contained in:
parent
556002e8d1
commit
2e58e30ffa
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.construction;
|
||||
|
||||
public class CoffeeMachine {
|
||||
|
||||
private Grinder grinder;
|
||||
private WaterTank tank;
|
||||
|
||||
public CoffeeMachine() {
|
||||
this.grinder = new Grinder();
|
||||
this.tank = new WaterTank();
|
||||
}
|
||||
|
||||
public CoffeeMachine(int mils) {
|
||||
this.grinder = new Grinder();
|
||||
this.tank = new WaterTank(mils);
|
||||
}
|
||||
|
||||
public String makeCoffee() {
|
||||
String type = this.tank.isEspresso() ? "Espresso" : "Americano";
|
||||
return String.format("Finished making a delicious %s made with %s beans", type, this.grinder.getBeans());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.construction;
|
||||
|
||||
public class Fruit {
|
||||
|
||||
public String getName() {
|
||||
return "Apple";
|
||||
}
|
||||
|
||||
public String getColour() {
|
||||
return "Red";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.construction;
|
||||
|
||||
public class Grinder {
|
||||
|
||||
private String beans;
|
||||
|
||||
public Grinder() {
|
||||
this.beans = "Guatemalan";
|
||||
}
|
||||
|
||||
public String getBeans() {
|
||||
return beans;
|
||||
}
|
||||
|
||||
public void setBeans(String beans) {
|
||||
this.beans = beans;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.construction;
|
||||
|
||||
public class WaterTank {
|
||||
|
||||
private int mils;
|
||||
|
||||
public WaterTank() {
|
||||
this.mils = 25;
|
||||
}
|
||||
|
||||
public WaterTank(int mils) {
|
||||
this.mils = mils;
|
||||
}
|
||||
|
||||
public int getMils() {
|
||||
return mils;
|
||||
}
|
||||
|
||||
public void setMils(int mils) {
|
||||
this.mils = mils;
|
||||
}
|
||||
|
||||
public boolean isEspresso() {
|
||||
return getMils() < 50;
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue