Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

How To Run JUnit Tests In Jupiter? [JUnit Jupiter Tutorial]

Shalini Baskaran

Posted On: March 16, 2022

view count331816 Views

Read time12 Min Read

JUnit Jupiter is a perfect blend of the JUnit 5 programming model and extension model for writing tests and extensions. The Jupiter sub-project provides a TestEngine for running Jupiter-based tests on the platform. It also defines the TestEngine API for developing new testing frameworks that run on the platform.

The Jupiter programming model is derived from JUnit 4’s annotations and assumptions about how you structure your test code. So, if you are familiar with JUnit 4, then it will be pretty simple to grasp Jupiter concepts.

With this article on JUnit Jupiter, let’s explore the basic usage of Jupiter, its unit testing capabilities, and how you can run JUnit tests in Jupiter.

What is JUnit?

JUnit is one of the most powerful unit testing frameworks that has been widely used for carrying out unit tests. JUnit 5 is the latest version of JUnit consisting of three different sub-components, namely JUnit Platform, JUnit Jupiter, and JUnit Vintage. JUnit 5 requires Java version 8 or any other higher version, which makes it stand out from its previous versions. But surprisingly, the unit tests written in JUnit4 or previous versions can still be compiled and tested with JUnit 5. Now let us understand the three sub-components of JUnit 5.

Also ReadHow to execute JUnit 4 tests with JUnit 5 [Tutorial]

JUnit 5 – The next generation testing framework

JUnit 5 is the latest, modular and highly extensible version of JUnit. The JUnit platform is the basic foundation of the testing framework, which helps in framework development. The JUnit framework is made up of three modules:

  • JUnit Platform provides a clean and functional API for managing, executing, and reporting the results of tests. Tools built with the JUnit Platform can rely on the JUnit tooling ecosystem to discover, run, report, and diagnose test execution.
  • JUnit Jupiter is used for writing the tests and the Jupiter sub-project provides a TestEngine for running Jupiter-based tests on the platform.
  • JUnit Vintage is used for running earlier versions of JUnit tests such as JUnit 3 and JUnit 4 based tests.

JUnit 5 Architecture

JUnit 5 Architecture

Annotations used in JUnit 5

JUnit annotations are required while performing Selenium automation testing with JUnit. So, basically, annotations are the meta-tags that provide additional information about the methods and classes defined in our code structure. Below are some of the annotations that are used in JUnit 5.

  • @Test – This annotation is used for declaring a test.
  • @TestFactory – This annotation is used for defining a method which is a test factory for dynamic tests which are generated at the runtime
  • @RepeatedTest – This annotation is used to specify that the method is a template for the tests that can be repeated a specific number of times.
  • @ParameterizedTest – This annotation is used to indicate that the method is a parameterized test. These parameterized tests are similar to normal test methods, but we have to specify a source to provide parameters for each invocation used in the test.
  • @TestMethodOrder – This annotation is used to define the order of the test execution.
  • @DisplayName – This annotation is used to specify a customized display name for the method or class.
  • @Tag – This annotation is used for filtering the tests at the method or class level by defining the tags.
  • @Disabled – This annotation is used to disable a test method or class.
  • @BeforeEach – This annotation is used to specify that the specific test method has to be executed before each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method.
  • @AfterEach – This annotation is used to specify that the specific test method has to be executed after each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method.
  • @BeforeAll – This annotation is used to specify that the specific test method has to be executed before all @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method.
  • @AfterAll – This annotation is used to specify that the specific test method has to be executed after all @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method.

To understand the difference between JUnit 5 and JUnit 4, please refer to our article on executing JUnit 4 test with JUnit 5. Now let us see how to create simple test cases in JUnit 5 and run them using Jupiter.

JUnit Certification CTA

LambdaTest offers free JUnit certification for Java developers that can be helpful to accelerate your career in Java development and testing.

Here’s a glimpse of the JUnit Certification offered by LambdaTest:

Writing simple JUnit 5 test cases using Jupiter

Prior to writing the test cases, let us quickly understand the scenarios and steps to automate the tests. We will be using the https://www.lambdatest.com website for writing our test cases.

