Я провожу модульное тестирование нескольких компонентов пружины, основным из которых является ApplicationRunner
, который автоматически запускается пружиной.
Однако перед этим * 1004 мне нужно выполнить фрагмент кода* bean называется.
=> Как я могу это сделать?
Я пытался использовать @Before
и @PostConstruct
, но оба они вызывались после AppllicationRunner.
Я не могу поделиться реальным кодом, но вот принцип:
@RunWith(SpringRunner.class)
// The class to be tested
// Comes with many bean, the main one being an ApplicationRunner
@SpringBootTest(classes = { MyApplication.class })
public class MyApplicationTest {
@Autowired private SomeProperties someProperties;
// the method I need to run before the ApplicationRunner
@PostConstruct // also tried @Before
public void clean() throws IOException {
// need to be sure some output dir is empty before starting
cleanOutputDirectory(someProperties);
}
@Test
public void testApplication() throws InterruptedException {
// test the ApplicationRunner has written some files
// in the output directory
}
}