不要在单测试中启动 spring 上下文

发布于 2024-11-05 23:23:22 字数 3227 浏览 1 评论 0原文

我在 spring mvc 和 Maven 上有 Web 应用程序。 当我执行“mvn clean install”时,我从一些单一测试中得到了空指针异常。 发生这种情况是因为其中一个资源为空,但为什么呢?

Uni-test:

package myapp.services.impl

....

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/spring/applicationContext.xml"})
@TransactionConfiguration
public class MyServiceImplTest {

    @Resource
    private MyService myService;

    @Transactional
    @Test
    public void someTest() {
        SomeEntity entity = new SomeEntity();
        myService.createSomething(entity);  // THROW - NullPointerException, myService is NULL
        ...
    }
}

pom.xml中的surefire插件:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
            <configuration>
                <systemProperties>
                    <property>
                        <name>myapp.env</name>
                        <value>test</value>
                    </property>
                </systemProperties>
                <junitArtifactName>org.junit:com.springsource.org.junit</junitArtifactName>
            </configuration>
        </plugin>

和applicationContext.xml:

...
<context:component-scan base-package="myapp.services,myapp.services.impl"/>

<context:property-placeholder location="classpath*:META-INF/spring/common_${myapp.env}.properties"
                              system-properties-mode="OVERRIDE"
                              ignore-resource-not-found="true"/>
....

PS:当我在eclipse中执行此测试时,作为JUnit测试运行 - 执行没有异常,

surefire报告后的堆栈跟踪:


测试集:myapp.services.impl .MyServiceImplTest

测试运行:1,失败:1,错误:0,跳过:0,已用时间:1.115 秒 <<<失败! myapp.services.impl.MyServiceImplTest.someTest() 已用时间:1.068 秒 <<<失败! java.lang.NullPointerException 在 myapp.services.impl.MyServiceImplTest.someTest(MyServiceImplTest.java:42)

MAVEN 输出:

...
[INFO] --- maven-surefire-plugin:2.4.3:test (default-test) @ mywebapp ---
[INFO] Surefire report directory: /home/xxx/Work/mywebapp/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running myapp.services.impl.MyServiceImplTest
log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender.
log4j:WARN No such property [maxBackupIndex] in     org.apache.log4j.DailyRollingFileAppender.
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.919 sec <<< FAILURE!


Results :

Failed tests: 
  myapp.services.impl.MyServiceImplTest.someTest()

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 41.465s
[INFO] Finished at: Mon May 09 00:05:06 MSD 2011
[INFO] Final Memory: 26M/70M
...

I have web application on spring mvc and maven.
When I execute "mvn clean install" I got nullpointerexception from some uni-test.
It's happens because one of resource is null, but why ?

Uni-test:

package myapp.services.impl

....

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/spring/applicationContext.xml"})
@TransactionConfiguration
public class MyServiceImplTest {

    @Resource
    private MyService myService;

    @Transactional
    @Test
    public void someTest() {
        SomeEntity entity = new SomeEntity();
        myService.createSomething(entity);  // THROW - NullPointerException, myService is NULL
        ...
    }
}

surefire plugin in pom.xml:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
            <configuration>
                <systemProperties>
                    <property>
                        <name>myapp.env</name>
                        <value>test</value>
                    </property>
                </systemProperties>
                <junitArtifactName>org.junit:com.springsource.org.junit</junitArtifactName>
            </configuration>
        </plugin>

And applicationContext.xml:

...
<context:component-scan base-package="myapp.services,myapp.services.impl"/>

<context:property-placeholder location="classpath*:META-INF/spring/common_${myapp.env}.properties"
                              system-properties-mode="OVERRIDE"
                              ignore-resource-not-found="true"/>
....

PS: When I execute this test in eclipse Run as JUnit Test - execute is fine without exception

Stack-trace after surefire report:


Test set: myapp.services.impl.MyServiceImplTest

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.115 sec <<< FAILURE!
myapp.services.impl.MyServiceImplTest.someTest() Time elapsed: 1.068 sec <<< FAILURE!
java.lang.NullPointerException
at myapp.services.impl.MyServiceImplTest.someTest(MyServiceImplTest.java:42)

MAVEN OUTPUT:

...
[INFO] --- maven-surefire-plugin:2.4.3:test (default-test) @ mywebapp ---
[INFO] Surefire report directory: /home/xxx/Work/mywebapp/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running myapp.services.impl.MyServiceImplTest
log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender.
log4j:WARN No such property [maxBackupIndex] in     org.apache.log4j.DailyRollingFileAppender.
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.919 sec <<< FAILURE!


Results :

Failed tests: 
  myapp.services.impl.MyServiceImplTest.someTest()

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 41.465s
[INFO] Finished at: Mon May 09 00:05:06 MSD 2011
[INFO] Final Memory: 26M/70M
...

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

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

发布评论

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

评论(1

清秋悲枫 2024-11-12 23:23:22

只是一个想法,但是...

您说您已将 applicationContext.xml 放在 test/resources 文件夹中 - 您是否重新创建了在该目录中定义的目录结构?即测试/资源/META-INF/spring/applicationcontext.xml?如果没有,那么 applicationcontext 将出现在 test-classes/ 中,并且不会根据您定义的类路径位置找到...如果它只是在 test/resources 中,请将 @ContextConfiguration 注释更改为

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})`

`

Just a thought but...

You say you've placed applicationContext.xml in the test/resources folder - have you recreated the directory structure you define within that directory? I.e. test/resources/META-INF/spring/applicationcontext.xml? If not, then the applicationcontext will appear in test-classes/ and won't be found according to the classpath location you've defined... If it's just in test/resources, change your @ContextConfiguration annotation to

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})`

`

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