Problems and solutions in the process of using Powermock

Source: Internet
Author: User
Tags exception handling

1.

The following exception appears

Org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument Matcher detected here:
-> at Beyond.tools.util.BeyondExcelUtilsTest.testDoMain (beyondexcelutilstest.java:44)

You are cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
When (Mock.get (Anyint ())). Thenreturn (NULL);
Dothrow (New RuntimeException ()). When (mock). Somevoidmethod (Anyobject ());
Verify (mock). SomeMethod (Contains ("foo")

Also, this is the error might show up because your use argument matchers with methods to that cannot.
Following methods *cannot* be Stubbed/verified:final/private/equals ()/hashcode ().

= = "

Because @PrepareForTest (Test object. Class) is not appended before the method that tests the object


2. Example of testing a static method using Powermock

An object that contains a static method (an object that is stub through Powermock)
public class Utilityclass {

public static String Staticstringmethodwithoutarg () {
Return "";
}

public static string Staticstringmethodwitharg (String arg) {
Return "";
}

public static String Staticstringmethodwithexception () {
Return "";
}

public static void Staticvoidmethodwithexception () {
Return
}

public static void Staticvoidmethodwitharg (List<string> List) {

}
}
object classes that need to be tested
public classTargetapp{
public void Dorun () {
Without parameters, and there are methods of return
String returnValue1 = Utilityclass.staticstringmethodwithoutarg ();
System.out.println (returnValue1);

A method with a parameter and a return value
String returnValue2 = Utility.staticstringmethodwitharg ("Test data");
System.out.println (returnValue2);

Methods with no parameters and abnormal generation
String returnValue3 = Utilityclass.staticstringmethodwithexception ();
System.out.println (RETURNVALUE3);

No parameters, abnormal occurrence, no return value method
Utilityclass.staticvoidmethodwithexception ();
System.out.println ("Staticvoidmethodwithexception is ok!!");

Methods with parameters and no return value
list<string> strlist = new arraylist<string> ();
Strlist.add ("this");
Strlist.add ("is");
Strlist.add ("A");
Strlist.add ("Test");
Utilityclass.staticvoidmethodwitharg (strlist);
System.out.println ("Staticvoidmethodwitharg is ok!!");

}
}
The class that tests the Targetapp
public voidTestutilityclass() {
Call the static method of a mock (when testing on a static method, you must)
Powermockito.mockstatic (Utilityclass.class);

When you confirm that there are return values, you can test them by using the following three methods
A method without parameters
Powermockito.when (Utilityclass.staticstringmethodwithoutarg ()). Thenreturn ("Result for Staticstringmethodwithoutarg () ");
With the above difference in the transfer of parameters, (you can pass different types of multiple parameters, reference machers)
Powermockito.when (Utilityclass.staticstringmethodwitharg (machers.anystring ())). Thenreturn ("Result for Staticstringmethodwitharg () ");
Return exception
Powermockito.when (Utilityclass.staticstringmethodwithexception ()). Thenthrow (new runtimeexception);

For void methods, the following two types of rows can be tested
Note:
1. For void methods, the above method cannot be used for testing
2. The following methods, if the parameters in the when, in accordance with the format of the above, there will be a compilation error, the specific reasons do not know.
3. Using the following method, exception handling is required. No compile incorrectly
try {
Powermockito.dothrow (New RuntimeException ()) When (Utilityclass.class, "staticvoidmethodwithexception");
catch (Exception ex) {

}
If you need to return a different value to a parameter in a mock method, you need to use answer to process the parameters, and then return the different content
The actual installation of answer is described in the following.
try {
Powermockito.doanswer (New Arganswer ()). When (Utilityclass.class, "Staticvoidmethodwitharg", Machers.anylist ( String.class));
catch (Exception ex) {

}
Call to test object
Targetapp app = new Targetapp ();
try {
App.dorun ()
}catch (Exception ex) {

}
Confirm the number of times the above mock method was invoked.
Powermockito.veriftystatic (Mockito.times (1));
Utilityclass.staticstringmethodwithoutarg ();
Utilityclass.staticstringmethodwitharg ("");

If you need to test the parameters passed to the mock method, follow the following.
Type of argumentcaptor< parameter > argument = Argumentcaptor.forclass (type of parameter. Class);
Argumentcaptor<string> arg1= Argumentcaptor.forclass (string.class);
Example: Staticstringmethodwitharg
Utilityclass.Staticstringmethodwitharg (Arg1.capture ());
Assertequals ("Expected value", Argument1.getvalue ());
}
In a mock method, you need to modify the parameters that are passed. (That is, the value of the parameter is not the same before and after invoking the method of the mock)
public class Arganswer implements Answer<List<String>> {

Public list<string> Answer (Invocationonmock invocation) {
Object args[] = invocation.getarguments (0;
Gets the parameters passed over and then modifies the parameters. There is nothing special about modifying the method, omitted here
list<string> list = (list<string>) args[0];
return list;
}
}








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.