I have a method:
public class MarginConverter {
int top = 0;
int bottom = 0;
int right = 0;
int left = 0;
public MarginConverter(String val){
top = bottom = right = left = Integer.parseInt(val);
}
public LayoutParams getLayoutParamsFromView(View view){
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
int height, width;
if (layoutParams == null) {
height = LayoutParams.WRAP_CONTENT;
width = LayoutParams.MATCH_PARENT;
layoutParams = new LayoutParams(width, height);
layoutParams.setMargins(left, top, right, bottom);
} else {
layoutParams.setMargins(left, top, right, bottom);
}
return layoutParams;
}
}
Note: Variables left, top, right, bottom are instance variables initialized on constructor call.
I want to test this method. I have mocked view.getLayoutParams() method. Method setMargin(int, int, int,int) is not setting margin parameters as mentioned and I am getting all parameters of LayoutParams(leftMargin, rightMargin, topMargin, bottomMargin) 0.0 in test though it's working correctly in code. What should I do so that I will get values as expected.
Please help me ! Any help will be appreciated..
This is my testCase code:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ViewGroup.LayoutParams.class})
public class MarginConverterTest {
@Mock
private View view = mock(TextView.class);
private int left = 0;
private int right = 0;
private int top = 0;
private int bottom = 0;
@Before
public void setUp() throws Exception {
when(view.getLayoutParams()).thenReturn(null);
}
/**
* tests output for margin conversion when one argument is given
*/
@Test
public void test_OneArgumentMarginConversion(){
left =top = right = bottom = 5;
MarginConverter marginConverter = new marginConverter("5");
LayoutParams params = marginConverter.getLayoutParamsFromView(view);
Object[] marginArray = new Object[4];
marginArray[0] = params.leftMargin;
marginArray[1] = params.topMargin;
marginArray[2] = params.rightMargin;
marginArray[3] = params.bottomMargin;
Assert.assertArrayEquals(new Object[]{left,top,right,bottom}, marginArray);
}
}
You seem to be confused about concept of mocking. If you mock something, it means its a dummy ( not real object ) and all of its method calls are fake.
If you wish margins to be non-zero , LayoutParams(leftMargin, rightMargin, topMargin, bottomMargin)
, don't mock view
or setMargin
method.
If your view
object is not real ( mocked , dummy etc ), your layoutParams
is a fake one too ( means unknown values ) unless you are returning an actual layoutParams
by using some mocking framework.
In simple terms, if you wish to set margins by calling setMargins
, don't mock setMargins
method call and make sure that layoutParams
is not fake one.
Also, in your case if view
is mocked, condition if (layoutParams == null)
will never be satisfied as layoutParams
will be a non - null fake object.
Solution - use an appropriate mocking method on call view.getLayoutParams()
to return an appropriate real layoutParams
object or null
to satisfy both branches of code , if
as well as else
.
Hope it helps and let me know if I misunderstood your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With