In my Spring Boot project I have a configuration class like this:
@Configuration
@RequiredArgsConstructor // ... using Lombok
public class MyConfigClass {
private final MyService myService;
@PostContruct
void setup(){
String s = myService.aMethod();
System.out.println(s.substring(0, 2));
}
}
Now I need to test some features of my project and for these tests I need to mock MyService
class:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class SomeOtherServiceTest {
@MockBean
private MyService mockMyService;
@BeforeEach
void setup() {
when(mockMyService.aMethod()).thenReturn("blablabla"); // ... using Mockito
}
@Test
void test() {
// test code
}
}
When I run test
I’m getting a NullPointerException
on s.substring(0, 2)
invocation in my configuration class. This means that the method has not been yet mocked at this point.
How can I achieve this?
You need to sign in to view this answers
Leave feedback about this