【问题标题】:NullPointerException thrown on Spring object injection with @Autowired使用 @Autowired 进行 Spring 对象注入时引发 NullPointerException
【发布时间】:2014-11-06 10:32:43
【问题描述】:

使用 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。

【问题讨论】:

  • 它应该被注入,因为?你的TestBase 班级是什么?确保您的层级中某处有@RunWith(SpringJUnitRunner.class)...
  • 已经试过了,还是抛出NPE
  • 那你没有尝试或者你的配置有其他问题。如果依赖项为空,则不会发生自动装配,并且该类未由SpringJUnitRunner 运行,或者您因忘记包含某些特定的TestExecutionListeners 而破坏了它的支持。因此,我对您的 TestBase 课程的请求以及您尝试运行的测试代码(或导致 NullPointer 的代码)的请求。

标签: java spring nullpointerexception autowired spring-test


【解决方案1】:

您的测试类应该如下所示

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多