pitest / pitclipse

Mutation testing for Java in Eclipse IDE. Based on PIT (Pitest).
https://pitest.org
Apache License 2.0
59 stars 17 forks source link

Alternative mechanism for detecting JUnit5 #149

Closed LorenzoBettini closed 2 years ago

LorenzoBettini commented 3 years ago

As noted in org.pitest.pitclipse.launch.AbstractPitLaunchDelegate.isJUnit5InClasspathOf(IJavaProject) we use a naive mechanism for detecting JUnit5 on the classpath.

We could try to load some classes of the JUnit 5 engine by constructing a classloader using the classpath of the Java project. Something like (untested)

String[] classPathEntries = JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
List<URL> urlList = new ArrayList<URL>();
for (int i = 0; i < classPathEntries.length; i++) {
    String entry = classPathEntries[i];
    IPath path = new Path(entry);
    URL url;
    try {
        url = path.toFile().toURI().toURL();
        urlList.add(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}
URL[] urls = (URL[]) urlList.toArray(new URL[urlList.size()]);
URLClassLoader classLoader = new URLClassLoader(urls, parentClassLoader);
LorenzoBettini commented 2 years ago

Actually, it's much easier:

    public static boolean onClassPathOf(IJavaProject project, String fullyQualifiedName) throws CoreException {
        return project.findType(fullyQualifiedName) != null;
    }
LorenzoBettini commented 2 years ago

@echebbi I hope you don't mind I'm merging these PRs