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);
  }
}

But it does not make a difference. I have tried different orders with the classpath (jmockit, robolectric, junit) with no luck. As soon as I try to use @Injectable or @Mocked in a test I get this exception. I have read this and this but it didn't help.

Any hint on where is the problem ? Thx

有帮助吗?

解决方案

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.

A workaround is to use mock fields (declared with @Mocked, @Injectable, etc. at the test class level) only, avoiding mock parameters.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top