【发布时间】:2021-01-21 12:18:38
【问题描述】:
我正在使用 SpringBoot 来创建应用程序。
应用程序包中的组件是:
@Getter
@Builder
@Component
@AllArgsConstructor
public class ContentDTO {
private UUID uuid;
private ContentAction contentAction;
private String payload;
}
在测试包中,我创建了用于创建 tets-data 的 utils-class:
@Component
public class TestData {
private UUID uuid = null;
private final ContentAction contentAction = ContentAction.valueOf("SEND_NEW");
private final String payload = "New message";
private ContentDTO contentDTO;
public TestData() {
contentDTO =
ContentDTO.builder().uuid(uuid).contentAction(contentAction).payload(payload).build();
}
public ContentDTO getContentDTO() {
return contentDTO;
}
}
和测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {InfoDiodeSinkApplication.class, ContentDTO.class, TestData.class})
public class InfoDiodeSinkApplicationTest {
@Autowired
private InfoDiodeSinkApplication infoDiodeSinkApplication;
@Autowired
private ContentDTO contentDTO;
@Autowired
private TestData testData;
public void contextLoads() {
}
@Test(expected = Exception.class)
public void testDtoHandler() {
contentDTO = testData.getContentDTO();
Mockito.doThrow(new
EOFException()).when(infoDiodeSinkApplication).dtoHandler(contentDTO);
}
}
但是当我运行测试时,我收到:
Error creating bean with name 'contentDTO': Unsatisfied dependency expressed through constructor parameter 0
原因:
No qualifying bean of type 'java.util.UUID' available: expected at least 1 bean which qualifies as autowire candidate
但是 java.util.UUID 包含在标准 java-lib 中。而且我认为 SpringBoot 能够为来自标准库的所有 bean 创建默认配置。
我是否理解正确,Spring 建议我为标准 UUID 创建自定义 bean 配置?我做错了什么?
【问题讨论】:
标签: spring-boot javabeans autowired applicationcontext