Reputation: 81
I get NullPointerException when injecting an object using spring for my testng test suite. Here's my .xml configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:hhla="http://www.hhla.de/schema/spring" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="my.package" />
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:test/props/my.properties</value>
</list>
</property>
</bean>
<bean id="my.package.svc.ap.calc.inspektion" class="my.package.entities.CarBean">
<property name="brand" value="${brand}"/>
<property name="model" value="${model}"/>
<property name="fuelType" value="${fuel}"/>
<property name="construction" value="${construction}"/>
<property name="mileage" value="${mileage}"/>
<property name="engineOptionIndex">
<value type="java.lang.Integer">
${engine}
</value>
</property>
<property name="approvalYear" value="${calc.inspektion.year}"/>
<property name="approvalMonth" value="${calc.inspektion.month}"/>
</bean>
</beans>
CarBean class is using private fields with getters and setters:
@Component
public class CarBean{
private String brand;
private String model;
private String fuelType;
private String construction;
private String mileage;
private int engineOptionIndex;
private String approvalYear;
private String approvalMonth;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
....
}
and the testng test class where the bean should be injected:
@ContextConfiguration(locations = { "classpath:META-INF/test-scripts.xml" })
public class SvcApCalcTest extends TestBase {
@Autowired
@Qualifier("my.package.svc.ap.calc.inspektion")
private CarBean inspektionCar;
.....
}
when I refer to the CarBean the NullPointerException is thrown.
Upvotes: 2
Views: 841
Reputation: 8272
Your Test class should be like following
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" })
public class UserServiceTest extends AbstractJUnit4SpringContextTests {
@Autowired
@Qualifier("my.package.svc.ap.calc.inspektion")
private CarBean inspektionCar;
@Test
public void testName() throws Exception {
Assert.assertNotNull(userService);
}
}
Upvotes: 2