UnitAssertion
Table of Contents
Javadoc: Javadoc.io > Junisert API > UnitAssertion
Is used to assert on any type of unit to verify stucture and expected behavior. To assume the unit under assertion is a specific type of unit, use methods starting with as for specific assertions. Use methods starting with is for convenient direct assertion.
asPojo
Assumes this unit is a plain object for assertion, which doesn’t necessarily follow a naming convention but carries properties. This method doesn’t perform any tests and just switches assertion to PlainObjectAssertion.
public record Model(String value) {
}
Example usage:
@Test
void givenModel_whenIsImmutable_thenIsWellImplemented() {
Junisert.assertThatUnit(Model.class)
.isImmutable()
.asPojo() // Switching to PlainObjectAssertion
.isWellImplemented();
}
isJavaBeanCompliant
Is a convenience assertion to assert that a unit complies with the Java Bean Specification.
@Test
void givenUnit_whenIsJavaBeanCompliant_thenPassesAssertion() {
Junisert.assertThatUnit(JavaBeanModel.class).isJavaBeanCompliant();
}
Negative test example:
import io.github.mattiaspersson09.junisert.api.assertion.UnitAssertionError;
import io.github.mattiaspersson09.junisert.core.Junisert;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Test
void givenUnit_whenIsNotJavaBeanCompliant_thenFailsAssertion() {
// Example using AssertJ
assertThatThrownBy(() -> Junisert.assertThatUnit(NoDefaultConstructorModel.class).isJavaBeanCompliant())
.isInstanceOf(UnitAssertionError.class)
.hasMessageContaining("NoDefaultConstructorModel")
.hasMessageContaining("expected to have a default constructor");
assertThatThrownBy(() -> Junisert.assertThatUnit(VisiblePropertiesModel.class).isJavaBeanCompliant())
.isInstanceOf(UnitAssertionError.class)
.hasMessageContaining("VisiblePropertiesModel")
.hasMessageContaining("expected to only have private properties");
assertThatThrownBy(() -> Junisert.assertThatUnit(IsMissingGetterModel.class).isJavaBeanCompliant())
.isInstanceOf(UnitAssertionError.class)
.hasMessageContaining("IsMissingGetterModel")
.hasMessageContaining("expected to have getter for instance field");
}
isImmutable
Asserts that unit is immutable, meaning that all instance properties are read-only.
public record Model(String value) {
}
Example usage:
@Test
void givenModel_whenIsImmutable_thenPassesAssertion() {
Junisert.assertThatUnit(Model.class).isImmutable();
}
Negative test with a mutable model:
import io.github.mattiaspersson09.junisert.api.assertion.UnitAssertionError;
import io.github.mattiaspersson09.junisert.core.Junisert;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Test
void givenModel_whenIsMutable_thenFailsAssertion() {
// Example using AssertJ
assertThatThrownBy(() -> Junisert.assertThatUnit(MutableModel.class).isImmutable())
.isInstanceOf(UnitAssertionError.class)
.hasMessageContaining("MutableModel")
.hasMessageContaining("expected to be immutable");
}
whenCreatedFromConstructor
Creates a new ConstructorAssertion and asserts unit constructor (or multiple constructors) to validate properties or state of an instantiated unit instance.
Find constructor(s) to assert on using their matching parameters or a predicate.
import io.github.mattiaspersson09.junisert.common.reflection.Constructor;
@Test
void givenUnit_whenCreatedFromConstructor_singleConstructorMatch() {
Junisert.assertThatUnit(ImmutableModel.class)
.whenCreatedFromConstructor(long.class, boolean.class);
// Following finds default constructor, accepting no arguments
Junisert.assertThatUnit(ImmutableModel.class)
.whenCreatedFromConstructor();
Junisert.assertThatUnit(ImmutableModel.class)
.whenCreatedFromConstructor(Constructor::isDefault);
}
@Test
void givenUnit_whenCreatedFromConstructor_multipleConstructorMatches() {
// Finds all argument constructors
Junisert.assertThatUnit(ImmutableModel.class)
.whenCreatedFromConstructor(Constructor::hasParameters);
// Finds constructors having long as parameter
Junisert.assertThatUnit(ImmutableModel.class)
.whenCreatedFromConstructor(constructor -> constructor.hasParameterType(long.class));
// Finds all constructors
Junisert.assertThatUnit(ImmutableModel.class)
.whenCreatedFromConstructor(constructor -> true);
}