unit testing uniqueness of a field of an entity
I am writting unit tests f开发者_如何转开发or various bean property constraints. For most of the constraints such is field length , format of the field and whether a field is null or not, I am able to test using the JSR303 validation API. My question is, how do I write a unit test for this constraint: 'The field username of entity User must be unique' . I am a java developer so would appreciate a java based solution though any answer is much welcome.
Try to store two User entities with equal username fields, and check, that adequate exception is thrown.
Exception checking may be easily done with junit with the help of "expected" parameter of @Text annotation:
@Test(expected=MyException.class)
public void testMyException() {
See details here: http://junit.sourceforge.net/doc/faq/faq.htm#tests_7
精彩评论