Scenario 1: Launch LambdaTest website and verify the tabs on the main page:

  1. Navigate to https://www.lambdatest.com website.
  2. Verify the tabs like Automation, Pricing, Resources, etc., are displayed on the main page or not.

Scenario 2: Login into the LambdaTest website and verify whether the user could successfully navigate to the LT Browser page:

  1. Navigate to https://www.lambdatest.com website.
  2. Enter the valid credentials.
  3. Upon successful login, verify if the user is able to navigate to the LT Browser page.

Scenario 3: Verify if the user is able to navigate to his profile page

  1. Navigate to https://www.lambdatest.com website.
  2. Login to the website using valid credentials.
  3. Verify if the user is able to navigate to his profile.

Scenario 4: Verify the list of options available under the Resources tab:

  1. Navigate to https://www.lambdatest.com website.
  2. Under the Resources tab, verify if the list of options is displayed or not.

To write and run your JUnit 5 tests cases using Jupiter, make sure that you have added the below dependencies in the pom.xml file.

Pom.xml

Below is the automation test script covering all the above-mentioned scenarios.

In the above script, you could have noticed some JUnit 5 annotations. Let us understand them one by one.

  • @DisplayName – This annotation is used to specify a customized display name for the method or class.
  • @Tag – This annotation is used for filtering the tests at the method or class level by defining the tags.
  • @Order – This annotation is used for providing the sequence or order in which the tests have to be executed.

To execute the JUnit Tests, right-click on the class and click Run.

Console Output:

The test cases would be run in the respective order that has been provided.

Writing simple JUnit 5 test cases using Jupiter Console Output

Running JUnit 5 tests with specific tags

In our test script, we have used tags for grouping the test cases. For example, during the process of testing, there would be a need to run only smoke or sanity cases. In such cases, we can group the tests using tags and execute them. To run the test cases specific to a tag,

  1. Right-click on your test class.
  2. Click Edit ‘your test class name.’
  3. In the Edit Run Configuration dialog box, select Test Kind as Tags and enter the tag value in the Tag expression field.
  4. Now Run your tests.

Running JUnit 5 tests with specific tags

Console Output:

In the console, you could see the execution of the tests specific to the tags.

Running JUnit 5 tests with specific tags Console Output

Also readMastering Selenium Testing: JUnit Asserts With Examples

Writing JUnit 5 tests with composed annotation

Annotations in JUnit Jupiter can be used as meta-annotations. This allows you to create a custom composed annotation that will automatically inherit the semantics of its meta-annotations. For example, you can use @Test and @Tag(“Smoke”) annotations as meta-annotations and create a composed annotation named @SmokeTest which can be used as a replacement for the other two annotations on tests.

Console Output:

The tests marked with composed annotation have been executed.

Writing JUnit 5 tests with composed annotation Console output

Writing test cases repeatedly in JUnit5

To run our JUnit tests repeated a certain number of times, JUnit 5 provides an annotation @RepeatedTest. Using the RepeatedTest class, you can write a test method that will repeat execution with different input data over and over again. This class gets used when your tests need to be repeated multiple times with data, such as when running performance profiling or simulating everyday use of your app.

Console Output:

Writing test cases repeatedly in JUnit5 Console Output

Also readHow To Use @RepeatedTest Annotation In JUnit 5

Running JUnit 5 tests using Jupiter

To run the same JUnit test with multiple inputs, we can use @ParameterizedTest annotation in JUnit 5. While parameterizing the values, the test has to be annotated with @ParameterizedTest instead of @Test.

Scenario: Verify the Login functionality of the website using different sets of credentials to cover valid and negative cases.

For parameterizing the tests, we have to add the below maven dependency in the pom.xml file.

Pom.xml

Test script for parameterizing our tests.

Running JUnit 5 tests in LambdaTest Grid

Using Selenium along with JUnit is a good option due to its capabilities to run tests on multiple browsers and platforms, providing flexibility in choosing any third-party language.

LambdaTest provides an online cloud platform to perform Selenium automation testing with ease at scale. Automation testing tools like LambdaTest allows you to perform cross browser testing on an online device farm of over 3000+ real devices running real operating systems.

