Add*_*dev 6 java android mockito powermockito
我正在尝试第一次使用PowerMockito模拟类构造函数,但是它不起作用。我当前的代码是:
public class Bar {
public String getText() {
return "Fail";
}
}
public class Foo {
public String getValue(){
Bar bar= new Bar();
return bar.getText();
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(Bar.class)
public class FooTest {
private Foo foo;
@Mock
private Bar mockBar;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
PowerMockito.whenNew(Bar.class).withNoArguments().thenReturn(mockBar);
foo= new Foo();
}
@Test
public void testGetValue() throws Exception {
when(mockBar.getText()).thenReturn("Success");
assertEquals("Success",foo.getValue());
}
}
Run Code Online (Sandbox Code Playgroud)
测试失败,因为返回的值为“ Fail”。我的问题在哪里?
Okey,找到了答案,您需要致电
@PrepareForTest(Foo.class)
Run Code Online (Sandbox Code Playgroud)
代替
@PrepareForTest(Bar.class)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7949 次 |
| 最近记录: |