I have the following scenario.
A class MyClass in which I have an API myAPI() whose contents are as follows:
class MyClass {
public void myAPI() {
...
MyOtherClass myOtherObj = new MyOtherClass();
String value = myOtherObj.decodeAndGetName();
...
}
}
Here we have MyOtherClass which contains an API decodeAndGetName() which does some operation. It is in a different package and I can't modify its code.
Requirement
I need to write a junit test for the above myAPI(). Now I want to somehow mock the object of MyOtherClass and mock the return value of decodeAndGetName().
I am not able to do this, as we have a new MyOtherClass() and as soon as the flow comes to this line, it creates a new instance and goes to the decodeAndGetName() API.
What I need is, some way to prevent the flow going to decodeAndGetName() and take a mock value instead when this call is encountered in the above code.
Please let me know a way to do this.
I've only used it with Android code, but I think you may be able to make use of something like Mockito to mock the MyOtherClass in your tests with code similar to:
MyOtherClass mockMyOtherClass = Mockito.mock(MyOtherClass.class);
when(mockMyOtherClass.decodeAndGetName()).thenReturn(new String("known return value");
I would also suggest using dependency injection and make use of something like Guice in order to accomplish this. I use the combination of Guice & Mockito on a daily basis with my Android projects to successfully accomplish exactly this sort of thing.
Brief Example
Here is what your code may look like after setting up dependency injection with Guice:
MyOtherClassWrapper.java
#Singleton
public class MyOtherClassWrapper {
private MyOtherClass myOtherClass = new MyOtherClass();
public String decodeAndGetName() {
return getMyOtherClass().decodeAndGetName();
}
...
private MyOtherClass getMyOtherClass() {
return myOtherClass;
}
}
MyClass.java
class MyClass {
...
#Inject private MyOtherClassWrapper myOtherClassWrapper;
...
public void myAPI() {
...
String value = getMyOtherClassWrapper().decodeAndGetName();
...
}
private MyOtherClass getMyOtherClassWrapper() {
return myOtherClassWrapper;
}
}
Please see the Guice User's Guide for info on how to get started setting up Guice. It's not too difficult.
Related
Is there a way to use ReflectionTestUtils to set the return value of a method inside a field that represents a class to a mocked value instead of mocking the entire field? I was trying .setField(), but that only seems to work for the entire field. If there isn't, what would be a good substitute? Example of what I mean below:
public class Example() {
private ClassField field;
public methodThatUsesField() {
methodReturnType type = field.method(); // I was trying to call .setField() to change the field's method to a mocked value, but can't figure out how to do it
...
}
}
The class that is called in the field is very complicated, but there is a very simple public method that acts as the root of the class, and I want to set that to a specific value. The class itself does not have a constructor, so I need a way to get around that.
This is a spring boot project written in Java, and I need to use ReflectionTestUtils to be able to pass an argument into Mockito mock
You can use Mockito.spy(field) and inject spied field using ReflectionTestUtils. Something like this
#SpringBootTest
class ExampleTest {
#Autowired
private Example example;
#Autowired
private ClassField field;
#Test
void testMethodThatUsesField() {
ClassField spiedField = Mockito.spy(field);
Mockito.when(spiedField.method()).thenReturn(methodReturnTypeValue);
ReflectionTestUtils.setField(example, "field", spiedField);
example.methodThatUsesField();
// assertions
}
}
I have a class like below:
public class ClassOne {
public function1(input, output, context) {
//some implementation
private function2(List, String, String);
}
private void function2(List, String, String){
Class2 class2 = new Class2();
String value = class2.method1(string, string);
}
}
public Class2 {
public String method2(string, string) {
//some implementation
return string;
}
}
I am writing Unit test for ClassOne using Mockito and PowerMockito and would like to mock the call to class2 and do not want to actually call method body for method2. How can I achieve this?
I tried Mockito.mock and PowerMockito.spy the class and when(class2.method2).thenReturn() and doReturn().when(class2).method2(); but everything calls the method body when I do classOne.function1. I have spied the ClassOne.
It would be really helpful if you would have also provided your non working Unit Tests. On the other hand I'm pretty sure the problem isn't there anyway :)
Your problem is not that Mockito & PowerMockito are not working.
The real problem is in the dependency of you classes. Or to be more specific the way your classes handle this dependency.
In General it is not a good idea to instantiate the dependency (Class2) in the place it is needed (ClassOne). As you can see right now it makes testing pretty hard.
It would be better to pass the dependency into the class that needs it.
This is called Dependency Injection (DI).
In your example you would pass an object of Class2 into the constructor of ClassOne. The Code would look something like that:
public class ClassOne {
private final Class2 class2;
public ClassOne(Class2 class2) {
this.class2 = class2;
}
...
private void function2(List, String, String){
String value = class2.method1(string, string);
}
}
As you can see you simply pass an instance of your dependency and use this one instead of creating it on your own.
In your Unit Test you are now able to pass a Mock of Class2 into your Class1 object which will then be used.
I have just started learning JUnit very recently and came across the following problem.
Have a look at the following class
class MyClass {
String a;
public MyClass(String a) {
this.a=a;
String doSomething(String a) {
if( a.isEmpty() )
return "isEmpty";
else
return"isNotEmpty";
}
I want to test the above method for both the conditions. If I proceed with the general structure of writing testcases it will look something like this:
class MyClassTest {
MyClass myClass;
#BeforeEach
void setUp() {
myClass=new MyClass("sampleString");
}
#Test
void doSomethingTest() {
Assertions.equal("isNotEmpty", myClass.doSomething());
}
}
However, for testing the empty string condition I will need another setup method where instead of "sampleString" I pass an empty string.
Following are the approaches I could think of and the questions for each:
Not use setUp at all and instead initialize the class in the individual test method. However, if let's say there are 10 testcases; 5 of which require empty string and rest "sampleString" then this doesn't make sense. Again, we can have a separate method for this repetitive code and call it individually in each testcase but then that defeats the purpose of having a steup method. Lets say I wanted to use two different setup methods, is there a way to do so?
Have a parameterized setup. I don't know if this is possible though. If yes, please share some useful links for this.
Use TestFactory. I tried reading up about this, but couldn't find an example for this specific case. If you have any, please share.
This example has been kept simple for illustrative purposes.
Group the tests with the same setup in an inner class annotated with #Nested. Each nested test class can have its own setup in a local #BeforeEach method.
You can always prepare the non-common data inside your test method. I've always thought it's easier this way, compared to using parameterized tests. You can't mix parameterized and non-parameterized tests in 1 file.
#Test
void doSomething_nullString()
{
myClass = new MyClass(null);
Assert.assertNull(myClass.doSomething());
}
#Test
void doSomething_emptyString()
{
myClass = new MyClass("");
Assert.assertEquals("", myClass.doSomething());
}
#Test
void doSomething_nonEmptyString()
{
myClass = new MyClass("sampleString");
Assert.assertEquals("sampleString", myClass.doSomething());
}
Or, you can always have helper methods inside the test class.
private MyClass createTestObject_nonNullString() {
return new MyClass("nonNullString");
}
private MyClass createTestObject_nullString() {
return new MyClass(null);
}
#Test
public void doSomething_sample() {
MyClass test = createTestObject_nonNullString();
// perform test
}
I am using mockito as mocking framework. I have a scenerio here, my when(abc.method()).thenReturn(value) does not return value, instead it returns null.
public class DQExecWorkflowServiceImplTest {
#InjectMocks
DQExecWorkflowServiceImpl dqExecWorkflowServiceImpl = new DQExecWorkflowServiceImpl();
#Mock
private DQUtility dqUtility;
#Mock
private DqExec dqExec;
#Mock
private DqCntlDefn dqCntlDefn;
#Mock
private DqCntlWfDefn dqCntlWfDefn;
#Mock
private DqCntlWfDefnTyp dqCntlWfDefnTyp;
#Mock
private IDQControlWfDefTypeService controlWfDefTypeService;
#Before
public void setUp() throws Exception {
dqExec = new DqExec();
dqCntlWfDefn = new DqCntlWfDefn();
dqUtility = new DQUtility();
dqCntlWfDefnTyp = new DqCntlWfDefnTyp();
dqCntlWfDefnTyp.setDqCntlWfDefnTypCd("MIN_INCLUSIVE_VAL");
dqExecWorkflowServiceImpl
.setControlWfDefTypeService(controlWfDefTypeService);
}
#Test
public void testExecuteWorkflow() {
when(controlWfDefTypeService.getDqCntlWfDefnTypCd(dqCntlWfDefn))
.thenReturn(dqCntlWfDefnTyp);
dqExecWorkflowServiceImpl.executeWorkflow(dqExec, dqCntlWfDefn);
}
}
Java class
#Override
public DqCntlWfExec executeWorkflow(final DqExec dqExec,
final DqCntlWfDefn dqCntlWfDefn) {
final DqCntlWfExec dqCntlWfExec = new DqCntlWfExec();
dqCntlWfExec.setDqCntlWfExecEffDt(dqUtil.getDefaultEffectiveDt());
dqCntlWfExec.setDqCntlWfExecExpDt(dqUtil.getDefaultExpiryDt());
dqCntlWfExec.setDqCntlWfDefn(dqCntlWfDefn);
dqCntlWfExec.setDqExec(dqExec);
final DqCntlWfDefnTyp dqCntlWfDefnTyp = controlWfDefTypeService
.getDqCntlWfDefnTypCd(dqCntlWfDefn);
String workflowType = null;
if(null!=dqCntlWfDefnTyp){
workflowType = dqCntlWfDefnTyp.getDqCntlWfDefnTypCd();
}
When ever i run the test file the when is not working and i am using mockito1.8.5 jar in the buildpath. The service call is being mocked but returns the null value.
final DqCntlWfDefnTyp dqCntlWfDefnTyp = controlWfDefTypeService
.getDqCntlWfDefnTypCd(dqCntlWfDefn);
This object dqCntlWfDefnTyp is null
I have done this before and there was no problem with the when, It seems to be working with files i have done before. I had followed the same procedure for the test file but i couldnt figure out the issue. Can anyone please assist me
Thanks to all the folks in advance
Mockito mock works when we mock the objects loosely.
Here is the change i have made to make it work:
when(controlWfDefTypeService.getDqCntlWfDefnTypCd(any(DqCntlWfDefn.class))
.thenReturn(dqCntlWfDefnTyp);
Instead of passing the object of the Mock class, I passed the class with the Matcher any() and it works.
TL;DR If some arguments in your test are null, be sure to mock the parameter call with isNull() instead of any(SomeClass.class).
Explanation
This might not be the answer that helps OP, but might be useful for someone else. In my case the setup was all good, however, some mocks returned the desired thenReturn(...) value and some didn't.
It's important to understand, that the method call you're trying to mock (i.e. the method in when(someMock.methodToMock)) has to match the actual call and not the signature only.
In my case, I mocked a method with a signature:
public void SomeValue method(String string, SomeParam param)
The call however, in the test was something like:
method("some string during test", null);
Now if you mock the call with:
when(MockedClass.method(anyString(), any(SomeParam.class))
Mockito will not match it even though the signature is correct. The problem is that Mockito is looking for a call of method() with the arguments String and SomeParam, whereas the actual call was with a String and null. What you have to do is:
when(MockedClass.method(anyString(), isNull())
Hint
Since there are many isNull() implementations in different frameworks, be sure to use this one org.mockito.ArgumentMatchers.isNull.
I think I have found your issue, but not all the credit goes to me.
Since you are trying to mock 'dqCntlWfDefnTyp' in your test class and the object itself is being instantiated in the class that you are trying to test, you inevitably run into some issues. The primary problem is that the object cannot be mocked because it is being recreated in during the test.
There are a few options, but the best choice in my humble opinion is using PowerMockito. You will be able to replace the object within the class that is being tested with the one you mock.
An excellent example of this usage of PowerMockito from #raspacorp on this question:
public class MyClass {
void method1{
MyObject obj1=new MyObject();
obj1.method1();
}
}
And the test class...
#RunWith(PowerMockRunner.class)
#PrepareForTest(MyClass.class)
public class MyClassTest {
#Test
public void testMethod1() {
MyObject myObjectMock = mock(MyObject.class);
when(myObjectMock.method1()).thenReturn(<whatever you want to return>);
PowerMockito.whenNew(MyObject.class).withNoArguments().thenReturn(myObjectMock);
MyClass objectTested = new MyClass();
objectTested.method1();
... // your assertions or verification here
}
}
I had the same problem. The solution for me was to put the Mockito.when(...).thenReturn(...); into the #Before-SetUp method.
Just like Younes EO said: it usually is related to null arguments being passed to mocked functions.
One thing worth adding is that if you're using Kotlin for your tests (and you have mockito-kotlin in your project), for Nullable arguments you should usually use anyOrNull() instead of any().
Relevant to the topic, just in case this helps someone. My mockito.when wasn't working when asserting anyString() reason being the String within the tested class was an injected value from a properties file. The following line set the injected value in the unit test:
ReflectionTestUtils.setField(bean, "theVariable", "theValue");
I haved the same problem when i make test for controler. Use #MockBean instead #Mock
I have a class that has a private class variable initialized like
public class MyClass{
private BusinessObject businessObject = BusinessObjectGenerator.getBusinessObject();
public MyClass(){
}
public Object myMethodToTest(){
return businessObject.getObject();
}
}
Now, I'm trying to unit test myMethodToTest I want to send in a mock object in place of businessObject. I use mockito for mocking and use spy(new MyClass()) for partial mocking but having trouble with mocking the call to get businessObject.
1. Is it possible to mock the call to the businessObject? If so how?
2. How can I refactor this code to help while writing unit test. Any resources pointing towards this would be of great help.
Thanks!
To properly refactor this code you'd:
private BusinessObject businessObject;
public void setBusinessObject(BusinessObject instance) {
businessObject = instance;
}
private BusinessObject getBusinessObject() {
if (businessObject == null) {
// represents existing implementation in original code sample
businessObject = BusinessObjectGenerator.getBusinessObject();
}
return businessObject;
}
/* rest of your code */
Now you can inject your mock into the class yourself at the test site.
I'd recommend doing this using dependency injection with a framework like Guice. It will be worth your time.