ConstructorAssertion

Table of Contents

Javadoc: Javadoc.io > Junisert API > ConstructorAssertion

Assertion for unit constructors which is owned by UnitAssertion. Asserts state in a unit after being instantiated from a constructor or multiple constructors.

Given unit with multiple constructors for the sake of documentation:

public class ImmutableModel {
    private final long longField;
    private final String stringField;
    private final boolean booleanField;

    public ImmutableModel(long longField, boolean booleanField) {
        this(longField, "default value", booleanField);
    }
    
    public ImmutableModel(long longField, String stringField, boolean booleanField) {
        this.longField = longField;
        this.stringField = stringField;
        this.booleanField = booleanField;
    }

    public long getLongField() {
        return longField;
    }

    public String getStringField() {
        return stringField;
    }

    public boolean isBooleanField() {
        return booleanField;
    }
}

hasAssignedProperty

Asserts that an instance property has been assigned a value other than default initial value (Java Language Specification > 4.12.5. Initial Values of Variables) after unit has been instantiated.

import io.github.mattiaspersson09.junisert.common.reflection.Constructor;

@Test
void givenUnit_whenCreatedFromConstructor_thenHasAssignedProperty() {
    Junisert.assertThatUnit(ImmutableModel.class)
            .whenCreatedFromConstructor(Constructor::hasParameters)
            .hasAssignedProperty("stringField");
}

hasAssignedProperties

Asserts that instance properties has been assigned a value other than default initial value (Java Language Specification > 4.12.5. Initial Values of Variables) after unit has been instantiated.

import io.github.mattiaspersson09.junisert.common.reflection.Constructor;

@Test
void givenUnit_whenCreatedFromConstructor_thenHasAssignedProperties() {
    Junisert.assertThatUnit(ImmutableModel.class)
            .whenCreatedFromConstructor(Constructor::hasParameters)
            .hasAssignedProperties("longField", "stringField", "booleanField");
}

hasAssignedAllProperties

Asserts that every instance property has been assigned a value other than default initial value (Java Language Specification > 4.12.5. Initial Values of Variables) after unit has been instantiated.

import io.github.mattiaspersson09.junisert.common.reflection.Constructor;

@Test
void givenUnit_whenCreatedFromConstructor_thenHasAssignedAllProperties() {
    Junisert.assertThatUnit(ImmutableModel.class)
            .whenCreatedFromConstructor(Constructor::hasParameters)
            .hasAssignedAllProperties();
}

hasState

Asserts that unit owning the constructor has a certain state after being instantiated.

@Test
void givenUnit_whenCreatedFromConstructor_thenHasState() {
    Junisert.assertThatUnit(ImmutableModel.class)
            .whenCreatedFromConstructor(long.class, boolean.class)
            .hasState(unit -> unit.getStringField().equals("default value"));
}

Set description on what the expected state test is supposed to validate.

@Test
void givenUnit_whenCreatedFromConstructor_thenHasState() {
    Junisert.assertThatUnit(ImmutableModel.class)
            .whenCreatedFromConstructor(long.class, boolean.class)
            .hasState(unit -> unit.getStringField().equals("default value"), "has default string value");
}

void givenUnit_whenCreatedFromConstructor_withSupportValue_thenHasState() {
    Junisert.assertThatUnit(ImmutableModel.class)
            .whenCreatedFromConstructor(constructor -> constructor.hasParameterType(String.class))
            .withSupport(String.class, () -> "validated value")
            .hasState(unit -> unit.getStringField().equals("validated value"), "has value from support");
}

assertThatUnit

This method doesn’t perform any tests and just switches assertion back to UnitAssertion, to continue asserting on the unit owning the constructor(s).

import io.github.mattiaspersson09.junisert.common.reflection.Constructor;

@Test
void givenUnit_whenCreatedFromConstructor_thenCanSwitchBackToUnitAssertion() {
    Junisert.assertThatUnit(ImmutableModel.class)
            .whenCreatedFromConstructor(Constructor::hasParameters)
            .hasAssignedAllProperties()
            .assertThatUnit() // Switches back to UnitAssertion
            .isImmutable();
}