Reputation:
I am new to JUnit, following this tutorial but need some suggestions and understanding on my testcase
I have some xmls (3MB-6MB each) in a folder, for each xml I need to test some tags whether they contain some value or not and sometimes match that value to a specific result.
So how can I execute all my @Test functions inside a loop, for each test? Do I need to normally call the @Test Functions (coz they are called automatically) inside loop?
Please help me undertand JUnit with this context. Thanks
Junit testcase
public class SystemsCheck {
def fileList
@Before
public void setUp() throws Exception {
def dir = new File("E:\\temp\\junit\\fast")
fileList = []
def newFile=""
dir.eachFileRecurse (FileType.FILES) { file ->
fileList << file
}
fileList.each {
//how should i test all @Test with it.text
println it.text
}
}
@Test
void testOsname(){
NixSystems ns=new NixSystems()
/*checking if tag is not empty*/
//assertEquals("Result","",ns.verifyOsname())
}
@Test
public void testMultiply(){
NixSystems ns=new NixSystems()
assertEquals("Result", 50, ns.multiply(10, 5))
}
}
class NixSystems {
public def verifyOsname(xml){
return processXml( xml, '//client/system/osname' )
}
public def multiply(int x, int y) {
return x * y;
}
def processXml( String xml, String xpathQuery ) {
def xpath = XPathFactory.newInstance().newXPath()
def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def inputStream = new ByteArrayInputStream( xml.bytes )
def records = builder.parse(inputStream).documentElement
xpath.evaluate( xpathQuery, records )
}
}
Upvotes: 0
Views: 273
Reputation: 5989
JUnit has dedicated feature for this kind of tests - Parameterized JUnit Tests. This basically do what you've already done but in concise, standarized manner.
Below is code for Java
- can be easily converted to Groovy
.
@RunWith(Parameterized.class)
public class TestCase {
private Type placeholder1;
private Type placeholder2;
...
public TestCase(Param1 placeholder1, Param2 placeholder2, ...) {
this.placeholder1 = placeholder1;
}
@Parameters
public static Collection<Object[][]> data() {
//prepare data
//each row is one test, each object in row is placeholder1/2... for this test case
}
@Test
public void yourTest() {...}
}
In JavaDocs of org.junit.runners.Parameterized you can find example of Fibonnaci numbers test.
Upvotes: 2