java.lang.reflect.invocationtargetexception 在 Junit 中导致 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26932778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:54:13  来源:igfitidea点击:

java.lang.reflect.invocationtargetexception cause null in Junit

javajunitjunit4

提问by stackUser44

I'm trying to run my junit test case, but it showing as nullpointerexception. when I debug its showing as java.lang.reflect.invocationtargetexception cause null. Please help me.

我正在尝试运行我的 junit 测试用例,但它显示为 nullpointerexception。当我调试它显示为 java.lang.reflect.invocationtargetexception 导致空。请帮我。

public class StudentTest {

公共类学生测试{

StudentService stuService;

学生服务 stuService;

StudentDAO stuDAO;
StudentVO stuVo;

@Before
public void setUp() throws Exception {
    System.out.println("In Setup");
    stuVo=new StudentVO ();
    stuService=new StudentService();
   stuDAO=Mockito.mock(StudentDAO.class);
    //MockitoAnnotations.initMocks(this);
    stuVo.setStudId("123");
    stuVo.setName("user1");
   Mockito.when(stuDAO.getStudent(stuVo.getStuId())).thenReturn(student);

}

@Test
public void getStudent() throws DataAccessException {
    StudentVO stVO=stuService.getStudent(123)   ;
    Assert.assertEquals("123", stVO.getStuId());
}

}

}

My Service Class is

我的服务等级是

public class StudentService {
@Inject
StudentDAO stuDao;
public StudentVo getStudent(String id){
    return stuDao.getStudent(id);
}
public StudentDAO getStuDao() {
    return stuDao;
}
}

In failure trace its just showing as "java.lang.NullPointerException

在失败跟踪中它只是显示为“java.lang.NullPointerException

at com.stu.StudentService.getStudent(StudentService.java:104)
at com.stu.junit.POCJunit.getgetStudent(StudentTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access
@Mock
StudentDAO stuDAO;

@InjectMocks
StudentService stuService;
0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

回答by stackUser44

I resolved it by putting following code

我通过放置以下代码解决了它

 MockitoAnnotations.initMocks(this);

and in the setUp() method I have written

并在我编写的 setUp() 方法中

public class StudentTest {
    @ObjectUnderTest
    StudentService stuService;
    @Inject
    StudentDAO stuDAO;
    StudentVO stuVo;

    @Before
    public void setUp() throws Exception {
        System.out.println("In Setup");
        stuVo=new StudentVO ();   
        stuVo.setStudId("123");
        stuVo.setName("user1");
        Mockito.when(stuDAO.getStudent(stuVo.getStuId())).thenReturn(student);
    }

    @Test
    public void getStudent() throws DataAccessException {
        StudentVO stVO=stuService.getStudent(123)   ;
        Assert.assertEquals("123", stVO.getStuId());
    }
}

回答by RHammonds

There are some issues with your code.

您的代码存在一些问题。

First on your mock for the DAO. You have no variable within the testing class called student. This should probably be stuVO unless you are converting from some other object into the stuVO object. Either way the variable student will need to be defined or replaced in that mock.

首先是你的 DAO 模拟。在名为 student 的测试类中没有变量。这可能应该是 stuVO,除非您要从某个其他对象转换为 stuVO 对象。无论哪种方式,都需要在该模拟中定义或替换变量 student 。

Another thing is that your inputs and outputs do not make sense. You have StudId as "123" in the setter but as an int in the assert. You may be doing a convert in the setter or getter but make sure your types match in the assert.

另一件事是您的输入和输出没有意义。您在 setter 中将 StudId 设为“123”,但在断言中设为 int。您可能正在 setter 或 getter 中进行转换,但请确保您的类型在断言中匹配。

Another thing I just noticed.

我刚刚注意到的另一件事。

You are not setting your mocked DAO inside of the service. This is causing your null pointer because your service is trying to call a method on a null DAO that I'm assuming is autowired in.

您没有在服务内部设置模拟的 DAO。这会导致您的空指针,因为您的服务试图在我假设已自动装配的空 DAO 上调用方法。

Try this

试试这个

##代码##