有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不在unitest中启动spring上下文

我有spring mvc和maven上的web应用程序。 当我执行“mvn clean install”时,我从一些uni测试中得到了nullpointerexception。 这是因为其中一个资源为空,但为什么

统一测试:

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中的surefire插件。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>

和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 test的形式运行——执行毫无异常

surefire报告后的堆栈跟踪:


测试集:myapp。服务。impl。MyServiceImplTest

测试运行:1,失败:1,错误:0,跳过:0,运行时间:1.115秒<<&书信电报;失败 我的应用。服务。impl。我的服务测试。someTest()经过的时间:1.068秒<<&书信电报;失败 JAVAlang.NullPointerException 在myapp上。服务。impl。我的服务测试。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
...

共 (1) 个答案

  1. # 1 楼答案

    只是一个想法,但是

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

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

    `