Java 使用@Autowired在Spring对象注入上引发的NullPointerException
在使用spring为我的testng测试套件注入对象时,我得到了NullPointerException。以下是我的.xml配置:Java 使用@Autowired在Spring对象注入上引发的NullPointerException,java,spring,nullpointerexception,autowired,spring-test,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
<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>
以及应该注入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);
}
}
应该注射,因为?您的
TestBase
类是什么?确保在您的层级结构中有一个@RunWith(SpringJUnitRunner.class)
已尝试过,但NPE仍然存在,然后您没有尝试或配置中有其他错误。如果依赖项为null,则不会发生自动连接,或者该类没有由SpringJUnitRunner运行,或者您忘记包含一些特定的TestExecutionListener,从而破坏了它的支持。因此,我请求您的TestBase
类,可能是您试图运行的测试的代码(或者导致NullPointer
的代码)。
@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);
}
}