Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对spring datetimeformat验证进行单元测试_Java_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 如何对spring datetimeformat验证进行单元测试

Java 如何对spring datetimeformat验证进行单元测试,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,我有一个DTO类,包含两个日期字段。两者都用@NotNull和@DateTimeFormat注释 我正在做TDD,我注意到我的NotNull错误消息已成功返回,但当我在单元测试中传入一个日期时,它几乎接受任何内容,即使它与我的模式不匹配 有趣的是,当我在my thymeleaf表单中测试时,它工作正常,返回了我期望的错误消息,其中包含错误的日期 我假设这与spring在单元测试DTO时没有应用DateTimeFormat有关,但是为什么我的NOTNULL会像预期的那样工作呢 我在下面提供了DTO

我有一个DTO类,包含两个日期字段。两者都用
@NotNull
@DateTimeFormat
注释

我正在做TDD,我注意到我的
NotNull
错误消息已成功返回,但当我在单元测试中传入一个日期时,它几乎接受任何内容,即使它与我的模式不匹配

有趣的是,当我在my thymeleaf表单中测试时,它工作正常,返回了我期望的错误消息,其中包含错误的日期

我假设这与spring在单元测试DTO时没有应用DateTimeFormat有关,但是为什么我的NOTNULL会像预期的那样工作呢

我在下面提供了DTO的代码

import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import java.util.Date;

public class HourTracker {
@NotNull(message = "start time cannot be null")
@DateTimeFormat(pattern = "hh:mma")
private Date startTime;
@NotNull(message = "end time cannot be null")
@DateTimeFormat(pattern = "hh:mma")
private Date endTime;

//getters and setters
}
单元测试:

public class HourTrackerTest {
private static final String HOURS_INPUT_FORMAT = "hh:mma";
private Validator validator;
private HoursTracker tested;

@Before
public void setUp() throws Exception {
    tested = new HoursTracker();
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    validator = factory.getValidator();
}

@Test
public void testValidTimeInputs() throws Exception {
    SimpleDateFormat timeFormatForDate = new SimpleDateFormat(HOURS_INPUT_FORMAT);
    Date validTimeInput = timeFormatForDate.parse("12:30pm");
    tested.setStartTime(validTimeInput);
    tested.setEndTime(validTimeInput);
    assertEquals("Start time was not correctly set", validTimeInput, tested.getStartTime());
    assertEquals("End time was not correctly set", validTimeInput, tested.getStartTime());
}

@Test
public void testNullStartTimeInputErrorMessage() throws Exception {
    tested.setStartTime(null);
    Set<ConstraintViolation<HoursTrackingForm>> violations = validator.validate(tested);
    assertFalse("No violation occurred, expected one", violations.isEmpty());
    assertEquals("Incorrect error message",
            "Please enter a valid time in AM or PM",
            violations.iterator().next().getMessage()
    );
}

@Test
public void testNullEndTimeInputErrorMessage() throws Exception {
    tested.setEndTime(null);
    Set<ConstraintViolation<HoursTrackingForm>> violations = validator.validate(tested);
    assertFalse("No violation occurred, expected one", violations.isEmpty());
    assertEquals("Incorrect error message",
            "Please enter a valid time in AM or PM",
            violations.iterator().next().getMessage()
    );
}

}
HourTrackerTest公共类{
专用静态最终字符串小时数\u输入\u格式=“hh:mma”;
私人验证器;
私人小时追踪器测试;
@以前
public void setUp()引发异常{
已测试=新的小时跟踪器();
ValidatorFactory=Validation.buildDefaultValidatorFactory();
validator=factory.getValidator();
}
@试验
public void testValidTimeInputs()引发异常{
SimpleDataFormat timeFormatForDate=新的SimpleDataFormat(小时\输入\格式);
Date validTimeInput=timeFormatForDate.parse(“12:30pm”);
已测试。设置开始时间(有效时间输入);
已测试。setEndTime(有效时间输入);
assertEquals(“启动时间设置不正确”,validTimeInput,tested.getStartTime());
assertEquals(“未正确设置结束时间”,validTimeInput,tested.getStartTime());
}
@试验
public void testNullStartTimeInputErrorMessage()引发异常{
已测试。设置开始时间(空);
设置冲突=validator.validate(已测试);
assertFalse(“未发生冲突,应为一次”,invalitions.isEmpty());
assertEquals(“错误错误消息”,
“请在上午或下午输入有效时间”,
违例情况。迭代器().next().getMessage()
);
}
@试验
public void testNullEndTimeInputErrorMessage()引发异常{
tested.setEndTime(空);
设置冲突=validator.validate(已测试);
assertFalse(“未发生冲突,应为一次”,invalitions.isEmpty());
assertEquals(“错误错误消息”,
“请在上午或下午输入有效时间”,
违例情况。迭代器().next().getMessage()
);
}
}

简单回答,您不能在Spring容器外部测试Spring约束。我试图以测试javax验证的方式进行测试

您能为单元测试提供代码吗?我不确定
@DateTimeFormat
注释的用途,但它看起来不像是用于验证的——请注意,它不在
javax.validation
包中:@DaveyDaveDave我想这就是我问题的答案,我想我已经知道了。DateTimeFormat注释是一个在mvc端工作的spring框架约束,由于此DTO测试没有在spring容器中运行,因此无法对其进行测试。我想我的问题真的是,那么,从我的mvc测试中测试这一点的唯一方法是什么?