Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import JunitParamsRunner in Java class

it is my first day with JUnit. I try to make test with parameters. I have code:

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

import static org.junit.Assert.assertEquals;

@RunWith(JUnitParamsRunner.class)
public class MoneyParameterizedTest {
    private static final Object[] getMoney() {
        return new Object[] {
                new Object[] {10, "USD"},
                new Object[] {20, "EUR"}
        };
    }
    @Test
    @Parameterized.Parameters(method = "getMoney")
    public void constructorShouldSetAmountAndCurrency(
            int amount, String currency) {
        Money money = new Money(amount, currency);
        assertEquals(amount, money.getAmount());
        assertEquals(currency, money.getCurrency());
    }
}

IntelliJ told me that: Can't resolve symbol JUnitParamsRunner and method. Is it problem with import? My class which I'm testing is in the same package.

----- EDIT -------

I change JunitParamsrunner.class to Parameterized.class and it's okay but problem with symbol 'method' in Parametrized.Parameters is the same.

like image 225
J. Carrer Avatar asked Sep 07 '16 09:09

J. Carrer


2 Answers

JUnitParams (licensed Apache 2.0) is a separate library, which means it's not shipped with JUnit itself. This also means that you need to make sure it's in the classpath of your project. If you're using maven (or something similar) then it's rather easy, you just need to add it as a dependency in your pom and make sure IJ has picked up the changes (wither automatically or manually if it shows a pop-up in the upper right corner):

<dependency>
  <groupId>pl.pragmatists</groupId>
  <artifactId>JUnitParams</artifactId>
  <version>1.0.5</version>
  <scope>test</scope>
</dependency>

Otherwise, download it yourself (click the Download ( JAR ) link) and the library it to your classpath.

Please note that, although the concepts are similar, Parameterized is not the same thing with JUnitParams, the latter trying to simplify and improve the way you can write parametrized JUnit tests.

P.S.: There's another library called Zohhak which seems even more flexible than JUnitParams but it's released under LGPL 3.0 so it depends on your license restrictions.

like image 71
Morfic Avatar answered Oct 09 '22 04:10

Morfic


Here's an example that should work:

import junitparams.JUnitParamsRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
//import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertEquals;
import junitparams.Parameters;

@RunWith(JUnitParamsRunner.class)
public class MoneyTest {

    private final static int VALID_AMOUNT = 5;
    private final static String VALID_CURRENCY = "USD";

    private static final Object[] getInvalidAmount() {
        return new Integer[][] {{-12387}, {-5}, {-1}};
    }

    @Test(expected = IllegalArgumentException.class)
    @Parameters(method = "getInvalidAmount")
    public void constructorShouldThrowIAEForInvalidAmount(int invalidAmount){
        new Money(invalidAmount, VALID_CURRENCY);
    }

    private static final Object[] getInvalidCurrency(){
        return new String[][]{{null}, {""}};
    }

    @Test(expected = IllegalArgumentException.class)
    @Parameters(method = "getInvalidCurrency")
    public void constructorShouldThrowIAEForInvalidCurrency(String invalidCurrency){
        new Money(VALID_AMOUNT, invalidCurrency);
    }

    private static final Object[] getMoney() {
        return new Object[] {
                new Object[] {10, "USD"},
                new Object[] {20, "EUR"}
        };
    }
    @Test
    @Parameters(method = "getMoney")
    public void constructorShouldSetAmountAndCurrency(
            int amount, String currency) {
        Money money = new Money(amount, currency);
        assertEquals(amount, money.getAmount());
        assertEquals(currency, money.getCurrency());
    }
}
like image 1
muninn Avatar answered Oct 09 '22 04:10

muninn