I'd like to do this mocking with Mockito
MyServiceClass
(this isn't the actual code, just a fake example with a similar intent)
public String getClassObjects() {
OtherClassObject otherclass = OtherClassObject.createOtherClassObject();
String id = otherclass.getParentObject().getId();
return id;
}
So essentially I want to mock ".getId()" but only in the context of this class "MyServiceClass" if I call the same method of "getId()" in a different class I want to be able to mock a different return.
This will return "3" in every method call for the OtherClassObject
new MockUp<MyServiceClass>() {
#Mock
public String getId(){
return "3";
}
};
Is there a way to isolate method calls for a class object within the scope of a specific class?
Plain Mockito is unable to mock static calls, so you need PowerMock here. To achieve desired you should return different values from the mocked object like this
// from your example it's not clear the returned type from getParentObject method.
// I'll call it ParentObj during this example. Replace with actual type.
ParentObj poOne = mock(ParentObj.class);
when(poOne.getId()).thenReturn("3");
ParentObj poTwo = mock(ParentObj.class);
when(poTwo.getId()).thenReturn("10");
...
OtherClassObject otherClassObjectMock = mock(OtherClassObject.class);
// return all your stubbed instances in order
when(otherClassObjectMock.getParentObject()).thenReturn(poOne, poTwo);
PowerMockito.mockStatic(OtherClassObject.class);
when(OtherClassObject.createOtherClassObject()).thenReturn(otherClassObjectMock);
Thus, you can customize your mocks per needs, specifying desired return value, or propagating call to actual (real) method.
Don't forget to use annotations #RunWith(PowerMockRunner.class) and #PrepareForTest(OtherClassObject.class) on class level to activate the magic of PowerMock.
An alternative idea is to get rid of static call inside your getClassObjects method and pass factory using constructor, so you can easily mock it, setting mocked object only for single class.
Hope it helps!
Related
I have following Java class:
public class FooServiceImpl {
private BarService barService;
public String generateFoo() {
String barValue = barService.generateBar();
return customFoo() + barValue;
}
public String customFoo() {
return "abc";
}
}
And here is exemplary Spock test method:
def "generate foo bar"() {
setup:
def barService = Mock(BarService) {
generateBar() >> "Bar"
}
FooServiceImpl spyFooService =
Spy(FooServiceImpl, constructorArgs: [[barService: barService]])
spyFooService.customFoo() >> "foo"
when:
def fooValue = spyFooService.generateFoo()
then:
fooValue == "fooBar"
}
I try to create a Spy object for FooServiceImpl class but I get following error:
org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack:
No such property: barService for class:
com.foo.FooServiceImpl$$EnhancerByCGL`
I can't add a constructor to FooServiceImpl or setter for BarService, so I want to use map constructor. Is this possible?
Note: according to this issue it should work
The easiest solution in your case would be to make this field protected instead of private. When you create a spy object from a class, CGLIB is involved and it creates as subclass from the class you are trying to create spy from - com.foo.FooServiceImpl$$EnhancerByCGL in your case. The thing is that the field you are trying to modify is a private field and according to regular subclassing strategy in Java, private field does not get inherited in child class. That is why field barService does not exist in spy object
ATTENTION: IntelliJ's debugger may tell you that barService is present in this spyFromService instance, however this is IDE's bug - if you list all available fields from spyFromService.class.fields or spyFromService.class.declaredFields you wont find barService field here.
Another problem is that when CGLIB gets involved in object creation process it also gets involved if it comes to invoking methods. This is why adding dynamic fields to a class or instance via Groovy's metaprogramming features wont work. Otherwise you would be able to do things like:
spyFromService.metaClass.barService = barService
or
spyFromService.class.metaClass.barService = barService
Alternatively you could get rid of spy object and use a real instance in your test. Then
FooServiceImpl spyFromService = new FooServiceImpl()
spyFromService.#barService = barService
will work. However you won't be able to stub existing customFoo() method and you will have to rely on what its real implementation returns.
public void attestResults(List<OMInvestigationResultMutableDTO.Id> resultIds, OMRequestSpeciality speciality)
{
List<Answer.Id> attestIds = new ArrayList<Answer.Id>();
for (OMInvestigationResultMutableDTO.Id id : resultIds)
{
Answer.Id answerId = answerIdFactory.createId(id.getValue(), null);
attestIds.add(answerId);
}
orderManagementServiceProvider.getOrderManagementService()
.attestResults(attestIds,
RequestSpeciality.valueOf(speciality.toString()));
}
My problem is how to write unit testing for attestResults()..
I want to try mock orderManagementServiceProvider.getOrderManagementService().attestResults() using doNothing() in mockito.
This is the method orderManagementServiceProvider.getOrderManagementService().attestResults,
public List<Id> attestResults(List<Id> answerIds, RequestSpeciality speciality) {
this.accessHandler.checkAccess(AccessRights.ATTEST_RESULTS);
ArgumentValidator.argument(answerIds, "AnswerIds").notNull().notEmpty();
ArgumentValidator.argument(speciality, "Speciality").notNull();
ResultCreator resultCreator = new ResultCreator(this.resultToolkitAdapter, this.pathologyReportToolkitService);
return resultCreator.attestResults(answerIds, speciality);
}
In this case I have not permission to use powerMockito.
I mocked
when(orderManagementServiceProvider.getOrderManagementService()).thenReturn(omService);
The relevant line in your code under test (cut) is this:
orderManagementServiceProvider.getOrderManagementService()
.attestResults(attestIds,
RequestSpeciality.valueOf(speciality.toString()));
Because of the violation of the Law of Demeter (Don't talk to strangers!) you have to mock both: the instance of the OrderManagementService and of the OrderManagementServiceProvider. Then you have to configure the mock of the later to return the mock of the first when getOrderManagementService() is called.
However, doNothing() only applies to void methods on spys (a wrapped concrete instance of the dependency). void methods on mocks are not called anyway.
If your method has a return value you have to use doReturn() (or doThrow()) so that the cut can act on the methods outcome. By default Mockito will return null, 0or false respectively.
Warning:
The form when(dependency.someMethodWithReturnValue()).thenReturn() does call the mocked method (and throws away its result). This may lead to NPEs if the method accesses member variables or objects returned by other not configured methods in the mock.
PowerMockito is used only if you want to mock static classes. By looking at this,
orderManagementServiceProvider.getOrderManagementService()
.attestResults(attestIds,
RequestSpeciality.valueOf(speciality.toString()));
it doesn't look like static class.
orderManagementServiceProvider = mock(OrderManagementServiceProvider.class);
doNothing().when(orderManagementServiceProvider).getOrderManagementService.attestResults(any(),any());
I need to mock following method call carBookBuilder.setTrip(protoConverter.convertTrip(carBookRequest, location)), But When carBookBuilder.setTrip(protoConverter.convertTrip(carBookRequest, location)) is calling i should just return mocking tripdetails and skip protoConverter.convertTrip(carBookRequest, location) method call.
ProtoRequestAdapterTest.java
#RunWith(PowerMockRunner.class)
#PrepareForTest({ProtoRequestAdapter})
class ProtoRequestAdapterTest {
#Test
public void testPopulateCarBookTest() {
CarApiToProtoConverter carApiToProtoConverter;
carApiToProtoConverter = PowerMockito.mock(CarApiToProtoConverter.class);
PowerMockito.when(carApiToProtoConverter.convertTripDetails(carBookRequest, locale)).thenReturn(tripDetails);
}
}
ProtoRequestAdapter.java
class ProtoRequestAdapter {
private CarBookRequest populateCarBook(BookingRequest bookingRequest) {
CarBookRequest newCarBookReq = bookingRequest.getCarBookRequest();
CarBookRequest.Builder carBookBuilder = CarBookRequest.newBuilder();
ProtoConverter protoConverter =
new ProtoConverter(refData, location);
carBookBuilder.setTrip(protoConverter.convertTrip(carBookRequest, location));
return carBookBuilder;
}
}
I see several problems here.
If I understand you correctly, you are trying to test
ProtoRequestAdapter#populateCarBook.
First of all you should make the method public or at least protected, as otherwise your unit test can not call it.
Next the method you are trying to mock is ProtoConverter#convertTrip, which is not private, as otherwise your code would not compile.
More problematic is that you are instantiating it directly in your code, which makes it impossible to replace it with a mock.
I would strongly suggest you to inject it into your class (or at least move the instantiation into a (protected) getProtoConverter()-method, which you can overwrite in your test).
You have to instantiate tripDetails in your test class.
TripDetails tripDetails = new TripDetails();
tripDetails.setLocation = "London";
I have a plain helper class with public methods which I am using in the service level class. When I am writing test for the service class and trying to mock this helper class for one of the method it is going inside the methods and running every line. Since code inside this method is more complex I want to mock helper class with method(s) so that I don't have to take care of every detail inside helper class method.
Service Class
class HistoryServiceImpl implements CaseHistory {
#Override
public List<CaseHistoryDto> getCaseHistory(Individual member, Individual provider) {
MemberUtil memberUtil = new MemberUtil();
List<CaseHistoryDto> caseHistoryDtoList = new ArrayList<CaseHistoryDto>();
List<CaseHistory> caseHistoryList = caseDetailDao.fetchCaseHistory(member.getId(), provider.getId());
for(CaseHistory caseHistory : caseHistoryList) {
CaseHistoryDto caseHistoryDto = new CaseHistoryDto();
caseHistoryDto.setMemberInfo(memberUtil.getMemberInfo(member, caseHistory.getCreateDate()));
caseHistoryDtoList.add(caseHistoryDto);
}
return caseHistoryDtoList;
}
}
Test Class
Class HistoryServiceTest {
#Mock MemberUtil memberUtil;
#InjectMocks private HistoryServiceImpl historyServiceImpl = new HistoryServiceImpl();
#Test
public void testGetCaseHistory() {
//why this line going inside real method and executing all lines?
when(memberUtil.getMemberInfo(any(Individual.class), any(Date.class))).thenReturn(member);
}
}
The reason that your test case is running all the lines in the "real" method, is because your mock object is never being used anywhere.
As written, you cannot mock MemberUtil in your HistoryServiceImpl, because you are manually instantiating it in the getCaseHistory() method. You need to make getCaseHistory() get its MemberUtil from somewhere else, so that you can inject your mock version in your test class.
The simplest solution would be to define your MemberUtil as a member variable, so that the #InjectMocks annotation can override the default value:
class HistoryServiceImpl implements CaseHistory {
MemberUtil memberUtil = new MemberUtil();
#Override
public List<CaseHistoryDto> getCaseHistory(Individual member, Individual provider) {
...
}
}
Alternately you could have HistoryServiceImpl accept an externally provided MemberUtil, either in its constructor or via a setter method. You can then easily pass in a mocked version in your test class.
Generally, utility classes are stateless, so another possible solution would be to convert MemberUtil to make all of its methods static. Then you can use something like PowerMock to mock your static methods.
public class First {
public First(){
}
public String doSecond(){
Second second = new Second();
return second.doJob();
}
}
class Second {
public String doJob(){
return "Do Something";
}
}
Here I want to test the method "doSecond()" of class "First". For the same, I want to mock the method "doJob" of class "Second".
I know that I can create a mocked instance of class "Second" using the code below.
Second sec = mock(Second.class);
when(sec.doJob()).thenReturn("Stubbed Second");
But I cannot relate this mocked instance with class "First" as of the current code.
Without refactoring the source code, is there any way by which i can achieve the requirement.
Please help.
Take a look at powermock's ability to intercept calls to new and return mocks instead
https://code.google.com/p/powermock/wiki/MockConstructor
This doesn't require changing any sourcecode.
here's the test code where we actually return a mock when First.doSecond() calls new Second()
#RunWith(PowerMockRunner.class)
#PrepareForTest(First.class)
public class TestFirst {
#Test
public void mockSecond() throws Exception{
Second mock = PowerMockito.mock(Second.class);
PowerMockito.whenNew(Second.class).withNoArguments().thenReturn(mock);
PowerMockito.when(mock.doSecond()).thenReturn("from mock");
First first = new First();
assertEquals("from mock", first.doSecond());
}
}
It's tricky to mock an instance that you create inside of a method, but it's possible.
Using PowerMock, you can accomplish this with the PowerMock.expectNew() method:
#RunWith(PowerMockRunner.class)
#PrepareForTest(First.class)
public class StackOverflowTest {
#Test
public void testFirst() throws Exception {
Second secondMock = EasyMock.createMock(Second.class);
PowerMock.expectNew(Second.class).andReturn(secondMock);
expect(secondMock.doSecond()).andReturn("Mocked!!!");
PowerMock.replay(secondMock, Second.class);
String actual = new First().doSecond();
PowerMock.verify(secondMock, Second.class);
assertThat(actual, equalTo("Mocked!!!"));
}
}
Effectively, PowerMock is proxying the creation of the new object and substituting whatever value we want when we invoke doSecond().
So, it's possible. However, this is a terrible practice to get into.
One typically wants to mock objects if they involve an outside concern, such as another layer (i.e. database, validation), or if the desired output is coming from other objects that are injected but are safe enough to consider tested.
If your method is capable of getting or retrieving data from a non-injectable source, you should not want to mock that out.
Considering that your method is simple and straightforward, you should really not need to do any mocks here at all. But if you felt that you were forced to, you could do one of a few things:
Create a factory for the creation of Second, and mock the results of the returning factory object with Mockito.
Pass in an instance of Second to that method, and use Mockito as the mock instance.
Declare it as a field (i.e. injected dependency), and use Mockito.
For completeness, here is how the test can be written with the JMockit mocking API, without any refactoring of the original code under test:
public class ExampleTest
{
#Test
public void firstShouldCallSecond(#Mocked final Second secondMock) {
new NonStrictExpectations() {{
secondMock.doJob(); result = "Mocked!!!";
}};
String actual = new First().doSecond();
assertEquals("Mocked!!!", actual);
}
}