Reputation: 775
My FixValueConceptIntegration
class has a constructor and it looks like this:
private ReferenceConceptHelper referenceConceptHelper;
private ConceptClientFacade conceptClientExternalFacade;
public FixValueConceptIntegration()
{
referenceConceptHelper = JournalSingletonFactory.getInstance().getSingletonInstance(ReferenceConceptHelper.class);
conceptClientExternalFacade = JournalSingletonFactory.getInstance().getSingletonInstance(ConceptClientFacade.class);
}
So now I'm going to test it using Mockito.
If we have a constructor like
public FixValueConceptIntegration(ReferenceConceptHelper referenceConceptHelper, ConceptClientFacade conceptClientExternalFacade)
{
this.referenceConceptHelper = referenceConceptHelper
this.conceptClientExternalFacade = conceptClientExternalFacade
}
I know it is easy to initialize when we are going to testing the class. Because we can just mock the ReferenceConceptHelper
and ConceptClientFacade
classes.
Then we can use it at the @BeforeMethod
like this:
@BeforeMethod
public void beforeMethod()
{
MockitoAnnotations.initMocks(this);
fixValueConceptIntegration = new FixValueConceptIntegration(referenceConceptHelper, conceptClientExternalFacade);
}
Then all the dependencies will inject to the constructor and no worries. So here the problem is I can't figure out how to inject these dependencies (by mocking) to the above testable class.
Upvotes: 3
Views: 7783
Reputation: 775
I extended my test class with TestNGBase
which extends PowerMockTestCase
.
And then add registerMockSingleton
method to the TestNGBase
class like this;
protected <E, I extends E> void registerMockSingleton(Class<E> typeInterface, I mock)
{
delegate.registerMockSingleton(typeInterface, mock);
}
Then inject mock dependencies to the constructor like this way;
@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;
}
All solved!!! (My testable class constructor doesn't inject dependencies to with constructor arguments.Thats why I solved my problem like this)
Upvotes: 0
Reputation: 19110
Just use the mock
(org.mockito.Mockito.mock
) method for the class and the when
method to mock the method calls:
@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());
}
In this case, you do not need to use the @BeforeMethod
or call MockitoAnnotations.initMocks(this);
. For unit tests, the initMocks
are only useful if you do not have access directly to the class injected (typically when you are using field injection).
But if you would like to use the annotations (I personally don't like), you can do something like that:
@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());
}
Upvotes: 1