2014-04-30 15 views

risposta

9

Preferirei quanto segue, per evitare che il lettore creda che tu voglia affermare che il nome del file non esiste ...!

assertThat("File name should exist", file.exists(), is(equalTo(true))); 
9

Utilizzare il sovraccarico assertThat metodo

assertThat("File name doesn't exist", file.exists(), is(equalTo(true))); 
+3

Attenzione alla leggibilità! Quando lo leggi: "asserisci che il nome del file non esiste ..." ... è l'opposto di quello che stai testando! – chipiik

1

si consiglia utilizzare semplicemente i metodi assertTrue() con 2 arg:

Assert.assertTrue("File "+file.getAbsoluePath()+"does not exist", file.exists()); 
2

preferisco questo, dal momento che si può leggere come una frase: "affermare che il file 'myFile' esiste: esiste myFile è vero"

assertThat("File '" + myFile + "' exists", myFile.exists(), is(true)); 

E si ottiene anche un messaggio leggibile con tutto il necessario informazioni quando fallisce:

java.lang.AssertionError: File '/blah' exists 
Expected: is <true> 
    but: was <false> 
Problemi correlati