我有一个春季启动应用程序。有时我需要启动长时间运行的报告生成。最简单的方法是创建一个@IntegrationTest
来使用spring @Autowire
:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes=Application.class)
@IntegrationTest
public class ReportGenerationTest {
@Autowired MyService myService;
@Value("classpath:/test.txt") Resource testData; //it's in test, not in src
@Test
public void generate_report() {
report(myService, testData);
}
}
它完美无缺。但我不希望它与每个构建一起运行。我也不想添加/删除@Ignore
,因为有人会在没有@Ignore
的情况下意外提交它,而且我不想仅仅为了运行报告而进行任何代码编辑
理想情况下,我希望它是按需运行的main
方法。但是如何为src服务和测试资源手动创建弹簧上下文?
我需要类似的东西:
public class MyReport {
public static void main(String[] args) {
ReportGenerationTest reportGenerator = getAutowiredInstanceFromSpring();
reportGenerator.generate_report();
}
}