JUnitWithParams

Parameterised tests, it works with any Runner (Robolectric, String, Mockito, and more!)

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

com.github.ignaciotcrespo
ArtifactId

ArtifactId

junitwithparams
Last Version

Last Version

1.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

JUnitWithParams
Parameterised tests, it works with any Runner (Robolectric, String, Mockito, and more!)
Project URL

Project URL

https://github.com/ignaciotcrespo/junitwithparams
Source Code Management

Source Code Management

https://github.com/ignaciotcrespo/junitwithparams.git

Download junitwithparams

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ignaciotcrespo/junitwithparams/ -->
<dependency>
    <groupId>com.github.ignaciotcrespo</groupId>
    <artifactId>junitwithparams</artifactId>
    <version>1.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ignaciotcrespo/junitwithparams/
implementation 'com.github.ignaciotcrespo:junitwithparams:1.0.4'
// https://jarcasting.com/artifacts/com.github.ignaciotcrespo/junitwithparams/
implementation ("com.github.ignaciotcrespo:junitwithparams:1.0.4")
'com.github.ignaciotcrespo:junitwithparams:jar:1.0.4'
<dependency org="com.github.ignaciotcrespo" name="junitwithparams" rev="1.0.4">
  <artifact name="junitwithparams" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ignaciotcrespo', module='junitwithparams', version='1.0.4')
)
libraryDependencies += "com.github.ignaciotcrespo" % "junitwithparams" % "1.0.4"
[com.github.ignaciotcrespo/junitwithparams "1.0.4"]

Dependencies

compile (1)

Group / Artifact Type Version
junit : junit jar 4.12

test (2)

Group / Artifact Type Version
org.mockito : mockito-core jar 2.7.5
org.assertj : assertj-core jar 2.6.0

Project Modules

There are no modules declared in this project.

JUnitWithParams

Parameterised tests, it works with any Runner (Robolectric, Spring, Mockito, and more!)

build status Build Status codecov.io MIT License

Example, single parameter

public class CalculatorTest {
 
  @Rule
  public WithParamsRule params = new WithParamsRule();
 
  @Test
  @WithParams({"2", "4", "8", "1000"})
  public void isEven() throws Exception {
        assertTrue(calculator.isEven(params.asInt()));
  }
  
}

Example, multiple parameters

public class CalculatorTest {
 
  @Rule
  public WithParamsRule params = new WithParamsRule();
 
  @Test
  @WithParams(
        names = {"n1", "n2", "result"},
        value = {
                "1", "2", "3",
                "11", "-2", "9"
        }
  )
  public void sum() throws Exception {
        int n1 = params.asInt("n1");
        int n2 = params.asInt("n2");
 
        int result = calculator.sum(n1, n2);
 
        assertEquals(params.asInt("result"), result);
  }
  
}

Kotlin

Kotlin is supported, with some small differences.

Create rule:

@get:Rule var params = WithParamsRule()

The format for the arrays in the annotations

  @WithParams(
        names = ["n1", "n2", "result"],
        value = [
                "1", "2", "3",
                "11", "-2", "9"
        ]
  )

About

JUnitWithParams uses the rule WithParamsRule to parse the annotation @WithParams

Very similar to the great library JUnitParams but not using a JUnit runner, this allows to use parameterised tests with the most used runners (Spring, Robolectric, AndroidJUnit4)

Current Version

  • The current stable version is 1.0.4
  • The current snapshot version is 1.0.4-SNAPSHOT

Android support

JUnitWithParams works fine in android, it is compiled with JDK 1.6 and tested on real projects.

Quickstart

JUnitWithParams is available as Maven artifact:

<dependency>
  <groupId>com.github.ignaciotcrespo</groupId>
  <artifactId>junitwithparams</artifactId>
  <version>1.0.0</version>
  <scope>test</scope>
</dependency>

To use JUnitWithParams in a Gradle build add this to your dependencies:

testCompile 'com.github.ignaciotcrespo:junitwithparams:1.0.4'

Contribution

JUnitWithParams is a work in progress, it is stable but of course there are still some edge cases not covered.

You are welcome to contribute to the project, feel free to create a pull request with your changes.

For questions, suggestions or feedback, create an issue in this repository.

License

JUnitWithParams is released under the MIT license.

The MIT License

Copyright (c) 2017, Ignacio Tomas Crespo ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Versions

Version
1.0.4
1.0.3
1.0.2
1.0.0