今天,我的私人项目有问题。虽然实体每次接收NullPointerException都会节省DTO的使用量。
通常,直到现在,我一直在使用不带ID标识符的DTO,但最后却发现我需要一个ID字段,因为在GUI端需要它来修改或删除一个特定的对象。
以下具有DTO的实体:
@RequiredArgsConstructor
public class DiscountCode {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NonNull private LocalDateTime startDate;
@NonNull private LocalDateTime endDate;
@NonNull @NotBlank private String code;
private Integer quantity;
@NonNull private BigDecimal rate;
@NonNull
@Enumerated(EnumType.STRING)
private DiscountCodeStatus status;
}
@RequiredArgsConstructor
public class DiscountCodeDto {
private Long id;
@NonNull private LocalDateTime startDate;
@NonNull private LocalDateTime endDate;
@NonNull private String code;
private Integer quantity;
@NonNull private BigDecimal rate;
@NonNull
@Enumerated(EnumType.STRING)
private DiscountCodeStatus status;
}
具有完整的lombok实用程序。
下面的映射器:
public class DiscountCodeMapper {
private DiscountCodeMapper() {
throw new UnsupportedOperationException();
}
public static DiscountCodeDto toDto(DiscountCode discountCode) {
return isNull(discountCode)
? null
: DiscountCodeDto.builder()
.id(discountCode.getId())
.startDate(discountCode.getStartDate())
.endDate(discountCode.getEndDate())
.code(discountCode.getCode())
.quantity(discountCode.getQuantity())
.rate(discountCode.getRate())
.build();
}
public static DiscountCode fromDto(DiscountCodeDto discountCodeDto) {
return isNull(discountCodeDto)
? null
: DiscountCode.builder()
.id(discountCodeDto.getId())
.startDate(discountCodeDto.getStartDate())
.endDate(discountCodeDto.getEndDate())
.code(discountCodeDto.getCode())
.quantity(discountCodeDto.getQuantity())
.rate(discountCodeDto.getRate())
.build();
}
}
以下服务方法:
@Service
@RequiredArgsConstructor
public class DiscountCodeService {
private final DiscountCodeRepository discountCodeRepository;
public DiscountCodeDto add(final DiscountCodeDto discountCodeDto) {
checkArgument(nonNull(discountCodeDto), "Expected not null discountCodeDto");
DiscountCode discountCode =
discountCodeRepository.save(Objects.requireNonNull(fromDto(discountCodeDto)));
return DiscountCodeDto.builder()
.id(discountCode.getId())
.startDate(discountCode.getStartDate())
.endDate(discountCode.getEndDate())
.code(discountCode.getCode())
.quantity(discountCode.getQuantity())
.rate(discountCode.getRate())
.status(discountCode.getStatus())
.build();
}
JUnit 5,带有测试用例:
class DiscountCodeTest {
private DiscountCodeService discountCodeService;
private DiscountCodeRepository discountCodeRepository = mock(DiscountCodeRepository.class);
@BeforeEach
void setUp() {
discountCodeService = new DiscountCodeService(discountCodeRepository);
}
@AfterEach
void tearDown() {
reset(discountCodeRepository);
}
@Test
void shouldAddDiscountCode() {
// given
Long expectedDiscountCodeId = 1L;
DiscountCode expectedDiscountCode =
new DiscountCode(
expectedDiscountCodeId,
LocalDateTime.now().plusDays(1),
LocalDateTime.now().plusDays(5),
"7142",
null,
BigDecimal.valueOf(100),
DiscountCodeStatus.ACTIVE);
DiscountCodeDto givenDiscountCodeDto =
new DiscountCodeDto(
LocalDateTime.now().plusDays(1),
LocalDateTime.now().plusDays(5),
"7142",
BigDecimal.valueOf(100),
DiscountCodeStatus.ACTIVE);
when(discountCodeRepository.save(any())).thenReturn(expectedDiscountCode);
// when
DiscountCodeDto actualDiscountCode = discountCodeService.add(givenDiscountCodeDto);
// then
verify(discountCodeRepository).save(any(DiscountCode.class));
assertEquals(givenDiscountCodeDto, actualDiscountCode);
}
当前,在每种情况下,我都会收到该行的NullPointerException:
DiscountCodeDto实际DiscountCode = discountCodeService.add(givenDiscountCodeDto);
在。我要做的就是在不使用ID到DTO对象的情况下保存使用DTO的实体。
我试图用邮递员用法执行add方法,但是在这种情况下,我都收到NullPointerException。
在此先感谢您的帮助。