Reputation: 491
What am I doing wrong? The test does not work.
This is my Interface class:
@Validated
public interface ICustomerService
{
public List<Customer> findbyStr(
@NotEmpty(message = "{column.notvalid}")
@NotNull(message = "{column.notvalid}")
String column,
@NotEmpty(message = "{column.notvalid}")
@NotNull(message = "{value.notvalid}")
String value);
}
This is my Implementation class:
@Service("customerService")
@Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class CustomerService implements ICustomerService {
@Autowired
private IStorageDao<Customer> dao;
@Override
public List<Customer> findbyStr(String column, String value) {
return dao.findByString(Customer.class, column, value);
}
}
This is my unit-Test class: JUNIT Test does not work.
@RunWith(SpringJUnit4ClassRunner.class)
public class CustomerTest extends BaseIntegrationTest {
@Autowired
private ICustomerService service;
@Autowired
public static Validator validator;
@Test
public void test_A6_CustomerFindByStrNull() {
List<Customer> result = service.findbyStr(null, null);
Set<ConstraintViolation<ICustomerService>> constraintViolations = validator
.validate(service);
assertEquals(0, constraintViolations.size());
assertEquals("Die angegebene E-Mail-Adresse ist fehlerhaft.",
constraintViolations.iterator().next().getMessage());
assertNotNull(result);
assertNotNull(result.get(1));
}
}
Upvotes: 0
Views: 818
Reputation: 10649
I'm pretty sure you cannot test ConstraintViolations
when the annotations are on a method of an object since it should throw a MethodConstraintViolationException
. You should try something like this :
@RunWith(SpringJUnit4ClassRunner.class)
public class CustomerTest extends BaseIntegrationTest {
@Autowired
private ICustomerService service;
@Test
public void test_A6_CustomerFindByStrNull() {
try {
List<Customer> result = service.findbyStr(null, null);
} catch (MethodConstraintViolationException ex) {
assertEquals("Die angegebene E-Mail-Adresse ist fehlerhaft.", ex.getConstraintViolations().iterator().next().getMessage());
}
fail("Exception expected");
}
}
You need to have the following Bean in your application-context.xml
file :
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>
Upvotes: 1