能合理单元测试被用于此代码,其通过它委托给一个能够工具(如果存在)在主机系统上提取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