Skip to content

Commit 2520e83

Browse files
committed
#27 - Tests for - No injection of dependencies when using withSpy() and field injection
1 parent 7506557 commit 2520e83

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.example.coffee.fruit;
2+
3+
import javax.inject.Inject;
4+
import javax.inject.Singleton;
5+
6+
@Singleton
7+
class AppleService {
8+
9+
@Inject
10+
BananaService bananaService;
11+
12+
@Inject
13+
PeachService peachService;
14+
15+
String ban() {
16+
return bananaService.ban("hello");
17+
}
18+
19+
20+
void apple(String a, String b, String c) {
21+
System.out.println("apple> a: " + a + " b:" + b + " c:" + c);
22+
bananaService.banana(a, b, c);
23+
peachService.peach(a, b, c);
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.example.coffee.fruit;
2+
3+
import io.dinject.BeanContext;
4+
import io.dinject.BootContext;
5+
import org.junit.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.mockito.ArgumentMatchers.anyString;
9+
import static org.mockito.Mockito.doNothing;
10+
11+
public class AppleServiceTest {
12+
13+
@Test
14+
public void test_spyWithFieldInjection() {
15+
16+
BootContext bootContext = new BootContext();
17+
bootContext.withSpy(AppleService.class);
18+
19+
try (BeanContext beanContext = bootContext.load()) {
20+
21+
AppleService appleService = beanContext.getBean(AppleService.class);
22+
23+
doNothing()
24+
.when(appleService)
25+
.apple(anyString(), anyString(), anyString());
26+
27+
assertThat(appleService.bananaService).isNotNull();
28+
assertThat(appleService.peachService).isNotNull();
29+
30+
appleService.apple("one", "two", "three");
31+
32+
String out = appleService.ban();
33+
assertThat(out).isEqualTo("hello banana peach");
34+
}
35+
}
36+
37+
@Test
38+
public void test_whenNoMockOrSpy() {
39+
40+
BootContext bootContext = new BootContext();
41+
42+
try (BeanContext beanContext = bootContext.load()) {
43+
44+
AppleService appleService = beanContext.getBean(AppleService.class);
45+
46+
assertThat(appleService.bananaService).isNotNull();
47+
assertThat(appleService.peachService).isNotNull();
48+
49+
appleService.apple("one", "two", "three");
50+
}
51+
}
52+
53+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.example.coffee.fruit;
2+
3+
import javax.inject.Inject;
4+
import javax.inject.Singleton;
5+
6+
@Singleton
7+
class BananaService {
8+
9+
@Inject
10+
PeachService peachService;
11+
12+
String banana(String a, String b, String c) {
13+
System.out.println("banana> a: " + a + " b:" + b + " c:" + c);
14+
return " banana " + peachService.poke();
15+
}
16+
17+
String ban(String hello) {
18+
return hello+" banana "+peachService.poke();
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.example.coffee.fruit;
2+
3+
import javax.inject.Singleton;
4+
5+
@Singleton
6+
class PeachService {
7+
8+
String poke() {
9+
return "peach";
10+
}
11+
12+
String peach(String a, String b, String c) {
13+
System.out.println("peach> a: " + a + " b:" + b + " c:" + c);
14+
return " peach";
15+
}
16+
}

src/test/java/org/example/coffee/grind/Grinder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package org.example.coffee.grind;
22

3+
import lombok.EqualsAndHashCode;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import lombok.ToString;
37
import lombok.extern.slf4j.Slf4j;
48

59
import javax.annotation.PostConstruct;
610
import javax.inject.Inject;
711
import javax.inject.Singleton;
812

13+
14+
@EqualsAndHashCode
15+
@ToString
16+
@Getter
17+
@Setter
918
@Slf4j
1019
@Singleton
1120
public class Grinder {

0 commit comments

Comments
 (0)