Monday 15 April 2013

java - Testing method via spy with mocked collaborators using PowerMock & Mockito -


Trying to find the best way to do something that seems very simple: test that a specific method is a collaborator From a class under test I'm Mokito (1.9.5) & amp; PowerMock (1.5.1) with JKK6

Setting a partial mimetic via Mokita Detective is a general approach, set internal code via PowerMock WhiteBox methods, and then I do the test Call method: creatFoo () .

As much as possible, here is the class under trial:

  public class FooGate {BlackBox.Factory factory; black box. Repeatedly; Public Zero createFoo (Fu Fu) {Bar = Factory. Produce (blackbox bars.); Bar.create (bbFoo); } ...}   

Here is the exam that is not working:

  @RunWith (PowerMockRunner.class) @PrepareForTest (FooGate.class ) Public Class FooGateTest {@Test Public Zero Test ({{FooGate testGate = Mockito.spy (New FooGate ()); Blackbucks Factor docfactry = Fake (Blackbox Factor class); black box. Bark Mockbair = Fake (Blackbox Bars.); Whitebox .Setsteinstate (TestGate, Blackbox. Factor, MockFactry); Whitebox .Setsteinstate (TestGate, Blackbox Bar, Mockbair); Fu fu = new foo (); Foo.setSetting ("X"); DoAnswer (new answer & lt; null & gt; () {public zero answer (invocationOnmock invocation) {... do stuff ...}}). (Mockbar) .create (any (Foo.class)); // NPE here: It seems that the test in the bar gate is testGate.createFoo (foo); AssertStuff (...); }}   

If I remove whitebox ... for factory , I get a NPE at factory.produce () . So, it seems that work is to be done.

But doAnswer () is not apparently. Note: It does not look like it is an issue with imports, so I left them, but if you think they can be useful, then I can include them.

I had this problem quite simple. This was also not possible in the first version of this question.

The problem was when the product at factory i white box.SetInternet () was called, that method It was not properly installed, so it was returning empty space. Therefore, bar was zero when I tried to call bar.create () : hence the NPE

Also the object ())). Then return (Mockbar);

... and pass the trial, no error.

No comments:

Post a Comment