Eclipse Plug-in Test execution for JUnit4

eclipse, java, junit, junit4, unit-testing

Solution

I found that there are couple of issues. First I consider one by one to solve the issue.

Issue#1: No match is available for the required execution environment: J2SE-1.5

Issue#2: java.lang.ClassCastException: junit.framework.JUnit4TestAdapter cannot be cast to junit.framework.Test

Solution for Issue#1:

First solution:

- Right-click on your project

- Click Properties

- Click the "Java Compiler" option on the left menu

- Under JDK compliance section on the right, change it to "1.8"

Second solution:

If you use maven in your project, then you can change pom.xml file as below:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

    </pluginManagement>
</build>

Resource Link: Maven "build path specifies execution environment J2SE-1.5", even though I changed it to 1.7

Solution for Issue#2:

JUnit runner has a dependency on JUnit 3.8. It won't be used but without it, the whole platform can't be initialized. So you need 2 versions like the following

Check you have below plugins

org.eclipse.xtext.xbase.junit
org.junit (3.8.2)
org.junit (4.8.2)

How to check for JUnit?

In eclipse, please check in 2 sections.

- Check your Project `Properties->Java Build Path->Libraries (tab)`

- Check you Project's `Run Configurations->JUnit->Classpath (tab)`

How to fix?

To fix the error make sure you have org.junit 3.8 in the target platform!

All credit goes to A Paul.

Resource Link:

https://stackoverflow.com/a/21334162/2293534

Aaron Digulla has commented like below:

Despite the fact that org.eclipse.xtext.junit4 imports org.junit 4.5.0, org.eclipse.xtext.junit (note the missing "4" at the end) seems to have a dependency to JUnit 3.8. After adding the old, outdated JUnit bundle, the plugin tests started.

Problem

I have a `JUnit4` test suite and I want to execute it under the `JUnit Plug-in Test` run configuration. It passes successfully when running through the `JUnit Test` configuration, but for plug-in conf it fails in different ways. For example if I use `JUnit4TestAdapter` it fails with `ClassCastException`, and if I trying to run it only through the `@RunWith` annotation it wrotes "No methods found" error. For both implementations I use `JUnit4` Test Runner setting inside run configuration. I use - Eclipse Neon.1a Release (4.6.1) - Jdk 1.8 - linking JUnit 4.1 lib to the plugin. For first case it seems that Eclipse proceed to use the JUnit3 version when executing the suite. Here it is: ``` @RunWith(Suite.class) @SuiteClasses({ DndTest.class }) public class JSTestSuite { public static Test suite() { return new JUnit4TestAdapter(JSTestSuite.class); } } ``` And exception is: ``` java.lang.ClassCastException: junit.framework.JUnit4TestAdapter cannot be cast to junit.framework.Test at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.getTest(RemoteTestRunner.java:403) ``` While starting the test I have a strange log message in console: ``` !ENTRY org.eclipse.update.configurator 2017-05-04 17:58:57.279 !MESSAGE Could not install bundle ../../platform/eclipse/plugins/org.eclipse.jdt.junit4.runtime_1.1.600.v20160505-0715.jar No match is available for the required execution environment: J2SE-1.5 ``` I see this lib is on the place, but I can't understand why it failing to be loaded. For `JUnit3` Test Runner setting junit3 lib is loaded ok. There are some bugs related to such issues (like this) but it is really hard to understand what can I do in this case. For second case I just try to execute simple `JUnit4` case without using the `JUnit4TestAdapter`, but it can't find any methods. Reloading of eclipse and renaming of the methods didn't help. What can I do in this case?

Original source

Related problems