The distance from the last time to do junit monomer test for several months, and sure enough to use, and learn some new things, summed up.
The first is the understanding of the spy, has been used to mock, did not use the spy, so the spy does not understand, recently used a few times, also in Google learning a bit, online said are more written, mock: all Mock;spy: part of the mock; What is called part but always can not understand, their actual use after feeling, all the mock should be the whole method to mock, do not actually walk method, directly return the mock return value, and the spy part of the mock should be although mock this method, and return the mock value we set, But in fact, the way to go to the mock, if the method is wrong, the same will report abnormal, affecting the current method of testing. Pure personal understanding!!! Misleading and irresponsible!!!!!
The use of verify, has not used this thing, the general assertion is enough, this need to use, understand the role of the next he, I know the main four role: ① whether to invoke the success of the method; ② the number of times the method was invoked, the ③ parameter is correct ④ the certificate timeout.
When using the spy Y should pay attention to a point, when the mock, do not use when the first, but with Doreturn ();
E.g:powermockito.doreturn ("1234"). When (instance, "method", args); √
E.g:powermockito.when (Instance, "method", args). Thenreturn (""); x
Mock Private Method (public method down with private method case) (all mock)
① previous methods, using Whitebox.invoke ();
②powermockito.docallrealmethod (). When (instance). Method ();
Powermockito.doreturn (). When (instance, "method", args);
Mock Private Method (public method down with private method case) (partial mock)
e.g:
public class A () {
Public String b (int id) {
return C ();
}
Private String C () {
Return "ABC"
}}
Test
A = new A ();
A aspy = Powermockito.spy (a);
Powermockito.doreturn ("CCC"). When (Aspy, "C", 1);
String RetValue = aspy.b (1);
Powermockito.verifyprivate (Aspy). Invoke ("C");
Mock static method
① @RunWith (Powermockrunner.class)
@PrepareForTest ({staticclass.class})
.......
Powermockito.mock (Staticclass.class);
Powermockito.when (Staticclass.method (args)). Thenreturn ("123")//Making Parameters Mockito.any ()
Checking the static method verify
Powermocito.verifystatic (Mockito.times (1));
Staticclass.method (ARGS1,ARGS2);
The static method under the Mock final class, the operation is the same as 5.
Suppress method, skipping a line in the test
E.g:powermockito.suppress (Powermockito.method (Staticclass.class, "method name", ARGS1,...));
In the case of no more than the two mock private methods, we generally use reflection to implement, the call to the private method
method = ClassName.class.getDeclaredMethod ("Private Method Name");
Method.setaccessible (TRUE)//set accessible
The current summary of these, new problems encountered, new receipts will continue to increase.
Finally recommend a website, I learned a lot here: http://javatechnology.net/java/mockito-spy/
This article is from the "Riding monkey on the Tree" blog, please be sure to keep this source http://qihoushangshu.blog.51cto.com/7872138/1694216