Once you are logged into the website you will be provided with a username and an access key which can be used to run your scripts in the cloud.

Below is the code snippet for executing the JUnit 5 tests using Jupiter in the LambdaTest platform.

To run this test in the LambdaTest platform:

  1. Right-click on the test class that you have created in the IDE.
  2. Click Run. Now the test will be started on the LambdaTest platform.
  3. Once your tests are executed, navigate to your LambdaTest Account in the browser and click Dashboard in the left-hand side panel. This page shows an overview of the tests that are run on the platform.
  4. Next, click the Automation label in the left-hand side panel. This is the place where you have to identify your executed tests. It contains multiple tests, and to identify the specific test, you have to search using the build name that you have provided in the script.
  5. In the code above, we have named the build as ” Running_Junit5Tests_In_Grid” and now search for that build. This page also shows the status of your build and the time taken for execution.
  6. Running_Junit5Tests_In_Grid

  7. Click the build to view the tests that were run. You can see the test “JUnit5Tests”, which was provided in the script, and this is how we identify our tests in LambdaTest. We will see four tests run in the grid as written in our script. Here you can verify all the test details that have been provided in the script, like the browser, browser version, operating system, and version in which the test was executed. This would be the same as provided in the test script.
  8. As we have enabled video recording in the code, you will see a video attached to the test on this page, which has recorded all the test steps while executing the test.

video attached to the test on this page

By playing the video, you can view how the tests were executed. In case of failure, this would help analyze the point where the failure has occurred. We can also have a quick view of the tests on the Meta tab.

quick view of the tests

To see the steps executed in a test, click a Test and go to the COMMAND tab. This displays the list of steps that are executed as part of the test.

list of steps that are executed as part of the test

If the test doesn’t behave as per the expectation as a result of which failure occurs in any step, it can be directly marked as a bug by clicking the Mark Bug icon in the test step.

Console Output:

Running JUnit 5 tests in LambdaTest Grid Console Output

LambdaTest also allows users to get a clear view of the quality and performance of their applications by tracking the health of test cases through LambdaTest Analytics. Visit Analytics under the Automation page to see your failed tests, download a report, and track test progress.

Analytics under the Automation page

You can go through the following video on JUnit Tutorial for beginners series, to run JUnit tests in parallel in the Selenium cloud using multiple browsers and platforms. The video demonstrates cross-browser testing with JUnit using the LambdaTest platform. By the end of this tutorial video, you will be in a position to run JUnit tests in parallel across different browsers and platforms.

You can also Subscribe to the LambdaTest YouTube Channel and stay updated with the latest tutorials around Selenium testing, Cypress testing, CI/CD, and more.

Finally!

We have reached the end of the article on JUnit Jupiter, and it is time to summarize our understanding here. So far, we have seen a quick introduction to the JUnit framework and the latest version of it, JUnit 5. We have seen how to frame our tests using JUnit 5, the various annotations used in JUnit 5, and the most important part, the execution of JUnit 5 tests using Jupiter for automated browser testing. Now it is time for you to try and explore. I hope you all find this article informative and helpful. I would love to hear your comments on this. Until then, Happy Testing…!

Frequently Asked Questions (FAQs)

What is JUnit Jupiter?

JUnit Jupiter is the next generation testing platform for writing tests in Java 8 and beyond. It provides extension features to unit test base classes, which was not possible in JUnit 3 and JUnit 4.

Is JUnit Jupiter the same as JUnit 5?

The JUnit 4 architecture embraced a monolithic design, where all components of the framework were contained internally within the bundle. The JUnit 5 iteration is based on a separation of concerns and provides APIs to tool developers (Platform) or to users (Jupiter).

Author’s Profile

Shalini Baskaran

Shalini works as a Technical Writer at LambdaTest. She loves to explore recent trends in test automation. Other than test automation, Shalini is always up for travel & adventure.

Blogs: 23



linkedintwitter

Test your websites, web-apps or mobile apps seamlessly with LambdaTest.

  • Selenium, Cypress, Playwright & Puppeteer Testing
  • Real Devices Cloud
  • Native App Testing
  • Appium Testing
  • Live Interactive Testing
  • Smart Visual UI Testing
Book a Demo