模拟模拟方法的副作用

发布于 2024-11-27 08:32:55 字数 1163 浏览 2 评论 0原文

我在一个类中有一个方法,该方法会对方法的参数产生副作用:

public void SideEffectsClass {
    public void doSomethingWithSideEffects(List<Object> list) {
        // do something to the list
    }
}

并且正在测试该类:

public void ClassUnderTest() {
   public List<Object> execute() {
       List<Object> results = new ArrayList<Object>();
       new SideEffectsClass().doSomethingWithSideEffects(results);
       return results;
   }
}

我使用 JMockit 的测试方法:

@Test
public void test() throws Exception
{
    // arrange
    new Expectations()
    {
        SideEffectsClass sideEffects;
        {
            new SideEffectsClass();
            sideEffects.doSomethingWithSideEffects((List<Object>) any);
            // I want to simulate that the List<Object> parameter has changed 
            // (has more elements, less elements, etc. after this method is called
        }
    };

    // act
    ClassUnderTest testClass = new ClassUnderTest();
    List<Object> results = testClass.execute();

    // assert
    Assert.assertEquals(myExpectedResults, results);
}

I have a method in a class that causes side effects to the method's parameter:

public void SideEffectsClass {
    public void doSomethingWithSideEffects(List<Object> list) {
        // do something to the list
    }
}

And this class being tested:

public void ClassUnderTest() {
   public List<Object> execute() {
       List<Object> results = new ArrayList<Object>();
       new SideEffectsClass().doSomethingWithSideEffects(results);
       return results;
   }
}

My test method using JMockit:

@Test
public void test() throws Exception
{
    // arrange
    new Expectations()
    {
        SideEffectsClass sideEffects;
        {
            new SideEffectsClass();
            sideEffects.doSomethingWithSideEffects((List<Object>) any);
            // I want to simulate that the List<Object> parameter has changed 
            // (has more elements, less elements, etc. after this method is called
        }
    };

    // act
    ClassUnderTest testClass = new ClassUnderTest();
    List<Object> results = testClass.execute();

    // assert
    Assert.assertEquals(myExpectedResults, results);
}

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

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

发布评论

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

评论(2

沧桑㈠ 2024-12-04 08:32:55

您可以使用 Delegate 对象来更改参数值:

    sideEffects.doSomethingWithSideEffects((List<Object>) any);
    result = new Delegate() {
        void delegate(List<Object> list) { list.add(new Object()); }
    };

如果您需要的只是验证是否调用了 doSomethingWithSideEffects 方法,那么测试可以更简单地编写为:

@Test
public void test(final SideEffectsClass sideEffects)
{
    List<Object> results = new ClassUnderTest().execute();

    assertEquals(myExpectedResults, results);

    new Verifications() {{
        sideEffects.doSomethingWithSideEffects((List<Object>) any);
    }};
}

You can use a Delegate object to change an argument value:

    sideEffects.doSomethingWithSideEffects((List<Object>) any);
    result = new Delegate() {
        void delegate(List<Object> list) { list.add(new Object()); }
    };

If all you need is to verify that the doSomethingWithSideEffects methods was called, then the test could be written more simply as:

@Test
public void test(final SideEffectsClass sideEffects)
{
    List<Object> results = new ClassUnderTest().execute();

    assertEquals(myExpectedResults, results);

    new Verifications() {{
        sideEffects.doSomethingWithSideEffects((List<Object>) any);
    }};
}
各自安好 2024-12-04 08:32:55

正如测试试图告诉您的那样,副作用不是 ClassUnderTest 行为的一部分。不要尝试在这里测试它。根据您显示的代码,您应该测试的只是将 results 传递给 doSomethingWithSideEffects() 并且从 execute( 返回相同的对象)。由于不熟悉 JMockit 语法,我无法准确告诉您如何编写它。

旁白:不过,我确实建议每个使用 jMock、JMockit 或 EasyMock 等工具的人都应该使用 Mockito,如果他们能。

As the test is trying to tell you, the side effects aren't part of the behavior of ClassUnderTest. Don't try to test that here. Based on the code you're showing, all you should be testing is that results is passed to doSomethingWithSideEffects() and that the same object is returned from execute(). Being unfamiliar with JMockit syntax, I can't tell you exactly how to write it.

Aside: I do recommend, however, that everyone using a tool like jMock, JMockit, or EasyMock should use Mockito instead if they can.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文