dcsimg
junit TestRunner constructor
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   rick_curtis
Posted On:   Thursday, February 3, 2005 01:44 PM


So I'm trying to use the constructor:

/**

* Constructs a TestRunner using the given stream for all

* the output

*/

public TestRunner(PrintStream writer) {
this(new ResultPrinter(writer));
}



Then i call test.run(something.class). This doesn't put any data to the given stream... am I missing something? I've made sure that my PrintStream is working properly. I can manually write to the stream via

outFile.print("Test printing.");


Thanks, Rick

Re: junit TestRunner constructor

Posted By:   Anonymous  
Posted On:   Tuesday, February 8, 2005 09:48 AM

More of a code sample would help, but I think I know what the issue is.


The TestRunner.run() methods are static. They create their own new instance of a TestRunner and then call doRun().


I t*h*i*n*k what you need to do is call a doRun() method instead.



I grabbed this TestRunner code from a google search, didn't look at the source on my machine, so buyer beware.


....
/**
* Runs a suite extracted from a TestCase subclass.
*/
static public void run(Class testClass) {
run(new TestSuite(testClass));
}

/**
* Runs a single test and collects its results.
* This method can be used to start a test run
* from your program.
*

* public static void main (String[] args) {
* test.textui.TestRunner.run(suite());
* }
*

*/
static public TestResult run(Test test) {
TestRunner runner= new TestRunner();
return runner.doRun(test);
}
....
public TestResult doRun(Test test) {
return doRun(test, false);
}

public TestResult doRun(Test suite, boolean wait) {
TestResult result= createTestResult();
result.addListener(fPrinter);
long startTime= System.currentTimeMillis();
suite.run(result);
long endTime= System.currentTimeMillis();
long runTime= endTime-startTime;
fPrinter.print(result, runTime);

pause(wait);
return result;
}

About | Sitemap | Contact