Un test unitaire raisonnable pourrait-il être écrit pour ce code qui extrait une archive rar en la déléguant à un outil capable sur le système hôte s'il en existe un? Je peux écrire un cas de test basé sur le fait que mon ordinateur exécute linux et que l'outil unrar est installé, mais si un autre développeur qui exécute windows vérifie le code, le test échouera, bien qu'il n'y ait aucun problème avec le code extracteur . Je dois trouver un moyen d'écrire un test significatif qui n'est pas lié au système et à l'outil unrar installé. Comment aborderiez-vous cela?Test unité sensible possible?
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 injection de la carte de "OS" aux commandes allait être ma suggestion aussi bien –