java.lang.NoSuchMethodException与JMockit 1.5和2.2 Robolectric
我试图使用JMockit 1.5与2.2 robolectric但我一旦我尝试创建活动得到了java.lang.NoSuchMethodException。我已经减少了测试,以这样的:
I'm trying to use JMockit 1.5 with robolectric 2.2 but I get a java.lang.NoSuchMethodException as soon I try to create an Activity. I've reduced the test to this:
@RunWith(RobolectricTestRunner.class)
//@RunWith(MyTestRunner.class)
public class Test_login {
@Test
public void test_login(@Mocked final Intent in) {
FragmentActivity frag = Robolectric.buildActivity(MainActivity.class).create().get();
}
}
不过,我得到这个堆栈跟踪:
But I get this stack trace:
java.lang.RuntimeException: java.lang.NoSuchMethodException: org.udg.pds.simpleapp_android.roboelectric.Test_login.test_login()
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:201)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NoSuchMethodException: org.udg.pds.simpleapp_android.roboelectric.Test_login.test_login()
at java.lang.Class.getMethod(Class.java:1665)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:199)
... 19 more
我也尝试使用这个亚军:
I've also tried to use this runner:
public class MyTestRunner extends RobolectricTestRunner {
static { Startup.initializeIfPossible(); }
public MyTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
}
不过,这并不有所作为。我曾尝试不同的订单类路径(jmockit,robolectric,JUnit的),没有运气。当我尝试使用@Injectable或@Mocked在测试中我得到这个例外。我已阅读this和这,但它并没有帮助。
这是哪里出了问题的任何提示? THX
Any hint on where is the problem ? Thx
A NoSuchMethodException
发生的原因Robolectric定制的JUnit测试运行器尝试查找一个无参数的测试方法, test_login()
,而不是实际的测试方法,它确实有一个参数。
A NoSuchMethodException
occurs because the Robolectric custom JUnit test runner tries to look up a parameter-less test method, test_login()
, instead of the actual test method which does have a parameter.
一个解决方法是使用模拟字段( @Mocked
, @Injectable
宣布,等在测试一流的水平)止,避免模拟参数。
A workaround is to use mock fields (declared with @Mocked
, @Injectable
, etc. at the test class level) only, avoiding mock parameters.