简体   繁体   English

如何为CDI编写Junit测试用例?

[英]How to write Junit test cases for CDI?

I want to write test cases for cdi. 我想为cdi编写测试用例。 I used @inject in my dao. 我在我的dao中使用了@inject。 Can any one help me how to write test cases for cdi.I tried the below code. 任何人都可以帮我如何为cdi编写测试用例。我尝试了下面的代码。 But its not working. 但它不起作用。 Please help me. 请帮我。

public class StudentTest {

    StudentService 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);

        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);

    }

}

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$1.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)...

I resolved it by putting following code 我通过下面的代码解决了它

@Mock
StudentDAO stuDAO;

@InjectMocks
StudentService stuService;

And in setUp() method I have written

MockitoAnnotations.initMocks(this);

You are never setting the dao mock in your service bean. 您永远不会在服务bean中设置dao mock。 No injection framework is present, so the dao just remains "null" and the test fails. 没有注入框架,因此dao保持“空”并且测试失败。 You could either set the dao mock directly, since you made the field package visible, or use reflection. 你可以直接设置dao mock,因为你可以看到字段包,或者使用反射。

My preferred way: Have a look at http://www.needle4j.org/ . 我的首选方式:查看http://www.needle4j.org/ Its a container agnostic test framework aiming to simplify testing of DI. 它是一个容器不可知的测试框架,旨在简化DI的测试。

Basic rule: whenever you inject something, needle4j will create a mock instance for you. 基本规则:每当你注入一些东西时,needle4j都会为你创建一个模拟实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM