Reputation: 976
I'm trying to automate my plug-in unit tests for an Eclipse RCP application via the method described here: http://www.eclipse.org/articles/article.php?file=Article-PDEJUnitAntAutomation/index.html Note, that I'm running Eclipse 3.6
However, I'm getting compile errors along the lines of:
package org.eclipse.jdt.internal.junit.model does not exist
[javac] import org.eclipse.jdt.internal.junit.model.ITestRunListener2;
and
cannot find symbol
[javac] symbol: class ITestRunListener2
[javac] public class PDETestListener implements ITestRunListener2 {
[javac] ^
It seems like it has something to do with the following warning displayed in Eclipse:
Discouraged access: The type ITestRunListener2 is not accessible due to restriction on required library C:\eclipse\plugins\org.eclipse.jdt.junit.core_3.6.1.r361_v20100825-0800.jar
What does this mean exactly and how can I resolve it?
For reference, here is the code for one of the classes I'm trying to compile (taken from the example code found in the link above):
Thanks in advance.
package pde.test.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import junit.framework.TestResult;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter;
import org.eclipse.jdt.internal.junit.model.ITestRunListener2;
public class PDETestListener implements ITestRunListener2 {
private Object resultsCollector;
private int totalNumberOfTests;
private int testsRunCount;
private int numberOfTestsPassed;
private int numberOfTestsFailed;
private int numberOfTestsWithError;
private boolean testRunEnded = false;
private XMLJUnitResultFormatter xmlResultsFormatter;
private File outputFile;
private String suiteName;
private JUnitTest junitTestSuite;
private TestCase currentTest;
public PDETestListener(Object collector, String suite) {
resultsCollector = collector;
suiteName = suite;
junitTestSuite = new JUnitTest(suiteName);
junitTestSuite.setProperties(System.getProperties());
}
public void setOutputFile(String filename) {
outputFile = new File(filename);
}
public File getOutputFile() {
if (outputFile == null) {
setOutputFile("TEST-" + suiteName + ".xml");
}
return outputFile;
}
public boolean failed() {
return ((numberOfTestsFailed + numberOfTestsWithError) > 0) || (testRunEnded && (testsRunCount == 0));
}
public int count() {
return testsRunCount;
}
private XMLJUnitResultFormatter getXMLJUnitResultFormatter() {
if (xmlResultsFormatter == null) {
xmlResultsFormatter = new XMLJUnitResultFormatter();
try {
xmlResultsFormatter.setOutput(new FileOutputStream(getOutputFile()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return xmlResultsFormatter;
}
public synchronized void testRunStarted(int testCount) {
totalNumberOfTests = testCount;
testsRunCount = 0;
numberOfTestsPassed = 0;
numberOfTestsFailed = 0;
numberOfTestsWithError = 0;
testRunEnded = false;
getXMLJUnitResultFormatter().startTestSuite(junitTestSuite);
System.out.println("PDE Test Run Started - running " + totalNumberOfTests + " tests ...");
}
public synchronized void testRunEnded(long elapsedTime) {
testRunEnded = true;
junitTestSuite.setCounts(testsRunCount, numberOfTestsFailed, numberOfTestsWithError);
junitTestSuite.setRunTime(elapsedTime);
getXMLJUnitResultFormatter().endTestSuite(junitTestSuite);
System.out.println("Test Run Ended - " + (failed() ? "FAILED" : "PASSED") + " - Total: " + totalNumberOfTests
+ " (Errors: " + numberOfTestsWithError
+ ", Failed: " + numberOfTestsFailed
+ ", Passed: " + numberOfTestsPassed + "), duration " + elapsedTime + "ms.");
synchronized (resultsCollector) {
resultsCollector.notifyAll();
}
}
public synchronized void testRunStopped(long elapsedTime) {
System.out.println("Test Run Stopped");
testRunEnded(elapsedTime);
}
public synchronized void testRunTerminated() {
System.out.println("Test Run Terminated");
testRunEnded(0);
}
public synchronized void testStarted(String testId, String testName) {
testsRunCount++;
currentTest = new WrapperTestCase(testName);
getXMLJUnitResultFormatter().startTest(currentTest);
System.out.println(" Test Started - " + count() + " - " + testName);
}
public synchronized void testEnded(String testId, String testName) {
numberOfTestsPassed = count() - (numberOfTestsFailed + numberOfTestsWithError);
getXMLJUnitResultFormatter().endTest(currentTest);
System.out.println(" Test Ended - " + count() + " - " + testName);
}
public synchronized void testFailed(int status, String testId, String testName, String trace, String expected, String actual) {
String statusMessage = String.valueOf(status);
if (status == ITestRunListener2.STATUS_OK) {
numberOfTestsPassed++;
statusMessage = "OK";
} else if (status == ITestRunListener2.STATUS_FAILURE) {
numberOfTestsFailed++;
statusMessage = "FAILED";
getXMLJUnitResultFormatter().addFailure(currentTest, new AssertionFailedError(trace));
} else if (status == ITestRunListener2.STATUS_ERROR) {
numberOfTestsWithError++;
statusMessage = "ERROR";
getXMLJUnitResultFormatter().addError(currentTest, new Exception(trace));
}
System.out.println(" Test Failed - " + count() + " - " + testName + " - status: " + statusMessage
+ ", trace: " + trace + ", expected: " + expected + ", actual: " + actual);
}
public synchronized void testReran(String testId, String testClass, String testName, int status, String trace, String expected, String actual) {
String statusMessage = String.valueOf(status);
if (status == ITestRunListener2.STATUS_OK) {
statusMessage = "OK";
} else if (status == ITestRunListener2.STATUS_FAILURE) {
statusMessage = "FAILED";
} else if (status == ITestRunListener2.STATUS_ERROR) {
statusMessage = "ERROR";
}
System.out.println(" Test ReRan - " + testName + " - test class: " + testClass + ", status: " + statusMessage
+ ", trace: " + trace + ", expected: " + expected + ", actual: " + actual);
}
public synchronized void testTreeEntry(String description) {
System.out.println("Test Tree Entry - Description: " + description);
}
class WrapperTestCase extends TestCase {
public WrapperTestCase(String name) {
super(name);
}
public int countTestCases() {
return 1;
}
public void run(TestResult result) {
}
}
}
Upvotes: 0
Views: 1231
Reputation: 1318
I had to add a few dependencies to get the method to work for Eclipse Juno (4.4).
These 3 were the ones I added to resolve ITestRunListener2
, ISafeRunnable
and ISchedulingRule
, as well as the one recommended by the OP.
org.eclipse.jdt.junit.core_*.jar
org.eclipse.equinox.common_*.jar
org.eclipse.core.jobs_*.jar
org.eclipse.core.runtime_*.jar
Upvotes: 0
Reputation: 13858
The problem is that the plug-in providing the ITestRunListener2 interface does not export the corresponding package. For solution, you might want to look at the patches provided in the corresponding bug 140503 linked in the article, that contains an attachment with the title PDETestListener / Collector for 3.5 + 3.6. That might be what you need to run the test cases.
PS.: I am not sure whether this is a good idea, but I believe, for test (and build) automation it would be a good idea to use Buckminster or Maven/Tycho. Both are Eclipse projects, and provide a nice way to handle test automation - I like them both better then the use of Ant scripts.
Upvotes: 1