Unit test - read xml file
I m trying to do a unit test to a class which load and read an xml file. I m using NetBeans.
Here is my class structure,
Class A
{
private String fileName;
A (String fileName)
{
this.fileName = fileName;
load();
}
private load()
{
//here i load a xml file by given name
}
public readXml()
{
// read xml file and return list of records
}
}
Using NetBeans, it generates me some test classes with methods
@BeforeClass
public static void s开发者_开发技巧etUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@After
public void tearDown() {
}
@Before
public void setUp() {
}
and one method which TestReadXml()
where i have to load the file in order to perform unit testing of method "readXml", because for that clas "A" i hava method "load()" which is called in the cunstrctor in order to load the file and method readXml is use only for reading and returning records.
Thanks
Edit
want to test method readXml(), give a expect result which is in the input xml file. the preriquest is to load a xml file. The point is that i dont know where to do this action, so i get an error
java.lang.NullPointerException
at com.schober.main.ATest.testReadXml(ATest.java:83)
because here assertEquals(expResult, result);
the result is null
You should test all public methods and constructors of a class file. The tests will verify, that every call with every possible input gives the expected result.
In your case, you could write some tests to test the constructor. Call it with different parameters like
- A String that represents a readable and valid xml file
null
- A String that does not represent a file or folder
- A String that represents a folder
- A String that represents a file which does not contain xml code
- A String that represents a file that contains malformed xml code
- A String that represents a file that contains invalid xml code
- A String that represents a file that contains valid xml code but uses a different schema
精彩评论