java - 不要在 uni-test 中启动 spring 上下文

标签 java unit-testing spring maven-2 junit

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

统一测试:

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 as JUnit Test 中执行此测试时 - 执行没有异常

万无一失的报告后的堆栈跟踪:

<小时/>

测试集: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
...

最佳答案

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

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

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

`

关于java - 不要在 uni-test 中启动 spring 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5929621/

相关文章:

java - Spring Form 一对多绑定(bind)对象

java - 填充二维数组迷宫时数组越界异常

c - 根据差异确定要运行的单元测试

java - 仅当列表不为空并且尚未在列表中时,如何将对象添加到列表中?

.net - 使用 FakeItEasy 断言已引发事件

javascript - rails : best way to test an action called remotely?

java - Spring DAO 测试

java - 无法在多模块 Spring Boot 项目中使用 @EnableJpaRepositories

java - 从外部应用程序的屏幕获取数据

java - 如何在 Java 中遍历 Map?