[英]Verifying a method called downstream in Mockito (through private void overloaded method)
I'm having trouble figuring out how to test if my method foo(int length, String name)
is being called.我无法弄清楚如何测试我的方法foo(int length, String name)
是否被调用。
My complications:我的并发症:
foo
method either once if it's over, or none if it's under.我正在尝试测试超过 5 和低于 5 的客户长度,以确保它在结束时命中私有foo
方法一次,或者在它不足时不命中。 Either way, it will be hitting public foo(Customer)
once.无论哪种方式,它都会访问 public foo(Customer)
一次。 public class Foo { public void foo(Customer customer) { if (customer.length() > 5) { foo(customer.length(), customer.name()); } else { return } } //Private overloaded method private void foo(int length, String name) { bar.bar(); } } public class Bar { public Order bar(int length, String name) { barProcessing(...) } private Order barProcessing(...) {...calling another Bar method } }
What I've tried so far:到目前为止我已经尝试过:
Mocking Foo and Bar classes plus doing a bunch of when()
statements, then doing... Mocking Foo 和 Bar 类加上做一堆when()
语句,然后做...
Pretty stumped here, with the addition of my complications.加上我的并发症,我在这里很困惑。 I'm still new to Mockito, open to any new suggestions.我还是 Mockito 的新手,愿意接受任何新建议。
bar
and confirming that it's used somewhere by foo(Customer)
.您在 mocking bar
的正确轨道上并确认foo(Customer)
在某处使用了它。 You said you've "tried" mocking it, but not how you actually did that.你说你已经“尝试”了 mocking 它,但不是你实际上是如何做到的。 Your #2 and #6 should generally work;您的#2 和#6 通常应该可以工作; actually debug your code and check whether bar.bar()
is really being invoked as well as whether your mock is being injected into your Foo
.实际上调试你的代码并检查bar.bar()
是否真的被调用以及你的模拟是否被注入到你的Foo
中。After some clarification, it seems you misunderstand when to use mocks in testing.经过一番澄清后,您似乎误解了何时在测试中使用模拟。 You use mocks specifically for the items that you are not testing;您专门针对未测试的项目使用模拟; if you're testing Foo
but then mock it, you're not testing the actual behavior, Instead, mock the Bar
and provide it however you would mock a real Bar
(such as passing it as constructor parameter).如果您正在测试Foo
但随后模拟它,则您不是在测试实际行为,而是模拟Bar
并提供它,但是您会模拟一个真正的Bar
(例如将其作为构造函数参数传递)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.