@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
// Using "PrepareOnlyThis" prevents powermock from trying to instrument the whole hierarchy,
// part of which we've ignored (android.os.* in this case)
@PrepareOnlyThisForTest({ServiceCallbackBase.class}) // this class extends Handler,
// so we need PrepareOnlyThis. It also has some final methods we need to verify()
public class ServiceBaseTests {
private class Foo {
// nothing
}
@Rule
public PowerMockRule rule = new PowerMockRule();
private ServiceCallbackBase
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type ServiceCallbackBase$$EnhancerByMockitoWithCGLIB$$9acf906b and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class ServiceCallbackBaseTests {
@Test
public void test_nothing(){
}
private ServiceCallbackBase uutSpy;
@Before
public void setup(){
uutSpy = mock( ServiceCallbackBase.class );
}
}
fonkap..
13
我无法建立你的例子,但我设法写出这个产生非常类似问题的minitest:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareOnlyThisForTest({ServiceCallbackBase.class, Dummy.class})
public class MainActivityTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Test
public void test1() throws Exception {
try {
//This Mockito.withSettings() thing is important to make the test fail!
ServiceCallbackBase callback = PowerMockito.mock( ServiceCallbackBase.class, Mockito.withSettings());
callback.dispatchMessage(null);
Mockito.verify(callback).dispatchMessage(null);
} catch (Exception e){
e.printStackTrace();
Assert.fail();
}
}
}
(注意Mockito.withSettings(),我不知道为什么会让测试失败)
打印:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type ServiceCallbackBase$$EnhancerByMockitoWithCGLIB$$62776c54 and is not a mock!
Make sure you place the parenthesis correctly!
......
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareOnlyThisForTest({ServiceCallbackBase.class, Dummy.class})
public class MainActivityTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Test
public void test1() throws Exception {
try {
//This Mockito.withSettings() thing is important to make the test fail!
ServiceCallbackBase callback = PowerMockito.mock( ServiceCallbackBase.class, Mockito.withSettings());
callback.dispatchMessage(null);
Mockito.verify(callback).dispatchMessage(null);
} catch (Exception e){
e.printStackTrace();
Assert.fail();
}
}
}
(注意Mockito.withSettings(),我不知道为什么会让测试失败)
打印:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type ServiceCallbackBase$$EnhancerByMockitoWithCGLIB$$62776c54 and is not a mock!
Make sure you place the parenthesis correctly!
......