java.lang.NoSuchMethodException พร้อม JMockit 1.5 และ Robolectric 2.2

ฉันกำลังพยายามใช้ JMockit 1.5 กับ robolectric 2.2 แต่ฉันได้รับ java.lang.NoSuchMethodException ทันทีที่ฉันพยายามสร้างกิจกรรม ฉันได้ลดการทดสอบลงเหลือเพียง:

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

แต่ฉันได้รับการติดตามสแต็กนี้:

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

ฉันได้ลองใช้นักวิ่งนี้ด้วย:

public class MyTestRunner extends RobolectricTestRunner {
  static { Startup.initializeIfPossible(); }

  public MyTestRunner(Class<?> testClass) throws InitializationError {
    super(testClass);
  }
}

แต่มันไม่ได้สร้างความแตกต่าง ฉันได้ลองคำสั่งที่แตกต่างกันกับ classpath (jmockit, robolectric, junit) โดยไม่มีโชค ทันทีที่ฉันพยายามใช้ @Injectable หรือ @Mocked ในการทดสอบ ฉันจะได้รับข้อยกเว้นนี้ ฉันได้อ่านสิ่งนี้ และ สิ่งนี้ แต่ก็ไม่ได้ช่วยอะไร

มีคำใบ้ว่าปัญหาอยู่ที่ไหน? ขอบคุณ


person Ignacio Martin    schedule 19.11.2013    source แหล่งที่มา


คำตอบ (1)


A NoSuchMethodException เกิดขึ้นเนื่องจากตัวดำเนินการทดสอบ JUnit แบบกำหนดเองของ Robolectric พยายามค้นหาวิธีทดสอบที่ไม่มีพารามิเตอร์ test_login() แทนที่จะเป็นวิธีทดสอบจริงซึ่งมีพารามิเตอร์

วิธีแก้ปัญหาคือใช้ฟิลด์จำลอง (ประกาศด้วย @Mocked, @Injectable ฯลฯ ที่ระดับคลาสทดสอบ) เท่านั้น โดยหลีกเลี่ยงพารามิเตอร์จำลอง

person Rogério    schedule 20.11.2013
comment
ขอบคุณ มันจะได้ผล!. อย่างไรก็ตาม ฉันสงสัยว่านี่เป็นปัญหา robolectric ที่มักจะละเว้นพารามิเตอร์ในวิธีทดสอบ หรือเป็นปัญหาของการรวมกันของทั้งสองเฟรมเวิร์ก - person Ignacio Martin; 20.11.2013
comment
หลังจากลองสิ่งนี้ ข้อยกเว้นก็หายไป แต่เมื่อฉันพยายามตั้งค่าความคาดหวัง ฉันได้รับข้อผิดพลาดนี้ สถานที่ไม่ถูกต้องในการตรวจสอบความคาดหวัง มีคำใบ้บ้างไหม? - person Ignacio Martin; 20.11.2013