Reputation: 29
I have the following code:
public class ABC {
private double a;
Other_object other_object;
public ABC(int a, int[] b){
this.a=a;
other_object=new Other_object(b);
}
...
public int method_1(){
return other_object.other_method();
}
public int method_2(){
if(method_1()>0 && a>0)
return 1;
else
return 0;
}
}
I've created the following mock object:
ABC a=mock(ABC.class);
Mockito.when(a.method1()).thenReturn(0);
I cannot set a value for "int a". I tried to create a setter function in ABC, and mocking it, but it doesn't work. I would like to know why mocking the setter function doesn't work how can I mock this value (int a)?
Upvotes: 2
Views: 17493
Reputation: 1669
Hope below code useful for you
@Mock
SampleClass sampleClass;
ReflectionTestUtils.setField(sampleClass, "variableName","ISO-8859-1");
Upvotes: 1
Reputation: 10652
A mock is an object that "pretends" to do something. So the important thing about a mock is its behavior, not its state (because it doesn't really have much of a state). Of course you COULD let the mock react to a setter, store the value and then return it via the getter, for example via an Answer (I will not provide code for that), but I can almost guarantee that this will NOT be a good solution for whatever problem you are having.
Mocking is for when you don't actually need a real object, but want to test another object that depends on it. So your mock object is just a way to test something else.
For example, let's assume you are testing your class MyClass
. Is has a method like this...
public int doSomething(ABC abc) {
return abc.method_1 * 2;
}
If you do not want to use a real "ABC" object for whatever reason, you can mock it. Your test of your MyClass would look like this (condensed)...
@Test
public void doSomething_must_return_2_when_method_1_returns_1() {
ABC abcMock = Mockito.mock(ABC.class);
when(abcMock.method_1()).thenReturn(1);
Assert.assertEquals(2, myClass.doSomething(abcMock) );
}
If you actually need to store some value in the mock, then you either don't need a mock or your test case is far to complex. Remember: A test should test ONE thing. It should only have ONE point of failure. If you have multiple test cases, use multiple methods, not one. If your object needs to do more than basic "call method, give result" a mock might not be the correct tool.
But in your example, your a is constant for any instance of ABC. You initialize it in the constructor. So for any mock you create, your a is also constant. So, if you needed to mock method_2, then you also already know the result... No need to set a here anywhere. If you want to mock ABC, you already know a and thus know what the methods would return on a real ABC. No need for the mock to actually store a and actually work with it.
Edit: IF you want to test method_2(), then what you want to test is class ABC itself. It makes absolutely no sense to create a mock of class ABC then, because all you would test would be this "fake" ABC, but not the real one, so you would end up as clever as before, with no knowledge gained about ABC. So, if you want to test your ABC.method_2(), then create an actual instance of ABC...
ABC abcToTest = new ABC(...);
Assert.assertEquals(..., abcToTest.method_2());
No need (or chance) for mocking here.
Upvotes: 2