Hi, I am creating a simple RecycleView Adapter and trying to test all adapter methods, but onCreateViewHolder was bad for me.
@Override public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_news,parent,false); return new NewsViewHolder(v); }
I'm trying to use mockito to create a viewGroup class layout and return the spy from MockContext when getContext () is a call, but that seems to be because I am returning MockContext LayoutInflater.from (), returning a null pointer exception.
This is my test.
@Test public void testOnCreateViewHolder() throws Exception { ViewGroup vg = mock(ViewGroup.class); MockContext mockContext = new MockContext(); MockContext spyContext = spy(mockContext); when(vg.getContext()).thenReturn(spyContext); NewsViewHolder vh = adapter.onCreateViewHolder(vg, 0); Assert.assertNotNull("Response cant be null",vh); }
Thanks in advance.
android unit-testing tdd
Manuel Alejandro Diaz Serret
source share