Skip to content

Commit 01b7e20

Browse files
committed
Mockito stubbing setter getter initial commit
1 parent 4f4f7a3 commit 01b7e20

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.gettersetter;
2+
3+
public class ExampleService {
4+
5+
public Long getId(IdAndName idAndName) {
6+
return idAndName.getId();
7+
}
8+
9+
public String getName(IdAndName idAndName) {
10+
return idAndName.getName();
11+
}
12+
13+
public String getSuperComplicatedField(NonSimpleClass nonSimpleClass) {
14+
return nonSimpleClass.getSuperComplicatedField();
15+
}
16+
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.gettersetter;
2+
3+
public interface IdAndName {
4+
5+
Long getId();
6+
7+
String getName();
8+
9+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.gettersetter;
2+
3+
public class NonSimpleClass implements IdAndName {
4+
5+
private Long id;
6+
private String name;
7+
private String superComplicatedField;
8+
9+
public NonSimpleClass(Long id, String name, String superComplicatedField) {
10+
this.id = id;
11+
this.name = name;
12+
this.superComplicatedField = superComplicatedField;
13+
}
14+
15+
public NonSimpleClass() {
16+
}
17+
18+
@Override
19+
public Long getId() {
20+
return id;
21+
}
22+
23+
public void setId(Long id) {
24+
this.id = id;
25+
}
26+
27+
@Override
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getSuperComplicatedField() {
37+
return superComplicatedField;
38+
}
39+
40+
public void setSuperComplicatedField(String superComplicatedField) {
41+
this.superComplicatedField = superComplicatedField;
42+
}
43+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.gettersetter;
2+
3+
public class SimpleClass implements IdAndName {
4+
5+
private Long id;
6+
7+
private String name;
8+
9+
public SimpleClass(Long id, String name) {
10+
this.id = id;
11+
this.name = name;
12+
}
13+
14+
public SimpleClass() {
15+
}
16+
17+
@Override
18+
public Long getId() {
19+
return id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
@Override
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.gettersetter;
2+
3+
import static org.mockito.Mockito.doAnswer;
4+
import static org.mockito.Mockito.doNothing;
5+
import static org.mockito.Mockito.when;
6+
7+
import org.junit.Test;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.mockito.ArgumentCaptor;
10+
import org.mockito.ArgumentMatchers;
11+
import org.mockito.Mockito;
12+
import org.mockito.invocation.InvocationOnMock;
13+
import org.mockito.stubbing.Answer;
14+
15+
import net.bytebuddy.asm.Advice;
16+
17+
public class ExampleServiceTest {
18+
19+
private final ExampleService testee = new ExampleService();
20+
21+
@Test
22+
public void givenSimpleClass_whenInvokingGetId_thenReturnId() {
23+
SimpleClass simple = new SimpleClass(1L, "Jack");
24+
Assertions.assertEquals(testee.getId(simple), simple.getId());
25+
}
26+
27+
@Test
28+
public void givenSimpleClass_whenInvokingGetName_thenReturnName() {
29+
SimpleClass simple = new SimpleClass(1L, "Alex");
30+
Assertions.assertEquals(testee.getName(simple), simple.getName());
31+
}
32+
33+
@Test
34+
public void givenNonSimpleClass_whenInvokingGetName_thenReturnMockedName() {
35+
NonSimpleClass nonSimple = Mockito.mock(NonSimpleClass.class);
36+
when(nonSimple.getName()).thenReturn("Meredith");
37+
Assertions.assertEquals(testee.getName(nonSimple), "Meredith");
38+
}
39+
40+
static class Wrapper<T> {
41+
42+
private T value;
43+
44+
Wrapper(T value) {
45+
this.value = value;
46+
}
47+
48+
Wrapper(Class<T> value) {
49+
50+
}
51+
52+
T get() {
53+
return value;
54+
}
55+
56+
void set(T value) {
57+
this.value = value;
58+
}
59+
60+
}
61+
62+
@Test
63+
public void givenNonSimpleClass_whenInvokingGetName_thenReturnTheLatestNameSet() {
64+
Wrapper<String> nameWrapper = new Wrapper<>(String.class);
65+
NonSimpleClass nonSimple = Mockito.mock(NonSimpleClass.class);
66+
when(nonSimple.getName()).thenAnswer((Answer<String>) invocationOnMock -> nameWrapper.get());
67+
doAnswer(invocation -> {
68+
nameWrapper.set(invocation.getArgument(0));
69+
return null;
70+
}).when(nonSimple)
71+
.setName(ArgumentMatchers.anyString());
72+
nonSimple.setName("John");
73+
Assertions.assertEquals(testee.getName(nonSimple), "John");
74+
nonSimple.setName("Nick");
75+
Assertions.assertEquals(testee.getName(nonSimple), "Nick");
76+
}
77+
78+
}

0 commit comments

Comments
 (0)