2014-11-06 55 views
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。

+0

它应该因为被注入?你的TestBase类是什么?确保你的hiearchy有一个'@RunWith(SpringJUnitRunner.class)'... – 2014-11-06 10:35:00

+0

已经尝试过了,仍然会抛出NPE – user1481927 2014-11-06 10:42:33

+0

然后你没有尝试或者在你的配置中有其他错误。如果依赖项为null,则自动装配不会发生,并且该类未由“SpringJUnitRunner”运行,或者您忘记包含某些特定的“Te​​stExecutionListener”而中断了其支持。因此,我对你的'TestBase'类的请求,也许你正在尝试运行的测试代码(或者导致'NullPointer'的代码。 – 2014-11-06 10:46:07

回答

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

相关问题