Wednesday, 15 February 2012

mockito - Is there any way to mock field of Class -


Here's my case, I have a class A that has a member field b. And I want to test and in the unit test, I have joked to A and also need to call it F () which will invite BKF. But fake B has B variable taps, so NPE will be thrown away, and I have not got any set / set method for B, so there is no way to copy B? Thank you

  Public Fixed Category B {Public Zero F () {}} Public Stable Class A {Bb; Public Zero F () (BF); }}  

If you enter b in an exam Property, you have given default (package-private) access to the b property, so as long as your test is in the same package, you can change directly to the B property.

  @Test Public Zero TestB () {an underTest = new A (); Bee Mock B = Mokito. Mock (baclas); UnderTest.b = mockedB; UnderTest.f (); Mockito.verify (mockedB) .f (); }  

On the individual side, I personally dislike personal access as well as personal access to members properties for trials, and instead a dependency injection framework Or the constructor will recommend injection.

Although you have described that you have ridiculed A, I would have thought that this is the case A's method F () will not do anything - you will not get a zero pointer exception because the fake calls are real B will change the property and there is only one zero method which does nothing. If this is the case, can you provide more information?


No comments:

Post a Comment