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

时间:2011-05-08 19:05:28

标签: java unit-testing spring maven-2 junit

我在spring mvc和maven上有网络应用程序。 当我执行“mvn clean install”时,我从一些uni-test获得了nullpointerexception。 这是因为其中一个资源为空,但为什么呢?

的Uni-测试:

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 Run中执行此测试时,作为JUnit测试 - 执行没有异常

surefire报告后的堆栈跟踪:


测试集:myapp.services.impl.MyServiceImplTest

测试运行:1,失败:1,错误:0,跳过:0,经过的时间:1.115秒&lt;&lt;&lt;失败! myapp.services.impl.MyServiceImplTest.someTest()经过的时间:1.068秒&lt;&lt;&lt;失败! 显示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
...

1 个答案:

答案 0 :(得分:0)

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

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

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

`