DV Bern JUnit BeanValidation Extension

Allow for easy injection and customization of a Java BeanValidation Validator into your tests

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

ch.dvbern.oss.junit-beanvalidation-extension
ArtifactId

ArtifactId

junit-beanvalidation-extension
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

DV Bern JUnit BeanValidation Extension
Allow for easy injection and customization of a Java BeanValidation Validator into your tests
Project URL

Project URL

https://github.com/dvbern/junit-beanvalidation-extension
Project Organization

Project Organization

DV Bern AG
Source Code Management

Source Code Management

https://github.com/dvbern/junit-beanvalidation-extension.git

Download junit-beanvalidation-extension

How to add to project

<!-- https://jarcasting.com/artifacts/ch.dvbern.oss.junit-beanvalidation-extension/junit-beanvalidation-extension/ -->
<dependency>
    <groupId>ch.dvbern.oss.junit-beanvalidation-extension</groupId>
    <artifactId>junit-beanvalidation-extension</artifactId>
    <version>1.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/ch.dvbern.oss.junit-beanvalidation-extension/junit-beanvalidation-extension/
implementation 'ch.dvbern.oss.junit-beanvalidation-extension:junit-beanvalidation-extension:1.0.3'
// https://jarcasting.com/artifacts/ch.dvbern.oss.junit-beanvalidation-extension/junit-beanvalidation-extension/
implementation ("ch.dvbern.oss.junit-beanvalidation-extension:junit-beanvalidation-extension:1.0.3")
'ch.dvbern.oss.junit-beanvalidation-extension:junit-beanvalidation-extension:jar:1.0.3'
<dependency org="ch.dvbern.oss.junit-beanvalidation-extension" name="junit-beanvalidation-extension" rev="1.0.3">
  <artifact name="junit-beanvalidation-extension" type="jar" />
</dependency>
@Grapes(
@Grab(group='ch.dvbern.oss.junit-beanvalidation-extension', module='junit-beanvalidation-extension', version='1.0.3')
)
libraryDependencies += "ch.dvbern.oss.junit-beanvalidation-extension" % "junit-beanvalidation-extension" % "1.0.3"
[ch.dvbern.oss.junit-beanvalidation-extension/junit-beanvalidation-extension "1.0.3"]

Dependencies

compile (1)

Group / Artifact Type Version
org.checkerframework : checker-qual jar 3.11.0

provided (2)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.7.1
javax.validation : validation-api jar 2.0.1.Final

test (6)

Group / Artifact Type Version
org.glassfish : javax.el jar 3.0.0
org.hibernate.validator : hibernate-validator jar 6.2.0.Final
javax.enterprise : cdi-api jar 2.0
org.assertj : assertj-core jar 3.19.0
org.mockito : mockito-core jar 3.8.0
org.mockito : mockito-junit-jupiter jar 3.8.0

Project Modules

There are no modules declared in this project.

junit-beanvalidation-extension

Allow for easy injection (and optionally: customization) of a Java BeanValidation Validator into your tests.

This enables you to use ConstraintValidators that need special setup (like e.g. Mocks).

Prerequisites/Dependencies

This project requires Java >= 8.

To spare you mostly of dependency hell, this library does not include dependencies on any beanvalidation library! You have to add these dependencies yourself, see Installing.

Releases

Current version: see GitHub releases or Maven Central

Installing

Maven dependency:

<dependency>
	<groupId>ch.dvbern.oss</groupId>
	<artifactId>junit-beanvalidation-extension</artifactId>
	<version>see-github-releases</version>
	<scope>test</scope>
</dependency>

For available versions: see Releases

If you do not run this library in a container, you might need to add at least these dependencies to make beanvalidation work:

