Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ';IllegalStateException:前面的方法调用缺少行为定义';即使行为是有定义的_Java_Junit_Easymock - Fatal编程技术网

Java ';IllegalStateException:前面的方法调用缺少行为定义';即使行为是有定义的

Java ';IllegalStateException:前面的方法调用缺少行为定义';即使行为是有定义的,java,junit,easymock,Java,Junit,Easymock,我从easymock和JUnit测试用例中得到了一些令人费解的行为。我收到一个非法状态异常:前面的方法调用缺少行为定义:myCollaborator.getCurrentApplyDate()用法是:expect(a.foo()).andXXX()。我正在使用easymock 3.1模拟JUnit 4测试类中的myCollaborator,该测试类正在测试classUnderTest classUnderTest需要调用两次myCollaborator。只要打一个电话,一切都很好。JUnit测试

我从easymock和JUnit测试用例中得到了一些令人费解的行为。我收到一个
非法状态异常:前面的方法调用缺少行为定义:myCollaborator.getCurrentApplyDate()用法是:expect(a.foo()).andXXX()
。我正在使用easymock 3.1模拟JUnit 4测试类中的
myCollaborator
,该测试类正在测试
classUnderTest

classUnderTest
需要调用两次
myCollaborator
。只要打一个电话,一切都很好。JUnit测试类中的
@Before
设置方法:

@Before
public void setUp() throws Exception {
    mockCollaborator = EasyMock.createMock(MyCollaborator.class);
    classUnderTest = new myObject(mockCollaborator);
    data = new MyDTO();
    // other setup code for data omitted
    EasyMock.expect(mockCollaborator.getCurrentApplyDate()).andReturn(new java.sql.Date(123456789));
    // comment out this expectation for now so it works
    // EasyMock.expect(mockCollaborator.getCurrentBatch()).andReturn("123");
    EasyMock.replay();
}
我正在测试的
classUnderTest.process()
方法是对
myCollaborator
的两个调用,第二个方法被注释掉了,这样它就可以工作了:

public MyDTO process(MyDTO data) throws Exception {
    // do some stuff to data
    java.sql.Date myDate = myCollaborator.getCurrentApplyDate();
    // do some stuff with myDate and data
    // comment out this call for now so it works
    // String currentBatch = myCollaborator.getCurrentBatch();
    // do some other stuff with currentBatch and data
    return data;
}
一旦我从
process()
方法中取消对第二个调用(对
myCollaborator.getCurrentBatch()
的调用)的注释,并取消对JUnit
setUp()
的预期的注释,我就开始接收前面提到的
IllegalStateException

包含那些未注释但不起作用的代码:

@Before
public void setUp() throws Exception {
    mockCollaborator = EasyMock.createMock(MyCollaborator.class);
    classUnderTest = new myObject(mockCollaborator);
    data = new MyDTO();
    // other setup code for data omitted
    EasyMock.expect(mockCollaborator.getCurrentApplyDate()).andReturn(new java.sql.Date(123456789));
    EasyMock.expect(mockCollaborator.getCurrentBatch()).andReturn("123");
    EasyMock.replay();
}

public MyDTO process(MyDTO data) throws Exception {
    // do some stuff to data
    java.sql.Date myDate = myCollaborator.getCurrentApplyDate();
    // do some stuff with myDate and data
    String currentBatch = myCollaborator.getCurrentBatch();
    // do some other stuff with currentBatch and data
    return data;
}
对于这两种方法,
java.sql.Date
String
的返回类型是正确的。这些方法就像它们听起来一样,只是一种吸气剂;它们所做的只是返回实例变量值;在这些getter方法中不会发生其他处理或方法调用

JUnit测试方法:

@Test
public void testSomeFunctionality(){
    // alter data to setup this test case
    try {
        data = classUnderTest.process(data);
    } catch (Exception e) {
        // this is line 531, where the IllegalStateException is being caught
        fail("error msg " + e);
    }
    assertTrue(data.getSomeValue() == expectedValue)
}
完整堆栈跟踪:

java.lang.AssertionError: An unexpected exception has occurred:
java.lang.IllegalStateException: missing behavior definition for the preceding method call:
MyCollaborator.getCurrentApplyDate()
Usage is: expect(a.foo()).andXXX()
at org.junit.Assert.fail(Assert.java:91)
at qualified.package.name.ClassUnderTestTests.testSomeFunctionality(ClassUnderTestTests.java:531)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我过去曾以这种方式广泛使用easymock和JUnit,以前从未遇到过类似的情况。我的同事们也同样受到阻碍,因此,任何人只要能对这里发生的事情有所了解,就可以得到奖励。在你的@Before示例中,你展示了:

EasyMock.replay();
这不应该是:

EasyMock.replay(mockCollaborator);

您的问题不清楚-您已经展示了一个调用
getCurrentApplyDate
的设置方法,但是您还发布了调用
getCurrentApplyDate
getCurrentBatch
的代码(在方法之外),这是哪个?你能发布一个简短但完整的测试类来演示这个问题吗?天哪,你是对的。。。它和第二个电话一起工作吗???我觉得朱尼特过量了。。。我们都在嘲笑自己。。。多谢!它与注释掉的第二个调用一起工作,因为模拟是“录制”的。因此,在第一次调用中,EasyMock认为您将再次调用EasyMock API,以说明前一次调用的行为。当你在mock上调用另一个方法时,EasyMock说“嘿,你没有告诉我最后一个方法调用应该是怎样的”。