0
我想用類似私有方法測試ProcessServiceImpl
類的過程方法:readContent
和isValidFile
和userService
類。在jmockit中模擬被測試類的私有方法
@Named("processServiceImpl")
public class ProcessServiceImpl {
@Inject
private UserService userService;
public User process(Long userId, File inputFile) throws InvalidFileException {
User user = userService.load(userId);
String fileContent = readContent(inputFile);
if (isValidFile(fileContent)) {
User updatedUser = userService.updateUser(user, fileContent);
return updatedUser;
} else {
throw new InvalidFileException();
}
}
private String readContent(File inputFile) {
//implementation
}
private boolean isValidFile(String fileContent) {
//implementation
}
}
我嘲笑了UserService
併成功注入。但我不能在測試下嘲笑這個班級的私人方法。基於this和this鏈接我試圖嘲笑他們與NonStrictExpectations
,但它沒有任何invoke
方法!有沒有另一種方法來做到這一點? (我使用jmockit-1.8)
new NonStrictExpectations(processServiceImpl) {{
invoke(processServiceImpl, "readContent");
result = "";
}};
在這個特定的用例中你是對的。但是在一個依賴於某些類(方法調用)的複雜私有方法中,我認爲嘲笑它並不是很糟糕,而不是嘲笑所有的依賴類。 – united 2014-10-29 20:44:03