Valter

Valter exists to test JSR303 validated beans.

License

License

GroupId

GroupId

com.oakfusion
ArtifactId

ArtifactId

valter
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Valter
Valter exists to test JSR303 validated beans.

Download valter

How to add to project

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

Dependencies

compile (2)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.2
uk.com.robust-it : cloning jar 1.9.0

provided (2)

Group / Artifact Type Version
junit : junit jar 4.11
javax.validation : validation-api jar 1.0.0.GA

Project Modules

There are no modules declared in this project.

Valter's logo

valter-project

Valter exists to test JSR303 validated beans. Thanks to Valter it's possible to create JUnit parametrized tests for such beans.

##Requirements Valter requires three libraries to be included to the project where You will be using it:

  • JUnit - optimum: v4.11+, minimum: v4.0, but tests doesn't have their names,
  • SLF4J,
  • library with JSR303 bean's validator (in example hibernate validator).

##Maven

<dependency>
	<groupId>com.oakfusion</groupId>
	<artifactId>valter</artifactId>
	<version>1.0</version>
</dependency>

##Usage Using Valter can be described in three steps:

Step 1. Create bean with fields annotated with JSR303 annotations.

Step 2. Create test class witch extends com.oakfusion.valter.ValidationTestBase class and create constructor matching super.

Step 3. Implement public static method which returns List<Object[]>. Use Valter's fluent interface to create this list.

Note: This method must be annotated with @Parameters annotation, because JUnit parametrized runner will use it to run tests cases.

Example:

public class UserTest extends ValidationTestBase<User> {

	public UserTest(String description, User bean, Object expected) {
		super(description, bean, expected);
	}

	@Parameters(name = DEFAULT_TESTS_NAME_PATTERN)
	public static List<Object[]> params() {

		User validBean = new User("Valter", "[email protected]", 26);

		return forBean(validBean)
				.field("name").shouldFailWith(NotEmpty.class).when("").withDescription("empty name")
				.field("email").shouldFailWith(Email.class).when("email@").withDescription("wrong email")
				.field("email").shouldFailOnce().when("valter@").withDescription("one violation on email field")
				.field("age").shouldFailWith(Range.class).when(2).withDescription("out of range")
				.toList();
	}
}

Note: if You are using JUnit older than 4.11 You need to remove (name = DEFAULT_TESTS_NAME_PATTERN).

##Valter's fluent interface. Creating tests cases for parametrized test should be done by Valter's fluent interface. Example usage can looks as follow:

forBean(validBean)  //passing valid bean object to Valter
	.field("name")  //passing field name which annotations You want to test
	.shouldFailWith(NotEmpty.class) //there You can call three methods, depends of it what You want to test: count of violations or class of annotation which will fail
	.when("")       //passing value which would cause violation
	.withDescription("empty name") //setting up name of the test case
	.toList();      //building List<Object[]>

##Example using Hibernate validator

Create maven project and add required dependencies to pom.xml:

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.11</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>com.oakfusion</groupId>
		<artifactId>valter</artifactId>
		<version>1.0</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-validator</artifactId>
		<version>4.3.0.Final</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.7.2</version>
	</dependency>
</dependencies>

Create bean with annotated fields:

import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Range;

public class User {

	@NotEmpty
	private String name;

	@Email
	private String email;

	@Range(min = 16, max = 150)
	private int age;

	public User(String notEmptyField, String emailField, int age) {
		this.name = notEmptyField;
		this.email = emailField;
		this.age = age;
	}
}

Create test class:

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
import org.junit.runners.Parameterized.Parameters;
import com.oakfusion.valter.ValidationTestBase;

import java.util.List;

public class UserTest extends ValidationTestBase<User> {

	public UserTest(String description, User bean, Object expected) {
		super(description, bean, expected);
	}

	@Parameters(name = DEFAULT_TESTS_NAME_PATTERN)
	public static List<Object[]> params() {

		User validBean = new User("Valter", "[email protected]", 26);

		return forBean(validBean)
				.field("name").shouldFailWith(NotEmpty.class).when("").withDescription("empty name")
				.field("email").shouldFailWith(Email.class).when("email@").withDescription("wrong email")
				.field("email").shouldFailOnce().when("valter@").withDescription("one violation on email field")
				.field("age").shouldFailWith(Range.class).when(2).withDescription("out of range")
				.toList();
	}
}

Have fun!

Versions

Version
1.1
1.0