Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing org.junit.jupiter.params from JUnit5

I am trying to add parameterized tests into my Java program. I found the examples for JUnit 5, which I do have included.

https://blog.codefx.org/libraries/junit-5-parameterized-tests/

The issue is I cannot add @ParameterizedTest because the namespace is missing. Idk why or how.

The documentation page clearly states it is in org.junit.jupiter.params, but I do not have that.

To give you an idea of my code:

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.jupiter.api.Assertions.*;

class SubsetPrinterTest
{
    // https://blog.codefx.org/libraries/junit-5-parameterized-tests/

    static Collection<Object[]> makeSetData()
    {
        return Arrays.asList(new Object[][]
        {
                {1, new char[]{'1'}},
                {2, new char[]{'1', '2'}},
                {3, new char[]{'1', '2', '3'}},
                {4, new char[]{'1', '2', '3', '4'}},
                {5, new char[]{'1', '2', '3', '4', '5'}}
        });
    }

    // This should be a parameterized test using the makeSetData.
    @Test
    void makeSet()
    {
        // Arrange
        SubsetPrinter subsetPrinter = new SubsetPrinter();

        // Act
        char[] set = SubsetPrinter.MakeSet(5);

        // Assert
        assertArrayEquals(set, new char[]{'1', '2', '3', '4', '5'});
        assertEquals(set.length, 5);
    }
}
like image 622
AzzamAziz Avatar asked Sep 19 '17 02:09

AzzamAziz


People also ask

Is Jupiter a junit5?

JUnit Jupiter is the combination of the programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform.

How do you write parameterized test cases in junit5?

Writing Our First Parameterized TestsAdd a new test method to our test class and ensure that this method takes a String object as a method parameter. Configure the display name of the test method. Annotate the test method with the @ParameterizedTest annotation. This annotation identifies parameterized test methods.

Why your JUnit 5 tests are not running under maven?

Prior to these releases, to run JUnit 5 tests under Maven, you needed to include a JUnit provider dependency for the Maven Surefire plugin. This is correct for pre 2.22. 0 releases of Maven Surefire/Failsafe.

Which type of JUnit 4 rule is supported in JUnit 5?

In JUnit 4, we used the @Rule and @ClassRule annotations to add special functionality to tests. In JUnit 5. we can reproduce the same logic using the @ExtendWith annotation.


2 Answers

You project class-path has to include a version of junit-jupiter-params-xxx.jar, like junit-jupiter-params-5.0.0.jar from http://central.maven.org/maven2/org/junit/jupiter/junit-jupiter-params/5.0.0/

The blog post from codefx.org you link to says (edited to the current 5.0.0 release):

Getting started with parameterized tests is pretty easy but before the fun can begin you have to add the following dependency to your project:

Group ID: org.junit.jupiter
Artifact ID: junit-jupiter-params
Version: 5.0.0

Either download and add it manually, or if you're using a build tool with dependency management (Gradle, Maven, ...) configure the build script (build.gradle, pom.xml, ...) accordingly.

Find some generic samples here: https://github.com/junit-team/junit5-samples

Starting with version 5.4.0-M1 JUnit Jupiter provides an aggregator artifact that bundles all available Jupiter-defining artifacts for easy consumption. See https://sormuras.github.io/blog/2018-12-26-junit-jupiter-aggregator.html for details.

like image 68
Sormuras Avatar answered Oct 14 '22 13:10

Sormuras


Add the following dependency in pom.xml. jupiter API [Junit 5] approaches modules as plugins, each of on has to be deliberately added,

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-params</artifactId>
  <version>${junit.version}</version>
  <scope>test</scope>
</dependency>

More on: https://mvnrepository.com/artifact/org.junit.jupiter

like image 23
Sankarganesh Eswaran Avatar answered Oct 14 '22 12:10

Sankarganesh Eswaran