How to return void for java unit test

Source: Internet
Author: User

 

When writing a unit test, you certainly do not like to test the methods that return void, because most of them execute some action sequences and cannot be tested. In fact, for such methods, if the execution of a method is possibly abnormal, for example, the method itself uses the throws statement), this method can be tested even if it returns the void value.

 

For example, the following is a unit test that I wrote for the return void method:

 
 
  1. @Test 
  2.          public void testServeResource()    throws Exception { 
  3.         MockWebServer mockWebServer = new MockWebServer(); 
  4.         mockWebServer.init(); 
  5.         MockResourceRequest request = new MockResourceRequest(); 
  6.         request.setParameter("id", "4"); 
  7.         request.setParameter("name", "walmart"); 
  8.         MockResourceResponse response = new MockResourceResponse(); 
  9.         MockPortletPreferences preferences = new MockPortletPreferences(); 
  10.         preferences.setValue(RSLaunchConstants.RS_LAUNCH_PORTLET_PREFERENCE_REST_WS_URL, "http://localhost:28080/"); 
  11.         request.setPreferences(preferences); 
  12.          
  13.          try{ 
  14.                   rsLaunchPortlet.serveResource(request, response); 
  15.                  mockWebServer.destory(); 
  16.                  assertTrue(true); 
  17.          }catch(Exception ex){ 
  18.               //normally,the execution should never reach here ,so if reach here , we fail this UT 
  19.               assertTrue(false); 
  20.          } 

We can see that the most important thing is between 13th and 20 rows. We use a try block to include code segments that may cause gun exceptions, and add assertTrue (true) at the end of the block ), when assertTrue (false) is added to the catch Block, the result is clear. If this method is correctly executed, it must be the completion of all the statements in the try block, therefore, assertTrue (true) is executed to display green bars. If this method is not correctly executed, an exception will be thrown in lines 14 or 15 to execute the catch Block. Therefore, 19 rows will be executed to display the red bars. It's easy.

This article from "parallel line cohesion" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/989564

Related Article

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.