使用@Autowired在Spring对象注入时抛出NullPointerException

时间:2014-11-06 10:32:44

标签: java spring nullpointerexception autowired spring-test

使用spring为我的testng测试套件注入一个对象时,我得到NullPointerException。这是我的.xml配置:

<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;
}
....
}

和应该注入bean的testng测试类:

@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。

1 个答案:

答案 0 :(得分:2)

您的测试类应该如下

@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);
    }
}

See this link by example