gpt4 book ai didi

java - 使用@Autowired在Spring对象注入(inject)时抛出NullPointerException

转载 作者:行者123 更新时间:2023-11-30 09:00:29 25 4
gpt4 key购买 nike

使用 spring 为我的 testng 测试套件注入(inject)对象时,我得到 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 的私有(private)字段:

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

以及应该注入(inject) 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。

最佳答案

你的测试类应该如下所示

@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

关于java - 使用@Autowired在Spring对象注入(inject)时抛出NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26777207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com