2
使用spring爲我的testng測試套件注入對象時,發生NullPointerException。這裏是我的.xml配置:使用@Autowired在Spring對象注入時拋出的NullPointerException異常
<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類是使用私有字段的getter和setter方法:
@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;
}
....
}
和TestNG的測試類,其中bean應注射:
@ContextConfiguration(locations = { "classpath:META-INF/test-scripts.xml" })
public class SvcApCalcTest extends TestBase {
@Autowired
@Qualifier("my.package.svc.ap.calc.inspektion")
private CarBean inspektionCar;
.....
}
當我引用CarBean時拋出了NullPointerException。
它應該因爲被注入?你的TestBase類是什麼?確保你的hiearchy有一個'@RunWith(SpringJUnitRunner.class)'... – 2014-11-06 10:35:00
已經嘗試過了,仍然會拋出NPE – user1481927 2014-11-06 10:42:33
然後你沒有嘗試或者在你的配置中有其他錯誤。如果依賴項爲null,則自動裝配不會發生,並且該類未由「SpringJUnitRunner」運行,或者您忘記包含某些特定的「TestExecutionListener」而中斷了其支持。因此,我對你的'TestBase'類的請求,也許你正在嘗試運行的測試代碼(或者導致'NullPointer'的代碼。 – 2014-11-06 10:46:07