能合理單元測試被用於此代碼,其通過它委託給一個能夠工具(如果存在)在主機系統上提取RAR存檔書面? 我可以根據我的機器上運行Linux和安裝的unrar工具,但事實寫一個測試情況下,如果誰運行Windows的另一個開發人員將檢查出的測試代碼會失敗,但不會有什麼不妥提取碼。 我需要找到一種方法來編寫一個有意義的測試,而不是綁定到系統和unrar工具安裝。 你將如何解決這個問題?明智的單元測試可能嗎?
public class Extractor {
private EventBus eventBus;
private ExtractCommand[] linuxExtractCommands = new ExtractCommand[]{new LinuxUnrarCommand()};
private ExtractCommand[] windowsExtractCommands = new ExtractCommand[]{};
private ExtractCommand[] macExtractCommands = new ExtractCommand[]{};
@Inject
public Extractor(EventBus eventBus) {
this.eventBus = eventBus;
}
public boolean extract(DownloadCandidate downloadCandidate) {
for (ExtractCommand command : getSystemSpecificExtractCommands()) {
if (command.extract(downloadCandidate)) {
eventBus.fireEvent(this, new ExtractCompletedEvent());
return true;
}
}
eventBus.fireEvent(this, new ExtractFailedEvent());
return false;
}
private ExtractCommand[] getSystemSpecificExtractCommands() {
String os = System.getProperty("os.name");
if (Pattern.compile("linux", Pattern.CASE_INSENSITIVE).matcher(os).find()) {
return linuxExtractCommands;
} else if (Pattern.compile("windows", Pattern.CASE_INSENSITIVE).matcher(os).find()) {
return windowsExtractCommands;
} else if (Pattern.compile("mac os x", Pattern.CASE_INSENSITIVE).matcher(os).find()) {
return macExtractCommands;
}
return null;
}
}
+1注入「OS」的地圖命令也將是我的建議以及 – 2010-04-27 20:56:25