Java編程中void方法的學習教程_java

來源:互聯網
上載者:User

void 關鍵字
本節說明如何聲明和調用一個void方法。
下面的例子聲明了一個名為printGrade的方法,並且調用它來列印給定的分數。
樣本

public class TestVoidMethod {  public static void main(String[] args) {   printGrade(78.5);  }  public static void printGrade(double score) {   if (score >= 90.0) {     System.out.println('A');   }   else if (score >= 80.0) {     System.out.println('B');   }   else if (score >= 70.0) {     System.out.println('C');   }   else if (score >= 60.0) {     System.out.println('D');   }   else {     System.out.println('F');   }  }}

以上執行個體編譯運行結果如下:

C

這裡printGrade方法是一個void類型方法,它不傳回值。
一個void方法的調用一定是一個語句。 所以,它被在main方法第三行以語句形式調用。就像任何以分號結束的語句一樣。

單測void類型的方法
Java的Sevice層會有很多void類型的方法,比如save*、update*,這類方法只是做一些更新,不會有傳回值,其單測不能根據方法的傳回值來編寫,只能採用特殊方法;

本方法環境:Mockito、testng

被測試的方法:

想要被測試的VOID方法

@Override  public void updateRuleName(Long ruleId, String newRuleName, Long ucId) {    Assert.notNull(ruleId, "規則ID不能為Null");    Assert.notNull(newRuleName, "規則名稱不能為Null");    Assert.notNull(ucId, "操作人的UCID不能為Null");        String cleanNewRuleName = StringUtils.trim(newRuleName);    if (StringUtils.isBlank(cleanNewRuleName)) {      throw new IllegalArgumentException("新的規則名稱不可為空");    }        // 查詢規則對象    Rule rule = queryRuleById(ruleId);    if (null == rule) {      throw new IllegalDataException("沒有查到該規則");    }        rule.setRuleId(ruleId);    rule.setRuleName(cleanNewRuleName);    rule.setUpdateUcid(ucId);    rule.setUpdateTime(new Date());        ruleDao.updateSelective(rule);  }

測試的方法:

void返回的方法測試

@Test  public void testUpdateRuleName() {    Long ruleId = 1L;    String newRuleName = "newRuleName";    Long ucId = 123L;        List<Rule> rules = new ArrayList<Rule>();    Rule rule = new Rule();    rule.setRuleStatus((byte) DBValueSetting.RULE_STATUS_TAKE_EFFECT);    rules.add(rule);        // 查詢規則對象    Map<String, Object> params = new HashMap<String, Object>();    params.put("ruleId", ruleId);    Mockito.when(ruleDao.queryRulesByCondition(params)).thenReturn(rules);        Mockito.doAnswer(new Answer<Object>() {      public Object answer(InvocationOnMock invocation) {        // 斷點2:這裡隨後執行        Rule rule = (Rule) invocation.getArguments()[0];        Assert.assertTrue(rule.getRuleName().equals("newRuleName"));        return null;      }    }).when(ruleDao).updateSelective(Mockito.any(Rule.class));        // 斷點1:先執行到這裡    ruleService.updateRuleName(ruleId, newRuleName, ucId);  }

如注釋所示,如果加了兩個斷點的話,執行的過程中,會先執行最後的調用行,端點1執行的過程中,會執行到端點2的stub,這時候在斷點2可以擷取到方法執行的入參,對入參進行Assert校正,即可實現目的;

new Anwer是個介面,其中只有一個方法,用於設定方法調用的代理執行入口

doAnswer的實現

public interface Answer<T> {  /**   * @param invocation the invocation on the mock.   *   * @return the value to be returned   *   * @throws Throwable the throwable to be thrown   */  T answer(InvocationOnMock invocation) throws Throwable;}

當代碼執行到“ruleDao.updateSelective(rule);”的時候,會觸發針對mock對象調用的攔截器,在攔截器中,會建立一個動態代理,動態代理的invocation就是new Answer中覆蓋的方法;

使用攔截、代理兩種方法,實現了對mock對象方法的入參、出參的設定和擷取,使用這種方式,就可以校正VOID方法內部的執行類調用的情況。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.