Java bean验证spring+;hibernate注释不起作用
我做错了什么?测试不起作用Java bean验证spring+;hibernate注释不起作用,java,spring,hibernate,validation,Java,Spring,Hibernate,Validation,我做错了什么?测试不起作用 @RunWith(SpringJUnit4ClassRunner.class) public class CustomerTest extends BaseIntegrationTest { @Autowired private ICustomerService service; @Autowired public static Validator validator; @Test public void test_A
@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));
}
}
这是我的接口类:
@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);
}
@已验证
公共接口ICCustomerService
{
公共列表findbyStr(
@NotEmpty(message=“{column.notvalid}”)
@NotNull(message=“{column.notvalid}”)
字符串列,
@NotEmpty(message=“{column.notvalid}”)
@NotNull(message=“{value.notvalid}”)
字符串值);
}
这是我的实现类:
@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);
}
}
@Service(“customerService”)
@范围(value=“singleton”,proxyMode=ScopedProxyMode.TARGET\u类)
公共类CustomerService实现ICCustomerService{
@自动连线
私人历史道道;
@凌驾
公共列表findbyStr(字符串列,字符串值){
返回dao.findByString(Customer.class、column、value);
}
}
这是我的单元测试课:
JUNIT测试不起作用
@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));
}
}
@RunWith(SpringJUnit4ClassRunner.class)
公共类CustomerTest扩展了BaseIntegrationTest{
@自动连线
私人客户服务;
@自动连线
公共静态验证器;
@试验
公共无效测试_A6_CustomerFindByStrNull(){
列表结果=service.findbyStr(null,null);
Set constraintViolations=验证程序
.验证(服务);
assertEquals(0,constraintViolations.size());
assertEquals(“Die angegebene E-Mail-address is fehlerhaft.”,
constraintViolations.iterator().next().getMessage());
assertNotNull(结果);
assertNotNull(result.get(1));
}
}
我很确定,当注释位于对象的方法上时,您不能测试约束violations
,因为它应该抛出方法约束violationexception
。您应该尝试以下方法:
@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");
}
}
什么不起作用?您是否得到一个
NullPointerException
?它需要得到一个错误消息,并且不会发生。如果我输入错误,它的assertEquals(1,constraintViolations.size());assertEquals(“Die angegebene E-Mail-adrese ist fehlerhaft.”,constraintViolations.iterator().next().getMessage())代码>最后断言(结果为(nullValue())
java.lang.AssertionError:预期值:但实际值:org.junit.Assert.fail(Assert.java:88)org.junit.Assert.failNotEquals(Assert.java:743)
你是对的,我在互联网上也找到了这个解决方案,但在hibernate validator 5.0.1版中找不到类MethodConstraintViolationException
。最终的MethodConstraintViolationException无法解析为类型
似乎hibernate 5+支持只会在Spring 4中添加。它只适用于hibernate Validator的4.x版本。我刚刚试用了4.3.1.final版本,但没有发现它应该适用于4.3.1: