Enjoy JUnit 5 in Eclipse before official release

Overview

The Java testing framework JUnit will release a major version of 5 this year. At a conference called Eclipse Converge 2017 held in San Jose the other day, I asked how to use this JUnit 5 with Eclipse, so my personal PC for Windows 10 I tried it with.

JUnit 5 This is the fifth major version of JUnit. It has been reworked to support the new language specifications for Java 8 and 9.

New language specifications that JUnit 4 couldn't cover

reference

For new features and precautions in JUnit 5, there are already many documents in Japanese, so please refer to them.

-"Java Magazine vol.30" Special Article "First JUnit 5"

environment

JUnit 5 will be beta-supported from Milestone 6 in Eclipse 4.7.0 (Oxygen). You can download it from the following.

https://eclipse.org/downloads/packages/release/Oxygen/M6

As of April 05, 2017, when this article is written, there seems to be no 4.7.x Japanese localization plugin.

http://mergedoc.osdn.jp/

Other environments are as follows.

OS Windows 10
Java SE 1.8.0_121
Eclipse 4.7.0-M6
git 2.12.2.windows.1

Introduction

Create an Eclipse plugin for JUnit 5 from the source code and install it on your Eclipse (4.7 series).

Follow the steps at https://wiki.eclipse.org/JDT_UI/JUnit_5. You need git to download the file. Of course you also need Eclipse. I don't think there is any problem if Eclipse is downloaded from Java. If you have downloaded "for Testers", please install the plug-in development tool separately. Plugins are built in Eclipse, so you don't need Ant, Maven or Gradle.

Preparing the workspace for plugin development

It's not required, but well, you don't want to mix it with your regular development workspace, so it's a good idea to have one.

Check out https://github.com/noopur2507/eclipse-junit5.git

You don't need to build any files, so just download https://github.com/noopur2507/eclipse-junit5/archive/master.zip. In that case, the file eclipse-junit5-master.zip will be downloaded. Please import & place the contents of this zip file as follows.

--Import org.eclipse.jdt.junit5.runtime into the plugin development workspace --JUnit-5-Place JARs jars in dropins

Place JUnit-5-JARs jars in dropins

ju5_1.png

ju5_9.png

Clone git: //git.eclipse.org/gitroot/jdt/eclipse.jdt.ui.git

Please clone using git. No key setting is required. It takes more than 10 minutes to download all of them.

$ git clone git://git.eclipse.org/gitroot/jdt/eclipse.jdt.ui.git
Cloning into 'eclipse.jdt.ui'...
remote: warning: ignoring extra bitmap file: objects/pack/pack-dffcdea022429c2393e25dfe14e4b833fb5d7dd0.pack
remote: Counting objects: 474929, done.
remote: Compressing objects: 100% (715/715), done.

remote: Compressing objects: 100% (715/715), done.
remote: Total 474929 (delta 364), reused 0 (delta 0)
Receiving objects: 100% (474929/474929), 73.43 MiB | 125.00 KiB/s, done.
Resolving deltas: 100% (249502/249502), done.
Checking out files: 100% (12727/12727), done.

Please move after cloning.

$ cd eclipse.jdt.ui

Switch to BETA_JUNIT5 branch

eclipse.jdt.ui $ git checkout -b BETA_JUNIT5
Switched to a new branch 'BETA_JUNIT5'

Pull BETA_JUNIT5 from origin

eclipse.jdt.ui $ git pull origin BETA_JUNIT5
From git://git.eclipse.org/gitroot/jdt/eclipse.jdt.ui
 * branch                  BETA_JUNIT5 -> FETCH_HEAD
