对象实例化时的java.lang.NullPointerException

对象实例化时的java.lang.NullPointerException,java,spring,unit-testing,struts,Java,Spring,Unit Testing,Struts,我的应用程序使用struts和spring框架。我有一个类FormA,其中有一个自动连接属性。当我试图在编写单元测试时实例化它时,我得到一个空指针异常。这是我的密码 我的班级A: public class FormA{ private String propertyOne; @Autowired private ServiceClass service; public FormA(){ } } 我的单元测试方法: @Test public void t

我的应用程序使用struts和spring框架。我有一个类
FormA
,其中有一个自动连接属性。当我试图在编写单元测试时实例化它时,我得到一个空指针异常。这是我的密码

我的班级A:

public class FormA{
    private String propertyOne;
    @Autowired
    private ServiceClass service;

    public FormA(){
    }
}
我的单元测试方法:

@Test
public void testFormA(){
FormA classObj = new FormA();    
}

@Autowired
仅在对象生命周期由Spring管理时起作用


您需要使用
@RunWith(SpringJUnit4ClassRunner.class)
运行测试,而不是手动实例化
FormA
,也可以将其注入测试类中,在通过新建创建对象时使用
@Autowired

,autowire\inject不起作用

作为解决方法,您可以尝试以下方法:

创建NotesPanel的模板bean

<bean id="notesPanel" class="..." scope="prototype">
    <!-- collaborators and configuration for this bean go here -->
</bean>
PROTOTYPE:这将单个bean定义的范围限定为具有任意数量的对象实例

无论如何,单元测试应该是

测试类

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" })
public class UserServiceTest extends AbstractJUnit4SpringContextTests {

    @Autowired
    private UserService userService;

    @Test
    public void testName() throws Exception {
        List<UserEntity> userEntities = userService.getAllUsers();

        Assert.assertNotNull(userEntities);
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={“classpath:META-INF/your spring context.xml”})
公共类UserServiceTest扩展了AbstractJUnit4SpringContextTests{
@自动连线
私人用户服务;
@试验
public void testName()引发异常{
List userEntities=userService.getAllUsers();
Assert.assertNotNull(用户实体);
}
}
您的spring context.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="userService" class="java.package.UserServiceImpl"/>

</beans>


您是否在ServiceClass中放置了服务或组件批注?是的,我放置了服务批注。@user2191903您是否解决了此问题?如果您已经解决了它,您是如何做到的?我们可以使用Spring自动连接一个生命周期不由Spring管理的对象吗?不。您需要使用
@Service
@Component
注释(或使用xml配置)向Spring注册它,我不知道Struts是如何与Spring一起工作的,但我希望这些操作以某种方式注册,由Spring管理,这就是为什么您能够自动连接它们。我刚才是说不能让Spring自动连接上下文之外的对象。@RomanC请解释一下这句话。顺便说一句,你根本没有回答OP的问题。想详细说明一下,这到底是怎么发生的吗?不起作用?你能读懂下面的帖子吗?当你用这种方式创建一个对象时
FormA classObj=new FormA()该类中的所有@Autowired都不会被注入。我们不能这样创建一个对象吗?如果您想在@Autowired中使用,我建议从上下文中检索该bean作为原型bean。该bean不在上下文中,是否要求该bean在上下文中?
<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="userService" class="java.package.UserServiceImpl"/>

</beans>