This article will briefly describe how to use powermock and Mockito to mock
1. Constructors
2. Static functions
3. Enumeration implementation of the Singleton
4. Select the parameter value as the return value of the function
5. Change the value of the method parameter in the call to the mock-out method
A brief explanation: Mockito has actually been able to meet most of the requirements, but its implementation mechanism is to use Cglib to dynamically create an instance of the interface class. However, this implementation cannot be used for constructors and static functions, because it requires the use of the class's bytecode (for example, using Javassist). That's why we need to use Powermock together.
1. Mock constructors, if there is code that does not use DI injection dependent instances, you can use Powermock to simulate the creation of objects in a unit test.
Note the beginning of the two lines of 2 annotations @RunWith and @PrepareForTest
@RunWith is relatively simple, powermockrunner.class is always behind
The @PrepareForText you need to add is the class name that calls the constructor, not the class itself that has the constructor.
In the following example, the class we want to test is helper, which invokes the constructor of the Somthing class in the helper class to create the instance.
@RunWith (Powermockrunner.class)
@PrepareForTest (Helper.class)
public class Helpertest {
@Mock
Private Something mocksomething;
@InjectMocks
Private helper Helper;
@Test
public void DoSomething () throws Exception {
String argument = "arg";
Powermockito.whennew (Something.class). witharguments (argument). Thenreturn (mocksomething)//mock static fields, you can also use this method
Call requires test method
helper.dosomething (argument);
To verify
Verify (mocksomething). DoIt ();
}
}
public class Helper {
public void dosomething (String arg) {
Something Something = new Something (ARG);
Something.doit ();
}
}
2,mock static functions, a singleton pattern is a typical example of a static function that is called. Note The key points are the same as the mock constructors.
Class Classwithstatics {
public static String getString () {
return "String";
}
public static int getInt () {
return 1;
}
}
@RunWith (Powermockrunner.class)
@PrepareForTest (Classwithstatics.class)
public class Stubjustonestatic {
@Test
public void Test () {
Powermockito.mockstatic (Classwithstatics.class);
When (Classwithstatics.getstring ()). Thenreturn ("hello!");
System.out.println ("String:" + classwithstatics.getstring ());
System.out.println ("Int:" + classwithstatics.getint ());
}
}
3. A singleton for a mock enumeration implementation
Singletonobject.java
}
SingletonConsumer.java
public class Singletonconsumer {
}
Singletonconsumertest.java
}
4. Returns the value of the parameter as a function return value.
Mockito 1.9.5, provides a convenient way to implement this need, before you can use an anonymous function to return a answer to implement.
when(myMock.myFunction(anyString())).then(returnsFirstArg());
where Returnsfirstarg () is a static method in Org.mockito.AdditionalAnswers.
There are other similar methods in this class
Returnssecondarg ()
Returnslastarg () returnsargumentat (int position)
5. Change the value of the method parameter in the call to the mock-out method
When (Mymock.somemethod (List.class)). Thenanswer ((new answer<void> () {
@Override
Public Void Answer (Invocationonmock invocation)
Throws Throwable {
object[] args = invocation.getarguments ();
List arg1 = (list) args[0];
Arg1.add ("12345");
return null;
}
} ) );
Verifying with generic parameters
Verify (Someservice). Process (Matchers.<collection<person>>any ());
Verify (Adunomasterbaseprocessor). Processbinfiles (Anylistof (File.class)); http://www.blogjava.net/usherlight/ Archive/2015/06/16/425740.html
The
Briefly describes how to use Powermock and Mockito to mock 1. Constructor 2. Static function 3. Enumeration of the implementation of the Singleton 4. Select parameter values as function return values (GO)