Believe me, the following annotations are really helpful and they are part of Mockito framework.
1) @InjectMock
2) @Mock
Although there are various other usages of the Mockito Framework, I am going to share my experience with the above two annotations.
My Business Class has the following structure:
Then we can define the behaviour in these test methods and carry out the testing.
Really, these annotations has made life much easier.
For Mocking or Spying with Spock (for Groovy classes), the Mock() and Spy() methods are really awesome.
Spy() is basically used for Partial Mocking.
They come with other features such as wildcard matching (any) of method parameters with _, setting the members of the mocked instance using closures, Verification of method invocation with Groovy Ranges etc.
For groovy classes annotated with @Immutable we could use MockFor class, which is really helpful. I think it is the Groovy Closures which has made these options more powerful. Check out these features!!!!!.
So Till Next Time Keep Code and Keep sharing your Views
1) @InjectMock
2) @Mock
Although there are various other usages of the Mockito Framework, I am going to share my experience with the above two annotations.
My Business Class has the following structure:
class Business
{
@Autowired
private BusinessPlanner businessPlanner;
public String createBusinessPlan() {
String plan = businessPlanner.planBusiness();
return plan;
}
}
& my Test Class has the following structure.
@RunWith(PowerMockRunner.class)
@PrepareForTest(Business.class)
class BusinessTest
{
@InjectMock
private Business business = new Business();
@Mock
private BusinessPlanner businessPlanner;
public void testBusinessFunctionality()
{
<<Define the Behaviour of the Mocked Object i.e. businessPlanner here >>
<<Invoke the Business Method here i.e. business.createBusinessPlan()>>
<<Assert ActualResult with the ExpectedResult>>
}
}
Here in the above Test Class, @Mock annotation injects a mocked instance of BusinessPlanner into the class Business annotated with @InjectMock Then we can define the behaviour in these test methods and carry out the testing.
Really, these annotations has made life much easier.
For Mocking or Spying with Spock (for Groovy classes), the Mock() and Spy() methods are really awesome.
Spy() is basically used for Partial Mocking.
They come with other features such as wildcard matching (any) of method parameters with _, setting the members of the mocked instance using closures, Verification of method invocation with Groovy Ranges etc.
For groovy classes annotated with @Immutable we could use MockFor class, which is really helpful. I think it is the Groovy Closures which has made these options more powerful. Check out these features!!!!!.
So Till Next Time Keep Code and Keep sharing your Views
No comments:
Post a Comment