name_masked
name_masked

Reputation: 9803

Query regarding assert in JUnit

All,

I am having a hard time understanding the concept of assertXXX () in Junit. Currently, I have a method A(String fileName) that is generating a xml file for a input filename "XXX.XX" . So my test case includes testing if the code is generating any file and not just XML file which is wrong i.e. method A should only generate xml files.

My code is:

testCreateFile()
{
     String fileName = "testFile.csv";
     A(fileName);
     File fileObj = new File (fileName);
     assertFalse(fileObj.exists()); // Since I check if the file should not be created

}

If I do this, I get an AssertionError and jUnit window shows 2 Failures. Do I have to handle this Exception?

Upvotes: 2

Views: 237

Answers (1)

hvgotcodes
hvgotcodes

Reputation: 120318

if A creates a file, don't you want to assertTrue? In other words, you expect to find the file after you run A. And then you assert that the content of the file is the expected xml...

If an exception is a failure, you don't need to handle the exception.

Upvotes: 1

Related Questions