I'm struggling to find out how to ignore a class method, which should start a thread when SpringBootApplication is ready, during normal operation:
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
this.start();
}
in case of testing I do not want such behavior, want to start it from test method.
As far as i understand ContextRefreshedEvent
is triggered by @SpringBootTest
annotation on test class.
You do not need @SpringBootTest
for every test of Spring Boot application (I would argue that you want minimum of such tests actually as they load everything.)
There are other options:
@JsonTest
will autoconfigure ObjectMapper
and other beans for working with JSON. There's quite a few of them so check documentation if there's any part of application you would like to be autoconfigured for your test.@SpringBootTest
tests:I see two options:
@MockBeans
.@SpringBootTest
@MockBeans(@MockBean(Listener.class))
public class SomeTest {
// ...
}
@Component
@Profile("default") // OR: @Profile("!test")
public class Listener {
// ...
}
@SpringBootTest
@ActiveProfiles("test")
public class SomeTest {
// ...
}
You might need to extract the listener from the existing bean if you need the bean as a dependency for another service though.