Java 布局充气机模拟问题
我尝试稍后使用layourInflater测试一个方法,但在该行得到一个空指针Java 布局充气机模拟问题,java,android,unit-testing,mockito,robolectric,Java,Android,Unit Testing,Mockito,Robolectric,我尝试稍后使用layourInflater测试一个方法,但在该行得到一个空指针 when(LayoutInflater.from(context)).thenReturn(layoutInflaterMock); 我尝试测试的方法如下所示: publc View method(RoomInfoAdapter.FacilityRoomInfoViewHolder holder) { View linearLayout = LayoutInflater.from(context)
when(LayoutInflater.from(context)).thenReturn(layoutInflaterMock);
我尝试测试的方法如下所示:
publc View method(RoomInfoAdapter.FacilityRoomInfoViewHolder holder) {
View linearLayout = LayoutInflater.from(context).inflate(R.layout.some, holder.getParentLayout(), false);
TextView label = linearLayout.findViewById(R.id.label);
TextView textView = linearLayout.findViewById(R.id.type_value);
....
}
我的测试班:
@RunWith(RobolectricTestRunner.class)
@PrepareForTest({LayoutInflater.class})
@Config(sdk = 23, manifest = "src/main/AndroidManifest.xml")
public class Test {
@Mock
private Context context;
@Mock
private LayoutInflater layoutInflaterMock;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
context = RuntimeEnvironment.application;
....
}
@Test
public void test() {
when(LayoutInflater.from(context)).thenReturn(layoutInflaterMock);
....
}
我试着遵循这个答案:
但它不起作用
编辑:现在我得到:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when()需要的参数必须是“模拟的方法调用”。
例如:
when(mock.getArticles())。然后返回(articles)
当(LayoutInflater.from(context)).thenReturn(…使用mockito和robolectric)时,是否有可能执行
操作?要模拟静态方法或类,必须使用PowerMock,与robolectric一起使用它有点棘手
您可以在这里尝试:
看起来您需要power mockito,然后是mock静态类