测试传入的值......而不仅仅是返回的值

发布于 2024-11-06 20:55:46 字数 557 浏览 0 评论 0原文

所以我有一个看起来像这样的课程。

@Inject
AnotherClass anotherClass;

public class Foo {
    public Boolean someMethod(){
         Holder<Boolean> booleanHolder = new Holder<Boolean>();
         //I have no control over this method, but I need it to set booleanHolder
         anotherClass.anotherMethodCall(booleanHolder, Boolean.TRUE);

         return booleanHolder.value;  
    }
}

我想做的是测试该方法。这看起来几乎是不可能的。我嘲笑了另一个班级。但我只能告诉传入什么变量。或者告诉 anotherMethodCall 应该返回什么。

我想要做的是能够在调用方法后设置 booleanHolder 的值。

有什么想法吗?

So I have a class that looks like this.

@Inject
AnotherClass anotherClass;

public class Foo {
    public Boolean someMethod(){
         Holder<Boolean> booleanHolder = new Holder<Boolean>();
         //I have no control over this method, but I need it to set booleanHolder
         anotherClass.anotherMethodCall(booleanHolder, Boolean.TRUE);

         return booleanHolder.value;  
    }
}

What I am trying to do is test the method. Which seems darn near impossible. I have anotherClass mocked. But I can only tell what variable are passed in. Or tell what anotherMethodCall should return.

What I want to do is be able to set what booleanHolder will be after the method is called.

Any ideas?

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

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

发布评论

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

评论(2

甜宝宝 2024-11-13 20:55:46

Holder 类是否有一个访问器(大概是模板化的),以便您可以在调用后获取它所保存的值,然后对该值执行 assert() ?我假设您有一个“外部”方法的测试版本,该方法使用“按引用”模拟参数调用另一个方法。

Does the Holder class have an accessor (presumably templated) so that you can get the value it holds after the call, then do an assert() against that value? I am assuming that you have a testing version of the "outer" method that calls the other method with the "by reference" emulation parameter.

谜泪 2024-11-13 20:55:46

我的问题实际上是我使用mockito的方式。我指向了错误的值。如果有人好奇这是如何工作的。

public void test_someMethod(){
    Foo myClass = new Foo();
    AnotherClass mockClass = mock(AnotherClass.class);
    final Boolean testBool;
    when(anotherClass.anotherMethodCall((Holder)anyObject(),anyBoolean()).thenAnswer(new Answer(){
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                Holder<Boolean> newHolder = (Holder<Boolean>) args[0];
                newHolder.value = testBool;
            }
        }

     Boolean retBool = myClass.someMethod();

     assertTrue(retBool);
 }

出于显而易见的原因,这并不完全是我正在做的事情。但这是一个非常普遍的案例。

My problem was actually the way I was using mockito. I was pointing to the wrong value. If anyone is curious as to how this works.

public void test_someMethod(){
    Foo myClass = new Foo();
    AnotherClass mockClass = mock(AnotherClass.class);
    final Boolean testBool;
    when(anotherClass.anotherMethodCall((Holder)anyObject(),anyBoolean()).thenAnswer(new Answer(){
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                Holder<Boolean> newHolder = (Holder<Boolean>) args[0];
                newHolder.value = testBool;
            }
        }

     Boolean retBool = myClass.someMethod();

     assertTrue(retBool);
 }

This, for obvious reasons, is not exactly what I am doing. But it is a great generalized case.

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