void 關(guān)鍵字
本節(jié)說明如何聲明和調(diào)用一個(gè)void方法。
下面的例子聲明了一個(gè)名為printGrade的方法,并且調(diào)用它來打印給定的分?jǐn)?shù)。
示例
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' ); } } } |
以上實(shí)例編譯運(yùn)行結(jié)果如下:
1
|
C |
這里printGrade方法是一個(gè)void類型方法,它不返回值。
一個(gè)void方法的調(diào)用一定是一個(gè)語句。 所以,它被在main方法第三行以語句形式調(diào)用。就像任何以分號結(jié)束的語句一樣。
單測void類型的方法
Java的Sevice層會(huì)有很多void類型的方法,比如save*、update*,這類方法只是做一些更新,不會(huì)有返回值,其單測不能根據(jù)方法的返回值來編寫,只能采用特殊方法;
本方法環(huán)境: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, "規(guī)則ID不能為Null" ); Assert.notNull(newRuleName, "規(guī)則名稱不能為Null" ); Assert.notNull(ucId, "操作人的UCID不能為Null" ); String cleanNewRuleName = StringUtils.trim(newRuleName); if (StringUtils.isBlank(cleanNewRuleName)) { throw new IllegalArgumentException( "新的規(guī)則名稱不能為空" ); } // 查詢規(guī)則對象 Rule rule = queryRuleById(ruleId); if ( null == rule) { throw new IllegalDataException( "沒有查到該規(guī)則" ); } 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); // 查詢規(guī)則對象 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) { // 斷點(diǎn)2:這里隨后執(zhí)行 Rule rule = (Rule) invocation.getArguments()[ 0 ]; Assert.assertTrue(rule.getRuleName().equals( "newRuleName" )); return null ; } }).when(ruleDao).updateSelective(Mockito.any(Rule. class )); // 斷點(diǎn)1:先執(zhí)行到這里 ruleService.updateRuleName(ruleId, newRuleName, ucId); } |
如注釋所示,如果加了兩個(gè)斷點(diǎn)的話,執(zhí)行的過程中,會(huì)先執(zhí)行最后的調(diào)用行,端點(diǎn)1執(zhí)行的過程中,會(huì)執(zhí)行到端點(diǎn)2的stub,這時(shí)候在斷點(diǎn)2可以獲取到方法執(zhí)行的入?yún)ⅲ瑢θ雲(yún)⑦M(jìn)行Assert校驗(yàn),即可實(shí)現(xiàn)目的;
new Anwer是個(gè)接口,其中只有一個(gè)方法,用于設(shè)置方法調(diào)用的代理執(zhí)行入口
doAnswer的實(shí)現(xiàn)
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; } |
當(dāng)代碼執(zhí)行到“ruleDao.updateSelective(rule);”的時(shí)候,會(huì)觸發(fā)針對mock對象調(diào)用的攔截器,在攔截器中,會(huì)創(chuàng)建一個(gè)動(dòng)態(tài)代理,動(dòng)態(tài)代理的invocation就是new Answer中覆蓋的方法;
使用攔截、代理兩種方法,實(shí)現(xiàn)了對mock對象方法的入?yún)ⅰ⒊鰠⒌脑O(shè)定和獲取,使用這種方式,就可以校驗(yàn)VOID方法內(nèi)部的執(zhí)行類調(diào)用的情況。