我有一些Spring测试可以启动应用程序上下文并测试一些服务.我可以使用Maven和IDE运行这些测试.现在我需要在无法访问Maven的其他机器上运行这些测试.我的想法是创建一个测试jar并通过命令行运行它们.
所以我创建了一个自定义Runner来调用我需要的测试类,这些测试将启动Spring Application上下文并测试一些服务.
以下是示例代码:
我的自定义亚军:
public class Main { public static voID main(String[] args) { System.out.println("Running tests!"); JUnitCore engine = new JUnitCore(); engine.addListener(new TextListener(System.out)); engine.run(SpringSampleTest.class); }}
上面的跑步者正在调用此测试
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = {AppConfig.class})public class SpringSampleTest { @autowired TestService testService; @Test public voID testSimple() { assertTrue("Test Simple",testService.isValID()); }}
这是我的配置和服务
@Configuration@ComponentScan(basePackages = {"mypackage"})public class AppConfig {}@Servicepublic class TestService { public boolean isValID() { return true; }}
所以要从jar运行这些测试,我已经使用assembly-plugin来创建一个包含我所有测试和依赖项的可执行jar(感谢这个答案here).现在,当我运行这个可执行jar时,我的自定义运行器(Main.java)能够触发测试,但是它没有加载Spring Context并且因为我的依赖项没有自动装配而失败并出现NullPointer异常.这是日志:
Running tests!Sep 05,2018 5:15:01 PM org.springframework.test.context.support.DefaultTestContextbootstrapper getDefaultTestExecutionListenerClassnamesINFO: Loaded default TestExecutionListener class names from location [meta-inf/spring.factorIEs]: []Sep 05,2018 5:15:01 PM org.springframework.test.context.support.DefaultTestContextbootstrapper getTestExecutionListenersINFO: Using TestExecutionListeners: [].ETime: 0.007There was 1 failure:1) testSimple(com.c0deattack.cu.runners.SpringSampleTest)java.lang.NullPointerException at com.c0deattack.cu.runners.SpringSampleTest.testSimple(SpringSampleTest.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runchild(SpringJUnit4ClassRunner.java:252) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runchild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.junit.runners.Suite.runchild(Suite.java:128) at org.junit.runners.Suite.runchild(Suite.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runchildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at org.junit.runner.JUnitCore.run(JUnitCore.java:105) at org.junit.runner.JUnitCore.run(JUnitCore.java:94) at com.c0deattack.cu.runners.Main.main(Main.java:15)FAILURES!!!Tests run: 1,Failures: 1
有人可以指出我做错了什么吗?
我还添加了我的pom.xml和汇编描述符文件:
的pom.xml
descriptor – assembly.xml
您可以在github:https://github.com/SaiUpadhyayula/executabletests中查看示例项目最佳答案我刚刚分叉你的存储库,我发现了错误.d簧工厂必须与maven抛出测试时使用的工厂相同.以与测试上下文相同的方式委托Spring Ic是必须的.
请参阅拉取请求获取附加信息:
https://github.com/SaiUpadhyayula/executabletests/pull/2
总结 以上是内存溢出为你收集整理的java – 从可执行jar运行spring测试全部内容,希望文章能够帮你解决java – 从可执行jar运行spring测试所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)