Mockito和CDI bean注入,@ InjectMocks调用@PostConstruct?

gur*_*het 10 java junit mockito postconstruct cdi

我有这个代码:

class Patient {

  @Inject Syringe syringe;

  @PostConstruct
  void sayThankyouDoc() {

    System.out.println("That hurt like crazy!");

  }

}

@RunWith(MockitoJUnitRunner.class)
class TestCase {

  @Mock
  Syringe siringeMock;

  @InjectMocks
  Patient patient;

  //...

}
Run Code Online (Sandbox Code Playgroud)

我希望Mockito能够调用PostConstruct,但我必须添加:

@Before
public void simulate_post_construct() throws Exception {
    Method postConstruct = Patient.class.getDeclaredMethod("sayThankyouDoc", null);
    postConstruct.setAccessible(true);
    postConstruct.invoke(patient);
}
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

geo*_*and 9

虽然不能直接回答你的问题,但我建议你不要再使用字段注入并使用构造函数注入(使代码更具可读性和可测试性).

您的代码如下所示:

class Patient {

  private final Syringe syringe;

  @Inject
  public Patient(Syringe syringe) {
    System.out.println("That hurt like crazy!");
  }

}
Run Code Online (Sandbox Code Playgroud)

那你的测试就是:

@RunWith(MockitoJUnitRunner.class)
class TestCase {

  @Mock
  Syringe siringeMock;

  Patient patient;

  @Before
  public void setup() {
     patient = new Patient(siringeMock);
  }

}
Run Code Online (Sandbox Code Playgroud)

更新

正如Erik-Karl在评论中所建议的,您可以使用@InjectMocks摆脱设置方法.解决方案的工作,因为会的Mockito使用适当的构造函数注入(如描述在这里).然后代码看起来像:

@RunWith(MockitoJUnitRunner.class)
class TestCase {

  @Mock
  Syringe siringeMock;

  @InjectMocks
  Patient patient;

}
Run Code Online (Sandbox Code Playgroud)

  • 它不仅更易于测试,而且通过将字段设为最终来获得一些线程安全保证.请注意,即使使用上面的代码,您也需要手动调用`@ PostConstruct`方法 (3认同)
  • 可能有点旧但我只想添加:InjectMock将通过构造函数注入(如果可用).所以在这里,您只需要使用InjectMock注释患者,并且可以摆脱设置方法. (2认同)