简体   繁体   English

验证Mockito中调用downstream的一个方法(通过private void重载方法)

[英]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:我的并发症:

  1. I have an overloaded void method, plus it's private (I cannot change it from private although that would work, for this purpose)我有一个重载的 void 方法,加上它是私有的(我不能将它从私有中更改,尽管为此目的可以工作)
  2. It has a public method downstream from the private method I could try to test (Bar.bar()), but not sure how to test it properly with Mockito它在私有方法下游有一个公共方法,我可以尝试测试 (Bar.bar()),但不确定如何使用 Mockito 正确测试它
  3. I'm trying to test for customer length over 5 and under 5, to make sure it hits the private 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)一次。
  4. Would rather not use Powermockito for simplicity if possible为了简单起见,宁愿不使用 Powermockito

 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()语句,然后做...

  • verify(foo, times(1)).foo(any(Customer.class));验证(foo, times(1)).foo(any(Customer.class)); (WORKS) (作品)
  • verify(bar, times(1)).bar(any(Integer.class), any(String.class));验证(条形图,时间(1))。条形图(任何(Integer.class),任何(String.class)); (FAILS, returns 0) (失败,返回 0)
  • doCallRealMethod().when(foo).foo(any(Customer.class)); doCallRealMethod().when(foo).foo(any(Customer.class)); (WORKS) (作品)
  • foo.foo(Customer); foo.foo(客户); (WORKS) (作品)
  • BDDMockito.given(bar.bar("123", date)).willReturn(Order); BDDMockito.given(bar.bar("123", date)).willReturn(Order); (FAILS) (失败)
  • BDDMockito.then(bar).should().bar(any(Integer.class), any(String.class)); BDDMockito.then(bar).should().bar(any(Integer.class), any(String.class)); (FAILS) (失败)

Pretty stumped here, with the addition of my complications.加上我的并发症,我在这里很困惑。 I'm still new to Mockito, open to any new suggestions.我还是 Mockito 的新手,愿意接受任何新建议。

  1. In general, test the behavior of public API. Private methods are an implementation detail;一般来说,测试public API的行为。私有方法是一个实现细节; test them indirectly through calling the public methods.通过调用公共方法间接测试它们。
  2. You're on the right track with mocking 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中。

Edit编辑

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM