When you write unit test, you must not like to test those return void method, because most of the execution of a sequence of actions, feel unable to test, in fact, for such a method, if the execution of the method may be abnormal, (such as the method itself using the throws statement), This method can also be tested if it is a void return value.
For example, here's a unit test I wrote for the return void method:
@Test public void Testserveresource () throws Exception {Mockwebserver mockwebserver = new Mock
WebServer ();
Mockwebserver.init ();
Mockresourcerequest request = new Mockresourcerequest ();
Request.setparameter ("id", "4");
Request.setparameter ("name", "Walmart");
Mockresourceresponse response = new Mockresourceresponse ();
Mockportletpreferences preferences = new Mockportletpreferences ();
Preferences.setvalue (Rslaunchconstants.rs_launch_portlet_preference_rest_ws_url, "http://localhost:28080/");
Request.setpreferences (preferences);
try{Rslaunchportlet.serveresource (request, response);
Mockwebserver.destory ();
Asserttrue (TRUE);
}catch (Exception ex) {//normally,the execution should never reach-so-if reach, we fail this UT
Asserttrue (FALSE); }
}
We can see that the most critical place is between line 13th to 20th, we use a try block to contain the code snippet of the possible cannon exception, and add Asserttrue (TRUE) to the end of the block, and add Asserttrue (false) to the catch block, and the result is clear. , if the method is executed correctly, then it must have executed all the statements in the try block, so the Asserttrue (true) is executed and the green bar is displayed. But if this method does not perform correctly, it throws an exception at 14 or 15 rows to execute the catch block, so it executes to 19 lines, thus displaying the red bar. It's simple.
This article from the "Cohesion of parallel Lines" blog, please be sure to retain this source http://supercharles888.blog.51cto.com/609344/989564