java - how to create a test description with junit4 runListner -
following jsonlistner have extended junit runlistner. using json report after test run
package org.junit.runner; import org.junit.runner.notification.runlistener; import org.junit.runner.description; import org.junit.runner.result; import org.junit.runner.notification.failure; import java.io.filewriter; import java.io.ioexception; import org.json.simple.jsonarray; import org.json.simple.jsonobject; class jsonlistener extends runlistener { private jsonobject _tests_passed = new jsonobject(); private jsonobject _tests_started = new jsonobject(); private jsonobject _tests_finished = new jsonobject(); private jsonobject _tests_failures = new jsonobject(); private jsonobject _tests_ignored = new jsonobject(); public void testrunstarted(description description) throws exception { } public void testrunfinished(result result) throws exception { system.out.println(_tests_passed); } public void teststarted(description description) throws exception { string key = description.getdisplayname(); long value = system.currenttimemillis(); _tests_started.put(key, value); } public void testfinished(description description) throws exception { // trying prit description of test running system.out.println(palindromestringtest.desc); } public void testfailure(failure failure) throws exception { string key = failure.getdescription().getdisplayname(); long value = system.currenttimemillis(); _tests_failures.put(key, value); } public void testassumptionfailure(failure failure) { string key = failure.getdescription().getdisplayname(); long value = system.currenttimemillis(); _tests_failures.put(key, value); } public void testignored(description description) throws exception { string key = description.getmethodname(); long value = system.currenttimemillis(); _tests_ignored.put(key, value); } } this 1 of test class running through runner.
import java.io.*; import org.junit.after; import org.junit.afterclass; import org.junit.before; import org.junit.beforeclass; import org.junit.test; import static org.junit.assert.*; public class palindromestringtest { bytearrayoutputstream outcontent = new bytearrayoutputstream(); static string desc; @before public void setupstream() { system.setout(new printstream(outcontent)); } @after public void cleanupstream() { system.setout(new printstream(new fileoutputstream(filedescriptor.out))); } @test public void revtest() { palindromestringtest.desc = "this should reverse string"; string = "madam"; palindromestring obj = new palindromestring(); string b = obj.rev(a); assertequals("madam", b); } } my question how this.desc value in testfinished method of runlistner? alternative have thought of using description field value after each test run complete. above implementation, failing compile runner
buildfile: /home/bonnie/workstation/junit-json-runner/build.xml clean: [delete] deleting directory /home/bonnie/workstation/junit-json-runner/build [delete] deleting directory /home/bonnie/workstation/junit-json-runner/dist mkdir: [mkdir] created dir: /home/bonnie/workstation/junit-json-runner/build/classes [mkdir] created dir: /home/bonnie/workstation/junit-json-runner/dist compile: [javac] compiling 2 source files /home/bonnie/workstation/junit-json-runner/build/classes [javac] /home/bonnie/workstation/junit-json-runner/src/jsonlistner.java:39: error: cannot find symbol [javac] system.out.println(palindromestringtest.desc); [javac] ^ [javac] symbol: variable palindromestringtest [javac] location: class jsonlistener [javac] note: /home/bonnie/workstation/junit-json-runner/src/jsonlistner.java uses unchecked or unsafe operations. [javac] note: recompile -xlint:unchecked details. [javac] 1 error build failed /home/bonnie/workstation/junit-json-runner/build.xml:22: compile failed; see compiler error output details. total time: 1 second what ways, can acomplished? there existing way create , test description in runner?
you have use java.lang.reflect.* in jsonlistner in order access description variable.
create method getdescrition in palindromestringtest class, , return description.
import java.io.*; import org.junit.after; import org.junit.afterclass; import org.junit.before; import org.junit.beforeclass; import org.junit.test; import static org.junit.assert.*; public class palindromestringtest { bytearrayoutputstream outcontent = new bytearrayoutputstream(); static string desc; //public method value of desc public function getdescription(){ return desc; } @before public void setupstream() { system.setout(new printstream(outcontent)); } @after public void cleanupstream() { system.setout(new printstream(new fileoutputstream(filedescriptor.out))); } @test public void revtest() { palindromestringtest.desc = "this should reverse string"; string = "madam"; palindromestring obj = new palindromestring(); string b = obj.rev(a); assertequals("madam", b); } } now can access description in testfinished method of java reflection class
public void testfinished(description description) throws exception { class<?> testclass = description.gettestclass(); method m = testclass.getdeclaredmethod("getdescription"); object o = m.invoke(null); system.out.println(o.tostring()); }
Comments
Post a Comment