Auto-merging org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/JunitPreferenceInitializer.java
Merge made by the 'recursive' strategy.
 org.eclipse.jdt.junit.core/plugin.properties       |   3 +-
 org.eclipse.jdt.junit.core/plugin.xml              |  13 +-
 .../jdt/internal/junit/JUnitCorePlugin.java        |   5 +-
 .../eclipse/jdt/internal/junit/JUnitMessages.java  |  10 +-
 .../jdt/internal/junit/JUnitMessages.properties    |   6 +-
 .../internal/junit/JUnitPreferencesConstants.java  |  38 ++-
 .../internal/junit/JunitPreferenceInitializer.java |  24 +-
 .../internal/junit/buildpath/BuildPathSupport.java |  90 ++++++-
 .../junit/buildpath/JUnitContainerInitializer.java |  59 ++++-
 .../internal/junit/launcher/JUnit5TestFinder.java  | 286 ++++++++++++++++++++
 .../JUnitLaunchConfigurationConstants.java         |   5 +-
 .../internal/junit/launcher/TestKindRegistry.java  |  12 +-
 .../internal/junit/model/ITestRunListener2.java    |  16 +-
 .../eclipse/jdt/internal/junit/model/IXMLTags.java |  16 +-
 .../junit/model/RemoteTestRunnerClient.java        |   2 +-
 .../jdt/internal/junit/model/TestCaseElement.java  |  12 +-
 .../jdt/internal/junit/model/TestElement.java      |  55 +++-
 .../eclipse/jdt/internal/junit/model/TestRoot.java |   4 +-
 .../jdt/internal/junit/model/TestRunHandler.java   |  14 +-
 .../jdt/internal/junit/model/TestRunSession.java   | 107 ++++++--
 .../junit/model/TestRunSessionSerializer.java      |  28 +-
 .../jdt/internal/junit/model/TestSuiteElement.java |  12 +-
 .../internal/junit/util/CoreTestSearchEngine.java  |  60 ++++-
 .../src/org/eclipse/jdt/junit/JUnitCore.java       |  10 +-
 .../launcher/JUnitLaunchConfigurationDelegate.java | 107 ++++++--
 org.eclipse.jdt.junit.runtime/META-INF/MANIFEST.MF |   8 +-
 .../junit/runner/FirstRunExecutionListener.java    |   4 +-
 .../jdt/internal/junit/runner/ITestIdentifier.java |   5 +-
 .../jdt/internal/junit/runner/ITestLoader.java     |   5 +-
 .../internal/junit/runner/IVisitsTestTrees.java    |   4 +-
 .../jdt/internal/junit/runner/MessageIds.java      |  24 +-
 .../internal/junit/runner/RemoteTestRunner.java    |  81 ++++--
 .../jdt/internal/junit/runner/TestIdMap.java       |   2 +-
 .../junit/runner/TestReferenceFailure.java         |   7 +-
 .../junit/runner/junit3/JUnit3Identifier.java      |  10 +-
 .../junit/runner/junit3/JUnit3TestLoader.java      |   4 +-
 .../junit/runner/junit3/JUnit3TestReference.java   |  22 +-
 org.eclipse.jdt.junit/plugin.xml                   |  11 +-
 .../junit/buildpath/JUnitContainerWizardPage.java  |  19 +-
 .../junit/ui/JUnitClasspathFixProcessor.java       |  38 ++-
 .../jdt/internal/junit/ui/JUnitMessages.java       |  12 +-
 .../jdt/internal/junit/ui/JUnitMessages.properties |  10 +-
 .../eclipse/jdt/internal/junit/ui/JUnitPlugin.java |  53 +++-
 .../internal/junit/ui/JUnitQuickFixProcessor.java  |  47 +++-
 .../junit/ui/JUnitUIPreferencesConstants.java      |  18 +-
 .../jdt/internal/junit/ui/OpenTestAction.java      |  30 ++-
 .../jdt/internal/junit/ui/TestRunnerViewPart.java  |  21 +-
 .../junit/ui/TestSessionLabelProvider.java         |  45 ++--
 .../eclipse/jdt/internal/junit/ui/TestViewer.java  | 152 ++++++++---
 .../jdt/internal/junit/util/JUnitStubUtility.java  |   8 +
 .../junit/wizards/NewTestCaseCreationWizard.java   |  69 +++--
 .../junit/wizards/SuiteClassesContentProvider.java |   4 +-
 .../jdt/internal/junit/wizards/WizardMessages.java |   7 +-
 .../junit/wizards/WizardMessages.properties        |   9 +-
 .../launcher/JUnitLaunchConfigurationTab.java      |  27 +-
 .../jdt/junit/launcher/JUnitLaunchShortcut.java    |  48 ++--
 .../junit/wizards/NewTestCaseWizardPageOne.java    | 290 ++++++++++++++++-----
 .../jdt/junit/wizards/NewTestSuiteWizardPage.java  |   4 +-
 .../internal/junit4/runner/JUnit4Identifier.java   |  10 +-
 .../internal/junit4/runner/JUnit4TestLoader.java   |   3 +-
 .../junit4/runner/JUnit4TestReference.java         |   6 +-
 .../corext/codemanipulation/StubUtility.java       |   2 +-
 .../templates/default-templates.properties         |   4 +-
 org.eclipse.jdt.ui/templates/default-templates.xml |  15 +-
 64 files changed, 1728 insertions(+), 404 deletions(-)
 create mode 100644 org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/launcher/JUnit5TestFinder.java