<dependencies>
	<dependency>
		<!-- validation API spec -->
		<groupId>javax.validation</groupId>
		<artifactId>validation-api</artifactId>
		<version>2.0.1.Final</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<!-- transitive requirement of the validtion API -->
		<groupId>org.glassfish</groupId>
		<artifactId>javax.el</artifactId>
		<version>3.0.0</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<!-- validation API implementation (hibernate-validaor is just used as an example) -->
		<groupId>org.hibernate.validator</groupId>
		<artifactId>hibernate-validator</artifactId>
		<version>6.2.0.Final</version>
		<scope>test</scope>
	</dependency>
</dependencies>

Basic Usage

Just inject a Validator parameter into your test method... and that is basically all there is to it.

// Enable the extension
@ExtendWith(ValidatorExtension.class)
public class CustomValidatorTest {
	@Test
	void validator_accepts_valid_input(Validator validator) {
		Set<ConstraintViolation<Object>> actual = validator.validate(new SomeFixture("Hello World"));

		assertThat(actual)
				.isEmpty();
	}
}

Customization

Inject a ValidatorCustomizer into your setup methods (annotated by @BeforeAll/BeforeEach) and start customizing:

// Enable the extension
@ExtendWith(ValidatorExtension.class)
public class CustomValidatorTest {

	// global setup for all followup tests
	// ValidatorCustomizer gets injected by the extension
	@BeforeAll
	static void beforeAll(ValidatorCustomizer customizer) {
		// 'Customizations' contains some predefined routines for ease of use.
		customizer.customize(Customizations.usingConstraintValidator(
				new CustomConstraintValidator(true)));
	}

	@Nested
	class ValidInputTest {
		// the Validator param gets injected by the extension
		@Test
		void validator_accepts_valid_input(Validator validator) {
			Set<ConstraintViolation<Object>> actual = validator.validate(new SomeFixture("Hello World"));

			assertThat(actual)
					.isEmpty();
		}
	}

	@Nested
	class ChangedConfigurationTest {

		// that one test that needs special setup
		@BeforeEach
		void beforeEach(ValidatorCustomizer customizer) {
			// please note: beforeEach customization completely *replaces* customization in beforeAll!
			customizer.customize(Customizations.usingConstraintValidator(
					new CustomConstraintValidator(false)));
		}

		@Test
		void rejects_same_input_with_changed_configuration(Validator validator) {
			Set<ConstraintViolation<Object>> actual = validator.validate(new SomeFixture("Hello World"));

			assertThat(actual)
					.isNotEmpty();
		}
	}
}

Common Use-Cases

Complex ConstraintValidator

This extension comes in quite handy if you have ConstraintValidators that get injected some parameters, e.g.:

public class CustomConstraintValidator implements ConstraintValidator<CustomConstraint, Object> {

	private final boolean ok;

	// maybe in production, this parameter gets injected by CDI/Spring
	@Inject
	// @Autowired
	public CustomConstraintValidator(boolean someConfiguration) {
		ok = someConfiguration;
	}

	@Override
	public void initialize(CustomConstraint constraintAnnotation) {
		// nop
	}

	@Override
	public boolean isValid(Object value, ConstraintValidatorContext context) {
		return ok;
	}
}

Integrating the standard BeanValidation ValidatorFactory makes using such Validators quite a pain.

Using the method described in Basic Usage, you now can create your ConstraintValidator instances easily in your test setup using Mocks/Stubs/Fakes/whatever.

Other customizations

The ValidatorCustomizer allows modifying all properties of the current ValidationContext

Nullability

All parameters/returns are Non-Null if not explicitly stated by a @Nullable annotation!

Built With

  • Maven - Dependency Management

Contributing Guidelines

Please read [CONTRIBUTING.md] for the process for submitting pull requests to us.

Code of Conduct

One healthy social atmospehere is very important to us, wherefore we rate our Code of Conduct high. For details check the file [CODE_OF_CONDUCT.md]

Authors

  • DV Bern AG - Initial work - dvbern

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache 2.0 License - see the [LICENSE] file for details.

ch.dvbern.oss.junit-beanvalidation-extension

DV Bern AG

Versions

Version
1.0.3
1.0.1
1.0.0
0.0.1