我有一些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 {
}
@Service
public 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 getDefaultTestExecutionListenerClassNames
INFO: Loaded default TestExecutionListener class names from location [Meta-INF/spring.factories]: []
Sep 05,2018 5:15:01 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: []
.E
Time: 0.007
There 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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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
descriptor – assembly.xml
您可以在github:https://github.com/SaiUpadhyayula/executabletests中查看示例项目
最佳答案
我刚刚分叉你的存储库,我发现了错误.弹簧工厂必须与maven抛出测试时使用的工厂相同.以与测试上下文相同的方式委托Spring Ic是必须的.
原文链接:https://www.f2er.com/spring/431398.html请参阅拉取请求获取附加信息: