Annotation Validation not work


#1

Ich habe mehrere Klassen mit Validation Annotations

  @Entity
  @Table(name = "staates")
  public class Staate {
	/**
	 * the id of the staate
	 * 
	 * @var PrimaryKey
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	/**
	 * the create date of the staate
	 * 
	 * @var TIMESTAMP
	 */
	@CreationTimestamp
	private Date    created_at;
	
	/**
	 * the user who create the staate
	 * 
	 * @var INTEGER(11)
	 */
	@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)
	private User	created_by;
	
	/**
	 * the last update of the staate
	 * 
	 * @var TIMESTAMP
	 */
	@UpdateTimestamp
	private Date    updated_at;
	
	/**
	 * the user who update the entity as last
	 * 
	 * @var INTEGER(11)
	 */
	@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)	
	private User	updated_by;	
	
	/**
	 * the international names of staate
	 * 
	 * @var INTEGER(11)
	 */
	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch=FetchType.EAGER)
	@JoinTable( 
			name = "staate_names",
			joinColumns={ @JoinColumn(name="staate_id", referencedColumnName="id") },
			inverseJoinColumns={ @JoinColumn(name="text_id", referencedColumnName="id", unique=true) }
	)
	private List<Text> names = new ArrayList<Text>();
	
	/**
	 * the iso code of the staate
	 * 
	 * @var VARCHAR(10)
	 */
	@Size(min=3, max=10)
	private String iso;
	
	/**
	 * the default constructor
	 */
	public Staate() {
		super();
	}
	
	/**
	 * initialize constructor
	 * 
	 * @param Date
	 *            created_at the creation date
	 * @param User
	 *            user who create this entity
	 * @param Date
	 *            updated_at the last updated date
	 * @param User
	 *            user who upadate this entity at least
	 * @param String[]
	 *            the international names of the staates
	 * @param String
	 *            iso the iso number of the staate
	 * @return void
	 */
	public Staate(Date created_at, User created_by, Date updated_at, User updated_by, List<Text> names,
			@Size(min = 3, max = 10) String iso) {
		super();
		this.created_at = created_at;
		this.created_by = created_by;
		this.updated_at = updated_at;
		this.updated_by = updated_by;
		this.names = names;
		this.iso = iso;
	}

	// Getter / Setter
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getCreatedAt() {
		return created_at;
	}

	public void setCreatedAt(Date created_at) {
		this.created_at = created_at;
	}

	public User getCreatedBy() {
		return created_by;
	}

	public void setCreatedBy(User created_by) {
		this.created_by = created_by;
	}

	public Date getUpdatedAt() {
		return updated_at;
	}

	public void setUpdatedAt(Date updated_at) {
		this.updated_at = updated_at;
	}

	public User getUpdatedBy() {
		return updated_by;
	}

	public void setUpdatedBy(User updated_by) {
		this.updated_by = updated_by;
	}

	public List<Text> getNames() {
		return names;
	}

	public void setNames(List<Text> names) {
		this.names = names;
	}

	public String getIso() {
		return iso;
	}

	public void setIso(String iso) {
		this.iso = iso;
	}

	@Override
	public String toString() {
		return "Staate [id=" + id + ", created_at=" + created_at + ", created_by=" + created_by + ", updated_at="
				+ updated_at + ", updated_by=" + updated_by + ", names=" + names + ", iso=" + iso + "]";
	}
	
  }

und entsprechend dazu Test Klassen

