Java Powermock(使用 Easymock)没有可用的模拟的最后一次调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20123613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:19:42  来源:igfitidea点击:

Powermock (With Easymock) no last call on a mock available

javaunit-testingjuniteasymockpowermock

提问by Cameron Jones

I am trying to just run a simple test case. I have the following method.

我正在尝试运行一个简单的测试用例。我有以下方法。

public static void run(String[] args) throws Throwable {
    CommandLineArguments opts = CommandLineOptionProcessor.getOpts(args);
}

I will continue to build this method / test case as I go. However I just wanted to make sure a simple test case worked first. So I wrote the following test.

我将继续构建此方法/测试用例。然而,我只是想确保一个简单的测试用例首先起作用。所以我写了下面的测试。

@Test
public void testRun() {
    String[] args = {"--arg1", "value", "--arg2", "value2"};

    mockStatic(CommandLineOptionProcessor.class);
    expect(CommandLineOptionProcessor.getOpts(args));

    EasyMock.replay(CommandLineOptionProcessor.class);
}

After that I get the following error:

之后,我收到以下错误:

java.lang.IllegalStateException: no last call on a mock available

I read some of the other posts on StackOverflow but their solution seemed to be that they were using PowerMock with Mockito. I am using Powermock and Easymock, so that should not be the problem.

我阅读了 StackOverflow 上的一些其他帖子,但他们的解决方案似乎是他们将 PowerMock 与 Mockito 一起使用。我正在使用 Powermock 和 Easymock,所以这应该不是问题。

I followed Rene's advice and added the following to the top of my class.

我听从了 Rene 的建议,并将以下内容添加到我的班级顶部。

@PrepareForTest(CommandLineOptionProcessor.class)
@RunWith(PowerMockRunner.class)
public class DataAssemblerTest {

I fixed the previous error. But now I have this error.

我修复了之前的错误。但现在我有这个错误。

java.lang.IllegalArgumentException: Not a mock: java.lang.Class
at org.easymock.internal.ClassExtensionHelper.getControl(ClassExtensionHelper.java:61)
at org.easymock.EasyMock.getControl(EasyMock.java:2172)
at org.easymock.EasyMock.replay(EasyMock.java:2074)
.
.
.

Any ideas on what could be causing this would be great.

关于可能导致这种情况的任何想法都会很棒。

采纳答案by René Link

Did you annotate the test class with @RunWith(PowerMockRunner.class)and @PrepareForTest(CommandLineOptionProcessor.class)?

您是否使用@RunWith(PowerMockRunner.class)和注释测试类@PrepareForTest(CommandLineOptionProcessor.class)

 @RunWith(PowerMockRunner.class)
 @PrepareForTest(CommandLineOptionProcessor.class)
 public class TestClass {

     @Test
     public void testRun(){

You need the @PrepareForTest(CommandLineOptionProcessor.class)at the test class level. See the Powermock doc:

您需要@PrepareForTest(CommandLineOptionProcessor.class)在测试类级别。请参阅Powermock 文档

Use the @PrepareForTest(ClassThatContainsStaticMethod.class) annotation at the class-level of the test case.

在测试用例的类级别使用 @PrepareForTest(ClassThatContainsStaticMethod.class) 注释。

Also ensure that the required libraries are on the test classpath.

还要确保所需的库位于测试类路径上。

In your case the javassistlibrary is missing. Put it on the classpath. Maybe some other libs are also missing... we will see.

在您的情况下,缺少javassist库。把它放在类路径上。也许其他一些库也丢失了……我们会看到的。

If you get

如果你得到

java.lang.IllegalArgumentException: Not a mock: java.lang.Class

then you are using EasyMock.replay(), but you must use PowerMock.replay()

那么你正在使用EasyMock.replay(),但你必须使用PowerMock.replay()

回答by Radu Toader

 EasyMock.expectLastCall() 

or

或者

 EasyMock.expectLastCall().anyTimes() 

or

或者

 EasyMock.expectLastCall().andAnswer(..)

is not present in your code, must be after the method you want to test this is in case your test method is a void method.

不存在于您的代码中,必须在您要测试的方法之后,以防您的测试方法是 void 方法。

otherwise you can use :

否则你可以使用:

expect(CommandLineOptionProcessor.getOpts(args)).andReturn(object);

also please add this to you test class :

也请将此添加到您的测试类中:

  @ObjectFactory
public IObjectFactory getObjectFactory() {

    return new org.powermock.modules.testng.PowerMockObjectFactory( );
}