JavaCodingNerd

Java Coding Nerd. This Blog will feature content-rich articles, tutorials and own opinions on variuos programming related topcis.

Testing

Parameterized JUnit tests with JUnitParams

JunitParams

JunitParams

In this Post we will learn how we can make use of JUnitParams to get the most out of JUnit tests. It allows you to run one test with a number of different parameters.

What you need

  • A simple Maven Project
  • JUnit 4
  • JUnitParams

Add the JUnit 4 and JUnitParams dependency to your pom.xml

<dependencies>
  <!-- https://mvnrepository.com/artifact/junit/junit -->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>
  <!-- https://mvnrepository.com/artifact/pl.pragmatists/JUnitParams -->
  <dependency>
    <groupId>pl.pragmatists</groupId>
    <artifactId>JUnitParams</artifactId>
    <version>1.0.4</version>
    <scope>test</scope>
  </dependency>
</dependencies>
  •        Animal.java this is the Object we want to test

in src/main/java   create a class named Animal. This class is representing a simple model of an Animal. There is nothing to complicated about it so you can go straight forward an copy/paste it.

public class Animal {

  private int age;
  private boolean isFlying;
  
  public Animal(int age, boolean isFlying) {

    this.age = age;
    this.isFlying = isFlying;
  }
  
  public Animal(){
    this(5,false);
  }
  
  public int getAge() {
    return age;
  }
  
  public boolean isFlying() {
    return isFlying;
  }
  

}

Writing a simple JUnit Test

Before we will use JUnitParams we are going to write a simple JUnit Test that creates a Animal Object and tests wether the properties age and isFlying are set correctly. To do this create a class called AnimalTest in src/test/java.

public class AnimalTest {
  
  @Test	
  public void constructorShouldSetWeightAndIsFlying() {

    Animal testAnimal = new Animal(5, false);
    assertEquals(5, testAnimal.getAge());
    assertEquals(false, testAnimal.isFlying());

  }
    
}

To run the test click right on the AnimalTest class and select Run As -> JUnit Test or use the Eclipse shortcut (alt +shift +x ) +t. You have to release the keys in brackets before you press t. Also you need to be in AnimalTest.
The test case is green and we are happy.

But what if we want to test the constructor with other parameters? We would have to create a new Animal Object for every Test. That would lead to a lot of redundant code. And we all know the DRY principle. DONT REPEAT YOURSELF.

This is where JUnitParams comes into play.

Using JUnitParams to write parameterized tests

The first thing we need to do ist tell our test class it will use JUnitParams (make sure you did add the dependecy in the pom.xml). To do so add this to our code

@RunWith(JUnitParamsRunner.class)
public class AnimalTest {
//
}

The next thing on our List is to make an Array of Objects, which will be used as the Parameter of our test.

@RunWith(JUnitParamsRunner.class)
public class AnimalTest {

  //age and isFlying
  private static final Object[] getAnimals() {
    return new Object[] { new Object[] { 4, false }, new Object[] { 30, true } };
  }

Wow this look a bit bloated. Luckily JUnitParams comes with a nice feature to declare those Objects. You write the above line also in this way

private static final Object[] getAnimals(){		
    return $(			
        $(4, true),			
        $(3, false),				
        );
  }

To do this you have to add the static import

import static junitparams.JUnitParamsRunner.$;

To use the defined Paramters we have to annotate our test  and add method parameters. The only thing left to do is change the hard coded values in our test with the added parameters. Note: You could define the parameters right in the annotation however i would not recommend it because this way you cant reuse them.

@Test
@Parameters(method = "getAnimals")
  public void constructorShouldSetWeightAndIsFlying(int age, boolean isFlying) {

    Animal testAnimal = new Animal(age, isFlying);
    assertEquals(age, testAnimal.getAge());
    assertEquals(isFlying, testAnimal.isFlying());

  }

If you run the tests again you will see that 3 JUnit tests ran, with minimal effort. And as you can imagine this technique is pretty scalable.

Spread the love

Leave a Reply

Theme by Anders Norén