import static org.junit.Assert.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

  /**
   * this is the test class for staates
   * 
   * @author tomth
   */
  public class StaateTest {
	
	Staate staate;
	
	@Before
	public void setUp() throws Exception {
		staate = new Staate();
	}
	
	/**
	 * test the default staate constructor
	 */	
	@Test
	public void testDefaultConstructor() {
		Staate staate = new Staate();
		
		assertNotNull(staate);	
	}
	
	/**
	 * test the intial staate constructor
	 * @throws ParseException 
	 */
	@Test
	public void testMinimalizedConstructor() throws ParseException {
		
		String createdAtValue = "28.06.2001";
		User createdByValue = new User();
		String updateAtValue = "16.5.2004";
		User updateByValue = new User();
		List<Text> nameValues = new ArrayList<>();
		String isoValues = "ISO";

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		Date createDateValue = dateFormat.parse(createdAtValue);
		Date updateDateValue = dateFormat.parse(updateAtValue);

		nameValues.add(new Text());
		nameValues.add(new Text());
		nameValues.add(new Text());
		
		Staate staate = new Staate(createDateValue, createdByValue, updateDateValue, updateByValue, nameValues, isoValues);
		
		assertEquals(createDateValue, staate.getCreatedAt());
		assertEquals(createdByValue, staate.getCreatedBy());
		assertEquals(updateDateValue, staate.getUpdatedAt());
		assertEquals(updateByValue, staate.getUpdatedBy());
		assertEquals(nameValues, staate.getNames());
		assertEquals(isoValues, staate.getIso());
	}
	
	/**
	 * test the id getter and setter
	 */	
	@Test
	public void testID() {
		Long idValue = 4L;
		
		staate.setId(idValue);
		assertEquals(idValue, staate.getId());		
	}
	
	/**
	 * test the create date getter and setter
	 */	
	@Test
	public void testCreatedAt() throws ParseException {
		String creationDateValue = "16.05.1989";
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		Date createDateValue = dateFormat.parse(creationDateValue);

		staate.setCreatedAt(createDateValue);
		assertEquals(createDateValue, staate.getCreatedAt());
	}


	/**
	 * test the create user getter and setter
	 */	
	@Test
	public void testCreatedBy() throws ParseException {
		User userValue = new User();
		
		staate.setCreatedBy(userValue);
		assertEquals(userValue, staate.getCreatedBy());		
	}
	
	/**
	 * test the create user getter and setter
	 */	
	public void testUpdatedAt() throws ParseException {
		String updateDateValue = "27.06.2003";
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		Date updateDate = dateFormat.parse(updateDateValue);

		staate.setUpdatedAt(updateDate);
		assertEquals(updateDate, staate.getUpdatedAt());
	}
	
	/**
	 * test the update user getter and setter
	 */	
	@Test
	public void testUpdatedBy() {
		User userValue = new User();
		
		staate.setCreatedBy(userValue);
		assertEquals(userValue, staate.getCreatedBy());		
	}
	
	/**
	 * test the names getter and setter
	 */
	@Test
	public void testNames() {
		List<Text> nameValues = new ArrayList<>();

		nameValues.add(new Text());
		nameValues.add(new Text());
		nameValues.add(new Text());

		staate.setNames(nameValues);
		assertEquals(nameValues, staate.getNames());		
	}

	/**
	 * test the iso getter and setter
	 */

	@Test
	public void testIso() {
		String isoValue = "ISO1234567890";

		staate.setIso(isoValue);
		assertEquals(isoValue, staate.getIso());		
	}

  }

Hier mal stellvertretend zum Beispiel die ISO - Funktion. Normalerweise müsste doch JUnit Test scheitern bei der Test Funktion testIso(), weil die Länge des Wertes über max 10 Zeichen lang ist. Aber bei der Ausführung wird der Test als positiv gewertet.

Woran liegt es, dass der Test als positiv gewertet wird? Wo ist mein Gedankenfehler? Wie testet man auf Entity Ebene, dass die korrekten Werte gewertet werden?


#2

Bin jetzt da nicht so der Profi drin, aber haste mal hier geschaut?
https://docs.oracle.com/javaee/6/api/javax/validation/Validator.html#validate(T,%20java.lang.Class...)

Normalerweise müsste das so gehen.

@Test(exception = ValidationException.class)
public void testIso() {
  String isoValue = “ISO1234567890”;
  staate.setIso(isoValue);
  Validator validator = ...//Don't know how to initialize Validator
  validator.validate(staate);
}

Wenn das Ding dann läuft, dann sollte beim Speichern automatisch nochmals ein Validator eingebunden werden, der die Entity checken und gegebenenfalls eine Exception werfen würde.