Skip to content

Commit 2e58e30

Browse files
JonCookjcook02
andauthored
BAEL-6221 Get Mocks from MockedConstruction in Mockito (#14776)
Co-authored-by: jcook02 <jonathan.paul.cook@ext.esa.int>
1 parent 556002e commit 2e58e30

6 files changed

Lines changed: 187 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.construction;
2+
3+
public class CoffeeMachine {
4+
5+
private Grinder grinder;
6+
private WaterTank tank;
7+
8+
public CoffeeMachine() {
9+
this.grinder = new Grinder();
10+
this.tank = new WaterTank();
11+
}
12+
13+
public CoffeeMachine(int mils) {
14+
this.grinder = new Grinder();
15+
this.tank = new WaterTank(mils);
16+
}
17+
18+
public String makeCoffee() {
19+
String type = this.tank.isEspresso() ? "Espresso" : "Americano";
20+
return String.format("Finished making a delicious %s made with %s beans", type, this.grinder.getBeans());
21+
}
22+
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.construction;
2+
3+
public class Fruit {
4+
5+
public String getName() {
6+
return "Apple";
7+
}
8+
9+
public String getColour() {
10+
return "Red";
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.construction;
2+
3+
public class Grinder {
4+
5+
private String beans;
6+
7+
public Grinder() {
8+
this.beans = "Guatemalan";
9+
}
10+
11+
public String getBeans() {
12+
return beans;
13+
}
14+
15+
public void setBeans(String beans) {
16+
this.beans = beans;
17+
}
18+
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.construction;
2+
3+
public class WaterTank {
4+
5+
private int mils;
6+
7+
public WaterTank() {
8+
this.mils = 25;
9+
}
10+
11+
public WaterTank(int mils) {
12+
this.mils = mils;
13+
}
14+
15+
public int getMils() {
16+
return mils;
17+
}
18+
19+
public void setMils(int mils) {
20+
this.mils = mils;
21+
}
22+
23+
public boolean isEspresso() {
24+
return getMils() < 50;
25+
}
26+
27+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.construction;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.Mockito.mockConstruction;
5+
import static org.mockito.Mockito.when;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.mockito.MockedConstruction;
9+
10+
class CoffeeMachineUnitTest {
11+
12+
@Test
13+
void givenNoMockedContructor_whenCoffeeMade_thenRealDependencyReturned() {
14+
CoffeeMachine machine = new CoffeeMachine();
15+
assertEquals("Finished making a delicious Espresso made with Guatemalan beans", machine.makeCoffee());
16+
}
17+
18+
@Test
19+
void givenMockedContructor_whenCoffeeMade_thenMockDependencyReturned() {
20+
try (MockedConstruction<WaterTank> mockTank = mockConstruction(WaterTank.class); MockedConstruction<Grinder> mockGrinder = mockConstruction(Grinder.class)) {
21+
22+
CoffeeMachine machine = new CoffeeMachine();
23+
24+
WaterTank tank = mockTank.constructed()
25+
.get(0);
26+
Grinder grinder = mockGrinder.constructed()
27+
.get(0);
28+
29+
when(tank.isEspresso()).thenReturn(false);
30+
when(grinder.getBeans()).thenReturn("Peruvian");
31+
32+
assertEquals("Finished making a delicious Americano made with Peruvian beans", machine.makeCoffee());
33+
}
34+
35+
}
36+
37+
@Test
38+
void givenMockedContructorWithArgument_whenCoffeeMade_thenMockDependencyReturned() {
39+
try (MockedConstruction<WaterTank> mockTank = mockConstruction(WaterTank.class, (mock, context) -> {
40+
int mils = (int) context.arguments().get(0);
41+
when(mock.getMils()).thenReturn(mils);
42+
});
43+
MockedConstruction<Grinder> mockGrinder = mockConstruction(Grinder.class)) {
44+
45+
CoffeeMachine machine = new CoffeeMachine(100);
46+
47+
Grinder grinder = mockGrinder.constructed()
48+
.get(0);
49+
50+
when(grinder.getBeans()).thenReturn("Kenyan");
51+
assertEquals("Finished making a delicious Americano made with Kenyan beans", machine.makeCoffee());
52+
}
53+
54+
}
55+
56+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.construction;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.mockito.Answers;
5+
import org.mockito.MockedConstruction;
6+
7+
import static org.mockito.Mockito.when;
8+
import static org.mockito.Mockito.withSettings;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
import java.util.List;
12+
13+
import static org.mockito.Mockito.mockConstruction;
14+
15+
class FruitUnitTest {
16+
17+
@Test
18+
void givenMockedContructor_whenFruitCreated_thenMockIsReturned() {
19+
assertEquals("Apple", new Fruit().getName());
20+
assertEquals("Red", new Fruit().getColour());
21+
22+
try (MockedConstruction<Fruit> mock = mockConstruction(Fruit.class)) {
23+
24+
Fruit fruit = new Fruit();
25+
when(fruit.getName()).thenReturn("Banana");
26+
when(fruit.getColour()).thenReturn("Yellow");
27+
28+
assertEquals("Banana", fruit.getName());
29+
assertEquals("Yellow", fruit.getColour());
30+
31+
List<Fruit> constructed = mock.constructed();
32+
assertEquals(1, constructed.size());
33+
}
34+
}
35+
36+
@Test
37+
void givenMockedContructorWithNewDefaultAnswer_whenFruitCreated_thenRealMethodInvoked() {
38+
try (MockedConstruction<Fruit> mock = mockConstruction(Fruit.class, withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS))) {
39+
40+
Fruit fruit = new Fruit();
41+
42+
assertEquals("Apple", fruit.getName());
43+
assertEquals("Red", fruit.getColour());
44+
45+
List<Fruit> constructed = mock.constructed();
46+
assertEquals(1, constructed.size());
47+
}
48+
}
49+
50+
}

0 commit comments

Comments
 (0)