我正在使用 Spring 和 Mockito 运行一些单元测试。我在 springcontext.xml 中配置了模拟如下:
<bean id="featureEndpoint" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.company.blah.blah.FeatureEndpoint" />
</bean>
我的测试类构造如下:
@ContextConfiguration(locations= {"/springcontext.xml"})
public class FeatureEndpointValidationTest extends JsonEndpointValidationTest {
private FeatureEndpoint featureEndpoint;
@BeforeClass
public void setUp() {
featureEndpoint = getBean("featureEndpoint");
super.setEndpoint(featureEndpoint);
}
@Test
.
.
.
}
当我运行这个测试时,getBean() 会抛出一个 NPE,因为上下文为空。但是,如果我将测试类重命名为 TestEndpoint (或任何没有字符串'Feature'的东西,它运行得非常好。我对这种行为感到困惑。我已经搜索了所有其他配置文件以查找任何命名冲突,但没有一个有任何包含名称'feature'的bean。关于为什么会发生这种情况的任何线索?
这是抛出异常的跟踪:
java.lang.NullPointerException
at com.foo.bar.UnitTest.getBean(UnitTest.java:14)
at com.foo.bar.service.api.rest.FeatureEndpointValidationTest.setUp(FeatureEndpointValidationTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:398)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:145)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:82)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:167)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
at org.testng.TestRunner.runWorkers(TestRunner.java:712)
at org.testng.TestRunner.privateRun(TestRunner.java:582)
at org.testng.TestRunner.run(TestRunner.java:477)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:324)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
at org.testng.SuiteRunner.run(SuiteRunner.java:198)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:821)
at org.testng.TestNG.runSuitesLocally(TestNG.java:788)
at org.testng.TestNG.run(TestNG.java:708)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:62)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:141)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
谢谢