Moking a bean is null in Nested class
bittap opened this issue · 1 comments
bittap commented
Hello.
Moking a bean is null in Nested class.
@SpringBootTest
public class NestedTest {
@Autowired
MyService target;
@MockInBean(MyService.class)
ThirdPartyApiService thirdService;
String mockResult = "mock";
@Test
void resultReturnMockedValue() {
when(thirdService.print()).thenReturn(mockResult);
assertEquals(mockResult, target.invokeThirdPartySomeMethod());
}
@Nested
class Inner {
@Test
void resultReturnMockedValue() {
// java.lang.NullPointerException because "this.this$0.thirdService" is null
when(thirdService.print()).thenReturn(mockResult);
assertEquals(mockResult, target.invokeThirdPartySomeMethod());
}
}
}
@Service
class MyService {
@Autowired
public ThirdPartyApiService thirdService;
public String invokeThirdPartySomeMethod() {
return thirdService.print();
}
}
@Service
class ThirdPartyApiService {
public String print() {
return "noMock";
}
}
The resultReturnMockedValue in the NestedTest class is working well.
But, The resultReturnMockedValue in the Inner class which is Inner class of NestedTest class throw NullPointerException.
I want to mock in nested class.
I am using this version
<dependency>
<groupId>com.teketik</groupId>
<artifactId>mock-in-bean</artifactId>
<version>boot2-v1.4</version>
<scope>test</scope>
</dependency>
antoinemeyer commented
Thanks for reporting the bug.
Should be fixed in boot2-v1.5
. Let me know if you're still facing issues!