我有一个 SpringBootApplication 类,它有一个@PostConstruct
类似的方法(它初始化数据库连接的类型):
@SpringBootApplication
public class SpringBootApp extends WebMvcConfigurerAdapter {
public static boolean workOffline = false;
private boolean setupSchema = false;
private IGraphService graphService;
private DbC conf;
@Autowired
public SpringBootApp(IGraphService graphService, DbC conf)
{
this.graphService = graphService;
this.conf = conf;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootApp.class, args);
}
@PostConstruct
public void initializeDB() {
if (workOffline) {
conf.setupOfflineEnvironment();
return;
}
else {
conf.setupProdEnvironment();
}
if (setupSchema) {
graphService.setupTestUsers();
}
}
}
我也在使用extend
这个基类的 Spring Boot 测试:
@RunWith(SpringRunner.class)
@Ignore
@SpringBootTest
public class BaseTest {
@Before
public void beforeTest() {
if (SpringBootApp.workOffline) {
conf.setupOfflineEnvironment();
} else {
conf.setupTestEnvironment();
}
graphService.setupTestUsers();}
@After
public void afterTest() {
graphService.deleteAllData();
}
}
我的测试在tests/
我的源代码下src/
不幸的是,有些情况beforeTest()
会在之前执行@PostConstuct
,有些情况会在之后执行。有没有办法让我的测试在@SprinbBootTest
不进入/构建SpringBootApp
类的情况下运行?
谢谢!