运行单元测试时出现 Spring 上下文问题

发布于 2024-10-13 05:12:28 字数 3232 浏览 3 评论 0原文

我正在使用 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)

谢谢

I am running some unit tests using Spring and Mockito.
I have configured the mocks in the springcontext.xml as follow:

<bean id="featureEndpoint" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="com.company.blah.blah.FeatureEndpoint" />
</bean>

My test class is constructed as follow:

@ContextConfiguration(locations= {"/springcontext.xml"})
public class FeatureEndpointValidationTest extends JsonEndpointValidationTest {

    private FeatureEndpoint featureEndpoint;

    @BeforeClass
    public void setUp() {
        featureEndpoint = getBean("featureEndpoint");
        super.setEndpoint(featureEndpoint);
    }

    @Test
    .
    .
    .
}

When I run this test, getBean() throws an NPE because context is null.
HOWEVER, if I rename the test class to say TestEndpoint (or anything that does not have the string 'Feature', it runs perfectly fine. Im flummoxed with this behaviour. I have searched all other config files for any naming clashes but none of them have any bean that contains the name 'feature'. Any clues as to why this is happening?

Here is the trace of the exception thrown:

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)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素食主义者 2024-10-20 05:12:28

您正在使用冲刺测试,这很好。尝试像这样设置您的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:springcontext.xml)
public class MyTest {

    @Autowired
    private FeatureEndpoint featureEndpoint;

    @Test
    public void testSomething() { }

}

在 Spring JUnit 运行程序类中使用 @Autowired 注释,所有自动装配变量将被初始化为其相应的相同类型的 bean。您还可以使用 @Qualifier("featureEndpoint") 来指定 Bean 名称。

如果您只想访问上下文,请实现包含 setApplicationContext 方法的 ApplicationContextAware

另外,如果你想验证bean,你可以实现InitializingBean,它在spring容器中会在实例化和初始化之后被调用,你可以在其中断言FeatureEndpoint是否不为null。

整个方程的关键是使用 SpringJUnit4ClassRunner 运行器类。

You're using sprint-test, which is good. Try setting up your test like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:springcontext.xml)
public class MyTest {

    @Autowired
    private FeatureEndpoint featureEndpoint;

    @Test
    public void testSomething() { }

}

Using the @Autowired annotation with the Spring JUnit runner class, all autowired variables will be initialized to their corresponding bean of the same type. You can also use @Qualifier("featureEndpoint") to specify a bean name.

If you simply want access to the context, implement ApplicationContextAware which contains a setApplicationContext method.

Also, if you want to verify beans, you can implement InitializingBean, which, in a spring container, will be called after instantiation and initialization, where you can assert whether or not FeatureEndpoint is not null.

The key to this whole equation though is to use the SpringJUnit4ClassRunner runner class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文