Oh I'm using Mokitos and Testing to write a unit test for a class that is very useful for a service I am really new to this, so I feel that for my exact problem, the net is stuck with a little bit of documentation.
My exam is basically seen like this
@Test Public Classes ClassToTestTest {@Mock Private Object1 object1; @mok pivot object2 object2; @InjectMocks Personal ClassTest Test Classest Test; Public Zero test1 () {classToTest.methodToTest (); } ... ...} The actual class is as follows:
import final class; Public Class Classost {Personal Object 1 Object 1; Private Object 2 Object 2; Public Zero Method (Test) (object2 = FinalClass.getObject2 (); ... ...} ... ...} I used the object 2 To return the fake, only the final class .getObject2 () is required, I know I can copy the final clip using PowerMock, but I do not like how to inject it into the classmate test created by me So that when I run classToTest.methodToTest () from my test object2, then my maccad Thanksgiving in advance!
You erase annotation @PrepareForTest and use mask static () are required, when you want to duplicate a final static class with powermokitos, I think where you can get confused that you have a last stable Classes and not only are working with a final class, so some additional fake calls come Looking at these implementation sections
/ * The last static class which is giving you all the funny sorrow * / Public Final Category FinalStaticClass {// This object Example "unless we will not copy" (") will not be" class private MyObject obj = new MyObject (); Public stable MyObject getMyObject () {back obj; }} / * MyClass * / Public Class MyObject {} * * A simple price object used by * * the category you want to test * / Public Classes MyClass {Private MyObject obj; Public Zero Method (Test) () {obj = FinalStaticClass.getMyObject (); // something) public MyObject getMyObject () {back obj; }} Make sure you include powermock-mockito-1.4.10-full.jar in your project, then you can use this test class Can use import static org.junit.assert.assertEquals; Static org.powermock.api.mockito.PowerMockito.mockStatic Import; Import static stat Import ... @Ravivith (PowerMonkar.class) @PrepareForTest (LastStatic Class.class) People Class MyClassTest {@Mock MyObject Expected Item; @InjectMocks MyClass myClass = New MyClass (); @Test Public Zero test1 () {// fake all static methods mockStatic (FinalStaticClass.class); . When (FinalStaticClass.getMyObject ()) thenReturn (expectedObject); // test myClass.methodToTest () execute the method under; AssertEquals (expected object, myClass.getMyObject ()); }} You can see that the required object is a duplicate that you made in test and the implementation is not returned from the final static class < / Div>
Comments
Post a Comment