Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding a non param test in parameterized test class

Is there any annotation in JUnit to exclude a non param test in parameterized test class?

like image 828
user405516 Avatar asked Jul 29 '10 09:07

user405516


People also ask

How do you pass a null in a parameterized test?

1. Solution – @NullSource. Since JUnit 5.4 and above, we can use the @NullSource to pass a null value to the parameterized tests.

What is true about parameterized test class?

Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized. class).

Which annotation is used on a class to perform parameterized test?

Parameterized test make use of @RunWith annotation along with @Parameters annotations to feed inputs.

Which annotation must be carried by a parameterized test?

A parameterised test class must carry which annotation? Explanation: A class that is annotated with @RunWith or extends a class that is annotated with @RunWith, JUnit will invoke the class it has referenced to run the tests in that class instead of the runner that is built into JUnit.


3 Answers

JUnit 5

As of Junit 5.0.0 you can now annotate your test methods with @ParameterizedTest. So no need for inner classes. There are many ways to supply the arguments to the parameterized test apart from ValueSource as shown below. See the official junit user guide for details:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class ComponentTest {

    @ParameterizedTest
    @ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
    public void testCaseUsingParams(String candidate) throws Exception {
    }

    @Test
    public void testCaseWithoutParams() throws Exception {
    }
}

JUnit 4

If you are still using Junit 4 (I tested with v4.8.2) you can use the Enclosed runner in conjunction with inner classes and the Parameterized runner:

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Enclosed.class)
public class ComponentTest {

    @RunWith(Parameterized.class)
    public static class ComponentParamTests {

        @Parameters
        ...

        @Test
        public void testCaseUsingParams() throws Exception {
        }
    }

    public static class ComponentSingleTests {

        @Test
        public void testCaseWithoutParams() throws Exception {
        }
    }
}
like image 140
Matthew Madson Avatar answered Sep 28 '22 06:09

Matthew Madson


No. The best practice is to move those non-parameterized tests to a different class (.java file)

like image 24
Jeanne Boyarsky Avatar answered Sep 27 '22 06:09

Jeanne Boyarsky


Zohhak test runner is a simpler way to parameterize specific tests. Thanks Piotr!

like image 3
AmanicA Avatar answered Sep 28 '22 06:09

AmanicA