View on GitHub

UnitSocializer

Simplify your sociable tests with minimal effort!

Quality Gate Status Reliability Rating Vulnerabilities

UnitSocializer jUnit-Mockito

Module to be used in combination with the jUnit testing framework, combined with Mockito as your mocking provider.

Installation

To use UnitSocializer jUnit-Mockito, you will need to add only one dependency!

<dependency>
    <groupId>io.github.wouter-bauweraerts</groupId>
    <artifactId>unit-socializer-junit-mockito</artifactId>
    <version>${unit-socializer.version}</version> <!-- Set correct version here on in the properties --> 
    <scope>test</scope>
</dependency>

Please check Maven Central Repository for the latest release version!

Compatibility info

The unit-socializer-junit-mockito module requires dependencies on jUnit and Mockito. I guarantee that it works with this version, using other versions may or may not cause problems.

UnitSocializer junit-jupiter mockito
0.0.2 5.11.4 5.15.2

Usage

The UnitSocializer jUnit-Mockito module provides a Sociable Test extension built upon jUnit 5. All you have to do to make your test use this extension, is annotate your test class with the @SociableTest annotation.

The @SociableTest annotation also requires an @TestSubject annotated field to be present in your test. This field defines your test unit, which describes the part of your code you want to test in this test class.

Examples

Minimal setup example

Below you can find a simple example, where the class has no actual dependencies. As mentioned in the Usage section, you only need to add 2 annotations to make your test run.

public enum CountryShippingCost {
    BELGIUM(BigDecimal.ONE, BigDecimal.ZERO),
    // ...
    US(BigDecimal.valueOf(120), BigDecimal.valueOf(100));

    final BigDecimal shippingCost;
    final BigDecimal noShippingCostMinimum;

    CountryShippingCost(BigDecimal shippingCost, BigDecimal noShippingCostMinimum) {
        this.shippingCost = shippingCost;
        this.noShippingCostMinimum = noShippingCostMinimum;
    }

    public BigDecimal getShippingCost() {
        return shippingCost;
    }

    public boolean isAboveMinimum(BigDecimal totalPrice) {
        return totalPrice.compareTo(noShippingCostMinimum) >= 0;
    }
}
// package declaration and imports omitted
public class ShippingCostCalculator {
    public BigDecimal calculateShippingCost(BigDecimal totalPrice, String shipToCountryCode) {
        CountryShippingCost countryShippingCost = CountryShippingCost.valueOf(shipToCountryCode);

        return countryShippingCost.isAboveMinimum(totalPrice)
                ? BigDecimal.ZERO
                : countryShippingCost.getShippingCost();
    }
}

The code below shows how to write a test for the code examples above

// package declaration and imports omitted

@SociableTest // <-- annotate your test class with @SociableTest
class ShippingCostCalculatorTest {
    @TestSubject // <-- annotate your system under test with @TestSubject
    ShippingCostCalculator shippingCostCalculator;

    @ParameterizedTest
    @MethodSource("shippingCostSource")
    void calculateShippingCost(CountryShippingCost country, BigDecimal value, BigDecimal expected) {
        assertThat(shippingCostCalculator.calculateShippingCost(value, country.name())).isEqualTo(expected);
    }
    
    // shippingCostSource method omitted
}

This minimal setup will also work if your class has dependencies on concrete implementations. If you need to configure components that you want to mock, check the mock configuration example

More examples

JavaDoc

Latest JavaDoc can be found here