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
...
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
`