Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized Unit Tests in Scala (with JUnit4)

Is there a way to implement a parameterized unit test with Scala? Currently I use JUnit4 in the rest of my programs and I would like to continue using only "standard" APIs.

I found an example for Junit4 with Groovy, but I have problems defining the static parts. Could be, because I am also quite new with Scala :-)

I am currently as fas as

import org.junit.Test
import org.junit.Assert._

import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters

@RunWith(classOf[Parameterized])
class MyTest extends junit.framework.TestCase {

    @Parameters object data {
        ...
    }

    @Parameter ...

    @Test
    def testFunction() = {
    }

like image 740
towi Avatar asked Dec 09 '10 15:12

towi


People also ask

What are parameterized tests used for in a unit?

Parameterized test is to execute the same test over and over again using different values. It helps developer to save time in executing same test which differs only in their inputs and expected results. Using Parameterized test, one can set up a test method that retrieves data from some data source.

Does JUnit support parameterized tests where tests can be executed multiple times with different parameter values?

JUnit5 Parameterized Test helps to run the same tests multiple times with different arguments/values. They are declared just like the regular @Test method but instead of @Test annotation @ParameterizedTest is used. All supported argument sources are configured using annotations from the org.


1 Answers

That's quite a nuisance, but it works. Two important things I discovered: companion object must come after test class, the function returning the parameters must return a collection of arrays of AnyRef (or Object). arrays of Any won't work. That the reason I use java.lang.Integer instead of Scala's Int.

import java.{util => ju, lang => jl}
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters

@RunWith(value = classOf[Parameterized])
class JUnit4ParameterizedTest(number: jl.Integer) {
    @Test def pushTest = println("number: " + number)
}

// NOTE: Defined AFTER companion class to prevent:
// Class com.openmip.drm.JUnit4ParameterizedTest has no public
// constructor TestCase(String name) or TestCase()
object JUnit4ParameterizedTest {

    // NOTE: Must return collection of Array[AnyRef] (NOT Array[Any]).
    @Parameters def parameters: ju.Collection[Array[jl.Integer]] = {
        val list = new ju.ArrayList[Array[jl.Integer]]()
        (1 to 10).foreach(n => list.add(Array(n)))
        list
    }
}

The output should be as expected:

Process finished with exit code 0
number: 1
number: 2
number: 3
number: 4
number: 5
number: 6
number: 7
number: 8
number: 9
number: 10
like image 109
Horst Dehmer Avatar answered Sep 28 '22 02:09

Horst Dehmer