我的FixValueConceptIntegration
类有一个构造函数,它看起来像这样:
private ReferenceConceptHelper referenceConceptHelper;
private ConceptClientFacade conceptClientExternalFacade;
public FixValueConceptIntegration()
{
referenceConceptHelper = JournalSingletonFactory.getInstance().getSingletonInstance(ReferenceConceptHelper.class);
conceptClientExternalFacade = JournalSingletonFactory.getInstance().getSingletonInstance(ConceptClientFacade.class);
}
所以现在我将使用 Mockito 对其进行测试。
如果我们有一个像这样的构造函数
public FixValueConceptIntegration(ReferenceConceptHelper referenceConceptHelper, ConceptClientFacade conceptClientExternalFacade)
{
this.referenceConceptHelper = referenceConceptHelper
this.conceptClientExternalFacade = conceptClientExternalFacade
}
我知道当我们要测试类时很容易初始化。因为我们可以模拟ReferenceConceptHelper
和ConceptClientFacade
类。 然后我们可以像这样在@BeforeMethod
使用它:
@BeforeMethod
public void beforeMethod()
{
MockitoAnnotations.initMocks(this);
fixValueConceptIntegration = new FixValueConceptIntegration(referenceConceptHelper, conceptClientExternalFacade);
}
然后所有的依赖项都将注入到构造函数中,无需担心。 所以这里的问题是我无法弄清楚如何将这些依赖项(通过模拟)注入到上述可测试类中。
只需对类使用mock
(org.mockito.Mockito.mock
) 方法,并使用when
方法来模拟方法调用:
@Test
public void yourTest() {
ReferenceConceptHelper referenceConceptHelper = mock(ReferenceConceptHelper .class);
when(referenceConceptHelper.someMethod(any()).thenReturn("hello");
ConceptClientFacade conceptClientExternalFacade = mock(ConceptClientExternalFacade.class);
when(conceptClientExternalFacade.someMethod(any()).thenReturn("world");
FixValueConceptIntegration integration = new FixValueConceptIntegration(referenceConceptHelper, conceptClientExternalFacade);
assertEquals("hello world", integration.methodThatYouWouldLikeToTest());
}
在这种情况下,您无需使用@BeforeMethod
或调用MockitoAnnotations.initMocks(this);
。对于单元测试,仅当无法直接访问注入的类(通常在使用字段注入时)时,initMocks
才有用。
但是如果你想使用注释(我个人不喜欢),你可以做这样的事情:
@InjectMocks
private FixValueConceptIntegration integration;
@Mock
private ReferenceConceptHelper referenceConceptHelper;
@Mock
private ConceptClientFacade conceptClientFacade;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
when(referenceConceptHelper.someMethod(any()).thenReturn("hello");
when(conceptClientExternalFacade.someMethod(any()).thenReturn("world");
}
@Test
public void yourTest() {
assertEquals("hello world", integration.methodThatYouWouldLikeToTest());
}
我用TestNGBase
扩展了我的测试类,它扩展了PowerMockTestCase
。 然后像这样将registerMockSingleton
方法添加到TestNGBase
类中;
protected <E, I extends E> void registerMockSingleton(Class<E> typeInterface, I mock)
{
delegate.registerMockSingleton(typeInterface, mock);
}
然后像这样向构造函数注入模拟依赖项;
@Override
public void performSetup() {
MockitoAnnotations.initMocks(this);
registerMockSingleton(ReferenceConceptHelper.class,mockReferenceConceptHelper);
registerMockSingleton(ConceptClientFacade.class,mockConceptClientExternalFacade);
fixValueConceptIntegration = new FixValueConceptIntegration();
}
@Override
protected void performTearDown() throws Exception
{
fixValueConceptIntegration = null;
}
都解决了!!(我的可测试类构造函数不会向构造函数注入依赖项 参数。这就是为什么我这样解决了我的问题)