This code also does not need to be built with a build tool such as Maven. From the updated folders, import the following 5 into the plugin development workspace.

  1. org.eclipse.jdt.ui
  2. org.eclipse.jdt.junit
  3. org.eclipse.jdt.junit.core
  4. org.eclipse.jdt.junit.runtime
  5. org.eclipse.jdt.junit4.runtime

ju5_4.png ju5_5.png ju5_6.png ju5_8.png ju5_7.png

Export and install plugins

Select "Deployable plug-ins and fragments" from "File"-> "Export"-> "Plug-in Development" from the Eclipse menu bar. Of course, if you don't have a plugin development tool, you won't have a choice. Be careful if you have the Test Developer version.

ju5_10.png

ju5_11.png

If the "Install into host. Repository:" under "Destination" checkbox is checked, the plugin will continue to be installed as soon as the plugin has been exported. After the installation is complete, Eclipse will ask you to restart it, so let's restart it.

On Windows, if you have Eclipse on your C drive and the launcher disappears, download Eclipse again and copy only the exe file to the eclipse folder.

Verification

Create a new Java project, open the build path settings and select JUnit in Add Library. If the plugin has been successfully exported and installed, JUnit 5 should have been added to the dialog choices that appear here.

ju5_12.png

You are now ready to develop JUnit 5 on Eclipse.


Enjoy JUnit 5

Now, let's try writing and running JUnit 5 code. This time I'll write code to try out the new JUnit 5 feature Group assertion.

JUnit_5_Example


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class BaseTest {

	@Test
	public void test() {
		Assertions.assertEquals(Integer.valueOf(1).toString(), "1");
		Assertions.assertAll(
				() -> Assertions.assertEquals(Integer.valueOf(1).toString(), "1"),
				() -> Assertions.assertEquals(Integer.valueOf(1).toString(), "2"),
				() -> Assertions.assertEquals(Integer.valueOf(3).toString(), "3"),
				() -> System.out.println("Hello JUnit5!")
				);
	}
}

It works the same as up to JUnit 4.

ju5_13.png

Yes, the test fails because the second Assertion fails. What you should pay attention to here is that "Hello JUnit 5!" Is output to the console. In traditional JUnit, if an assertion fails within a single test method, no further processing is done. In JUnit 5 Group assertion, the processing within that group is always executed.

You have now confirmed that the JUnit 5 plugin you exported this time works properly. It took about 3 hours so far.


Summary

It's still quite a hassle, so if you want to get JUnit 5 into your project for business, wait until Eclipse officially supports JUnit 5 or use IntelliJ, unless you have a good reason to do so ...

Alternatively, it's best to use it via the Gradle or Maven plugins.


reference

JUnit 5 User Guide http://junit.org/junit5/docs/current/user-guide/

Eclipse Converge 2017 session "Embracing JUnit 5 with Eclipse"

--Session introduction: Embracing JUnit 5 with Eclipse --Material (PDF): Embracing JUnit 5 with Eclipse

JUnit 5 sample project in Gradle

https://github.com/junit-team/junit5-samples/tree/r5.0.0-M4/junit5-gradle-consumer

JUnit 5 sample project in Maven

https://github.com/junit-team/junit5-samples/tree/r5.0.0-M4/junit5-maven-consumer

Recommended Posts

Enjoy JUnit 5 in Eclipse before official release
How to run JUnit in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
MVC in Eclipse.
Refactoring in JUnit
JUnit 5 becomes java.lang.NoClassDefFoundError: org / junit / platform / engine / EngineDiscoveryListener in Eclipse
To debug in eclipse