void 關鍵字
本節說明如何聲明和調用一個void方法。
下面的例子聲明了一個名為printGrade的方法,并且調用它來打印給定的分數。
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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' ); } } } |
以上實例編譯運行結果如下:
1
|
C |
這里printGrade方法是一個void類型方法,它不返回值。
一個void方法的調用一定是一個語句。 所以,它被在main方法第三行以語句形式調用。就像任何以分號結束的語句一樣。
單測void類型的方法
Java的Sevice層會有很多void類型的方法,比如save*、update*,這類方法只是做一些更新,不會有返回值,其單測不能根據方法的返回值來編寫,只能采用特殊方法;
本方法環境:Mockito、testng
被測試的方法:
想要被測試的VOID方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@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返回的方法測試
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@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的實現
1
2
3
4
5
6
7
8
9
10
|
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方法內部的執行類調用的情況。