I have a service class which starts a thread and run some background tasks. These tasks should not be run while I'm running a unit test. The service class itself is so simple that it doesn't need to be unit tested. It is like:
@Service
public class BackgroundTaskService {
@PostConstruct
public void startTask() {
// ...
}
}
Currently I'm setting a system property to declare that a unit test is running:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeTest {
static {
System.setProperty("unit_testing", "true");
}
@Test
public void test() {}
}
Then I can check:
@PostConstruct
public void startTask() {
if (System.getProperty("unit_testing") != null) {
return; // skip background tasks
}
}
I'd like to know if there is a better way to do that.