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);
}
}
As I was mocking view, view was not actually created which leads to this problem. I used RobolectricTestRunner instead of PowerMockRunner and created view using shaddow application context. And it worked..!
Now my testcode is:
@RunWith(RobolectricTestRunner.class)
public class MarginConverterTest {
private int left = 0;
private int right = 0;
private int top = 0;
private int bottom = 0;
View view;
@Before
public void setUp() throws Exception {
view = new TextView(Robolectric.getShadowApplication().getApplicationContext());
}
/**
* 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);
}
}