模拟最后一堂课:powermock + Easymock +测试

发布于 2024-11-08 05:23:43 字数 2310 浏览 5 评论 0原文

我是 PowerMock 的新手,找不到符合我情况的示例。

这并不复杂——我正在尝试使用 PowerMock+EasyMock 来模拟 TestNG 测试中的最终类。

这是最终类:

public final class MyFinalClass {
    private String myString = "someString";

    public final String getMyString() {
        return myString;
    }
}

被测试的类委托给最终类的实例:

public class MyActiveClass {
    private MyFinalClass finalClass;

    public String getFinalString() {
        return finalClass.getMyString();
    }

    public void setFinalClass(MyFinalClass finalClass) {
        this.finalClass = finalClass;
    }
}

这是测试类:

import static org.easymock.EasyMock.expect;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.Test;


@PrepareForTest({ MyFinalClass.class })
public class MyTestClass extends PowerMockTestCase {

    private MyActiveClass activeClass = new MyActiveClass();
    private MyFinalClass mockFinalClass;

    @Test
    public void getFinalString() {
        String expectedString = "mockedReturnString";
        mockFinalClass = PowerMock.createMock(MyFinalClass.class);
        activeClass.setFinalClass(mockFinalClass);
        expect(mockFinalClass.getMyString()).andReturn(expectedString);
        PowerMock.replayAll();
        Assert.assertEquals(activeClass.getFinalString(), expectedString);
        PowerMock.verifyAll();
    }
}

但是当我运行测试时,我收到非法参数异常,抱怨 MyFinalClass 不是接口:

FAILED: getFinalString
java.lang.IllegalArgumentException: MyFinalClass is not an interface
    at java.lang.reflect.Proxy.getProxyClass(Unknown Source)
    at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
    at org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:24)
    at org.easymock.internal.MocksControl.createMock(MocksControl.java:51)
    at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212)
    at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2163)
    at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:89)
    at MyTestClass.getFinalString(MyTestClass.java:22)
         ...

我缺少什么?预先感谢您的帮助!

I'm new to PowerMock and can't find an example that matches my situation.

It's nothing complicated-- I am trying to use PowerMock+EasyMock to mock a final class in a TestNG test.

Here's the final class:

public final class MyFinalClass {
    private String myString = "someString";

    public final String getMyString() {
        return myString;
    }
}

The class under test delegates to an instance of the final class:

public class MyActiveClass {
    private MyFinalClass finalClass;

    public String getFinalString() {
        return finalClass.getMyString();
    }

    public void setFinalClass(MyFinalClass finalClass) {
        this.finalClass = finalClass;
    }
}

And here's the Test class:

import static org.easymock.EasyMock.expect;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.Test;


@PrepareForTest({ MyFinalClass.class })
public class MyTestClass extends PowerMockTestCase {

    private MyActiveClass activeClass = new MyActiveClass();
    private MyFinalClass mockFinalClass;

    @Test
    public void getFinalString() {
        String expectedString = "mockedReturnString";
        mockFinalClass = PowerMock.createMock(MyFinalClass.class);
        activeClass.setFinalClass(mockFinalClass);
        expect(mockFinalClass.getMyString()).andReturn(expectedString);
        PowerMock.replayAll();
        Assert.assertEquals(activeClass.getFinalString(), expectedString);
        PowerMock.verifyAll();
    }
}

But when I run the test, I get an illegal argument exception complaining that MyFinalClass is not an interface:

FAILED: getFinalString
java.lang.IllegalArgumentException: MyFinalClass is not an interface
    at java.lang.reflect.Proxy.getProxyClass(Unknown Source)
    at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
    at org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:24)
    at org.easymock.internal.MocksControl.createMock(MocksControl.java:51)
    at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212)
    at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2163)
    at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:89)
    at MyTestClass.getFinalString(MyTestClass.java:22)
         ...

What am I missing? Thanks in advance for your help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2024-11-15 05:23:43

以下是一些选项:

  • MyFinalClass 引入一个新接口并模拟该接口。
  • 如果您没有编辑 MyFinalClass 的权限,则可以使用包装器对象,该对象委托给 MyFinalClass 并在测试中模拟包装器对象。
  • 我不知道是否可以使用“powermock + easymock + testng”直接模拟最终类,但在我看来,可以通过反射更改最终属性。所以也许有更好的解决方案。

Here are some options:

  • Introduce a new interface for MyFinalClass and mock that interface instead.
  • If you don't have permissions to edit MyFinalClass, you could use a wrapper object, which delegates to MyFinalClass and mock the wrapper object in your test.
  • I don't know if it's possible with "powermock + easymock + testng" to mock a final class directly, but in my opinion it's possible to change a final attribute via reflection. So maybe there's a better solution.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文