我有一个 DTO 类,其中包含两个日期字段。两者都用@NotNull和注释@DateTimeFormat。
我正在执行 TDD,我注意到我的NotNull错误消息已成功返回,但是当我在单元测试中传入一个日期时,它几乎接受任何内容,即使它与我的模式不匹配。
有趣的是,当我以 thymeleaf 形式进行测试时,它可以正常工作,并返回我期望的错误消息,日期格式错误。
我假设这与 spring 在我只对 DTO 进行单元测试时没有应用 DateTimeFormat 有关系,但是为什么我的 not null 会按预期工作?
我在下面提供了 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
}
Run Code Online (Sandbox Code Playgroud)
单元测试:
public class HourTrackerTest {
private static final String HOURS_INPUT_FORMAT = "hh:mma";
private …Run Code Online (Sandbox Code Playgroud) 我正在努力自学java,在使用类编写代码时遇到了这个错误
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at StateCalculator.getOperand(StateCalculator.java:29)
at StateCalculator.main(StateCalculator.java:77)
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
import java.util.Scanner;
public class StateCalculator {
private double currentValue = 0;
//Initialize to 0
public StateCalculator() {
}
public static int displayMenu() {
Scanner keyboard = new Scanner(System.in);
int menuChoice = 0;
do {
System.out.print("Menu\n 1. Add\n 2. Subtract\n 3. Multiply\n 4. Divide\n 5.Clear\n 6. Quit\n What would you like to do?: ");
menuChoice = keyboard.nextInt();
} while(menuChoice < 1 …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
/**
* setup() reads in the next command line, separating it into distinct tokens
* using whitespace as delimiters. It also sets the args parameter as a
* null-terminated string.
*/
void setup(char inputBuffer[], char *args[],int *background)
{
int length, /* Number of characters in the command line */
i, /* Loop index for inputBuffer array */
start, /* Index …Run Code Online (Sandbox Code Playgroud)