I'm facing an issue regarding spring-test, @Autowired and @Before. I have the following test class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AuditServiceImplTest {
private static final String PETICIONARIO = "peticionario";
private static final String AUDITADO = "auditado";
private static final String AUDITOR = "auditor";
private static final String URL = "url";
@Autowired
private AuditService auditService;
@Before
public void loadAutowiredResources() {
loadAuthService();
loadUserService();
}
private void loadAuthService() {
AuthenticationService authenticationService = mock(AuthenticationService.class);
when(authenticationService.getPeticionario()).thenReturn(PETICIONARIO);
ReflectionTestUtils.setField(auditService, "authenticationService", authenticationService);
}
private void loadUserService() {
UserService userService = mock(UserService.class);
when(userService.getAtributosPerfilado(AUDITADO)).thenReturn(new Perfilado(AUDITADO));
when(userService.getAtributosPerfilado(AUDITOR)).thenReturn(new Perfilado(AUDITOR));
when(userService.getPermisosPeticionario(anyString()))
.thenReturn(new Permisos(anyString()));
ReflectionTestUtils.setField(auditService, "userService", userService);
}
@Test
public void testExitoIsAuthorizedOperationAuditado() {
assertTrue(auditService.isAuthorizedOperation(AUDITADO, URL));
}
@Test
public void testExitoIsAuthorizedOperationAuditadoAuditor() {
assertTrue(auditService.isAuthorizedOperation(AUDITADO, AUDITOR, URL));
}
}
And the AuditServiceImplTest-context.xml
file is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.package.audit" />
When I run the tests with Maven (with eclipse IDE they run fine), in the @Before
method the field auditService
(whose implementation is annotated with @Service
) is initialized for one of the @Test
, but is null
on the other one. Why is this?
This is my pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.package.audit</groupId>
<artifactId>audit</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>windows-1252</project.build.sourceEncoding>
<java.version>1.7</java.version>
<spring.version>3.2.3.RELEASE</spring.version>
<jackson.version>2.5.4</jackson.version>
<!-- Variables de entorno de Maven -->
<cfg.entorno>de</cfg.entorno>
<cfg.cloneId>10</cfg.cloneId>
<cfg.location>C:/Users/user/Workspace/audit/src/test/resources/</cfg.location>
<log.location>C:/Users/user/Workspace/audit/</log.location>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aspects</artifactId>
<version>0.21</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>ISO-8859-1</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>junit</id>
<phase>verify</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<parallel>both</parallel>
<threadCount>10</threadCount>
<systemPropertyVariables>
<ENTORNO>${cfg.entorno}</ENTORNO>
<cloneId>${cfg.cloneId}</cloneId>
<CFG_LOCATION>${cfg.location}</CFG_LOCATION>
<LOG_LOCATION>${log.location}</LOG_LOCATION>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.12</version>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>com.jcabi</groupId>
<artifactId>
jcabi-maven-plugin
</artifactId>
<versionRange>
[0.12,)
</versionRange>
<goals>
<goal>ajc</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
And the stack trace:
<testcase name="testExitoIsAuthorizedOperationAuditadoAuditor" classname="com.package.audit.service.impl.AuditServiceImplTest" time="37.355">
<error message="Target object must not be null" type="java.lang.IllegalArgumentException">java.lang.IllegalArgumentException: Target object must not be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:105)
at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:84)
at com.grupobbva.package.service.impl.AuditServiceImplTest.loadAuthService(AuditServiceImplTest.java:43)
at com.grupobbva.package.service.impl.AuditServiceImplTest.loadAutowiredResources(AuditServiceImplTest.java:36)