Mocking EJB injection in tests

后端 未结 4 1285
盖世英雄少女心
盖世英雄少女心 2021-01-13 03:25

Whenever I want to test a class which uses resource injection I end up including a constructor that will only be used within the test:

public class A {

             


        
4条回答
  •  没有蜡笔的小新
    2021-01-13 04:04

    It's certainly one way to do it, although I'd rely on package access; don't provide a constructor injection point, but simply have your test in the same package as the bean being tested. That way, your test can just access the value directly (assuming it's not private):

    @Test
    public void EJBInjectionTest() {
       A a=new A();
       a.b=new B() {
           // mock functionality here, of course...
       };
       assertNotNull(a.b);
    }
    

提交回复
热议问题