pbt4j

pbt4j is property-based testing extensions for JUnit

License

License

Categories

Categories

Net
GroupId

GroupId

net.soundvibe
ArtifactId

ArtifactId

pbt4j
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

pbt4j
pbt4j is property-based testing extensions for JUnit
Project URL

Project URL

https://github.com/soundvibe/pbt4j
Source Code Management

Source Code Management

https://github.com/soundvibe/pbt4j

Download pbt4j

How to add to project

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

Dependencies

compile (3)

Group / Artifact Type Version
org.reflections : reflections jar 0.9.10
com.pholser : junit-quickcheck-generators jar 0.6.1
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Maven Central Build Status Join the chat at https://gitter.im/soundvibe/pbt4j

pbt4j

Property-based testing extensions for JUnit. pbt4j allows you to add parameters to your test methods. You can either provide your parameter's values using annotations or they will be randomly generated. You can also control how many times tests are being run (default value is 100 times). When providing custom test values, pbt4j uses Nashorn's JavaScript engine to resolve them so possibilities are almost endless.

Learn more about pbt4j on the Wiki home.

Getting started

import org.junit.*;
import org.junit.runner.RunWith;
import pbt4j.PropertyBasedTesting;
import pbt4j.annotations.*;
import java.util.*;
import static org.junit.Assert.*;

@RunWith(PropertyBasedTesting.class)
@JsEval("var Foo = Java.type('pbt4j.Foo'); var Bean = Java.type('pbt4j.Bean');")
//we can optionally register our custom types in js context if we want to provide them using @JsData annotation
public class PropertyBasedTestingTest {
    @Test
    public void shouldProvideTwoArguments(
           @JsData({"1", "2"}) long numbers,
           @JsData({"'foo'", "'bar'"}) String strings) {
        System.out.println("arg1: " + numbers);
        System.out.println("arg2: " + strings);
        //first run prints:
        // arg1: 1
        // arg2: foo
        //second run prints:
        // arg1: 2
        // arg2: bar
    }

    @Test
    public void shouldProvideListsOfArguments(
            @JsData({"['foo', 'bar']", "['second', 'third']"}) List<String> arg,
            @JsData({"[1, 2]", "[3, 4]"}) List<Integer> numbers) {

        assertEquals(2, arg.size());
        assertEquals(2, numbers.size());

        System.out.println(arg);
        System.out.println(numbers);
        //first run prints:
        // [foo, bar]
        // [1, 2]
        //second run prints:
        // [second, third]
        // [3, 4]
    }

    @Test
    @Repeat(times = 10)
    public void shouldProvideRandomValuesAndCallMethod10Times(int number) throws Exception {
        System.out.println(number);
        //Prints 10 random numbers
    }

    @Test
    public void shouldProvideCustomClass(Foo foo) throws Exception {
        System.out.println(foo);
        //prints randomly generated Foo 100 times
    }

    @Test
    public void shouldProvideMap(
            @JsonData({"{foo: 'bar', second: 'bar2'}"}) Map<String, String> map) throws Exception {
        assertEquals(HashMap.class, map.getClass());
        assertEquals("bar", map.get("foo"));
        assertEquals("bar2", map.get("second"));
    }

    @Test
    public void shouldProvideCustomClassUsingJs(
            @JsData({"new Foo('Foo', 28, [new Bean()], null)"}) Foo foo) throws Exception {
        assertEquals("Foo", foo.name);
        assertEquals(28, foo.age);
        assertEquals(Foo.class, foo.getClass());
    }

}

Binaries

Binaries and dependency information for Maven, Ivy, Gradle and others can be found at http://search.maven.org.

Example for Gradle:

compile 'net.soundvibe:pbt4j:x.y.z'

and for Maven:

<dependency>
    <groupId>net.soundvibe</groupId>
    <artifactId>pbt4j</artifactId>
    <version>x.y.z</version>
</dependency>

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

LICENSE

Copyright 2016 Linas Naginionis

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Versions

Version
1.0