我有一个控制器,我正在编写测试。一切似乎都编译得很好,但后来我得到了一个运行时错误,说下面的话。我不记得我把我的DTO转换为字符串的任何地方。堆栈跟踪显示,这个“ResultActions response = mockMvc.perform(post(“/app/API/agency/signup”).contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(agency))”是有问题的一行,但我不确定原因。
Request processing failed; nested exception is java.lang.ClassCastException: class com.zshift.io.dto.records.AgencyDTORecord cannot be cast to class java.lang.String (com.zshift.io.dto.records.AgencyDTORecord is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
org.springframework.web.util.NestedServletException
at com.zshift.io.tests.agency.AgencyIntegrationTest.testAgencySignup(AgencyIntegrationTest.java:77)
Caused by: java.lang.ClassCastException: class com.zshift.io.dto.records.AgencyDTORecord cannot be cast to class java.lang.String (com.zshift.io.dto.records.AgencyDTORecord is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
at com.zshift.io.tests.agency.AgencyIntegrationTest.testAgencySignup(AgencyIntegrationTest.java:77)
控制器看起来像这样。
@PostMapping(path = "/signup", produces = { MediaType.APPLICATION_JSON }, consumes = { MediaType.APPLICATION_JSON })
public ResponseEntity addAgency(@RequestBody AgencyDTORecord record) throws AlreadyExistsException {
return new ResponseEntity<>(service.saveAgency(record), HttpStatus.CREATED);
}
我的DTO类看起来像这样
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
public class AgencyDTORecord {
private String agencyAbout;
private String agencyAddress;
private String agencyEmail;
private String agencyLocation;
private String agencyName;
private String agencyPhone;
private String agencyLatitude;
private String agencyLongitude;
private String agencySecret;
private String agencyPostCode;
private Long agencyID;
private String agencyRoleName;
private String agencyStaffName;
private String agencyStaff;
}
这是我的测试课
@WebMvcTest(controllers = AgencyController.class)
@AutoConfigureMockMvc(addFilters = false)
@ExtendWith(MockitoExtension.class)
public class AgencyIntegrationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AgencyService service;
@Autowired
private ObjectMapper mapper;
private AgencyDTORecord agency;
@BeforeEach
public void init()
{
agency = new AgencyDTORecord();
agency.setAgencyAbout("For testing");
agency.setAgencyAddress("test address");
agency.setAgencyEmail("agency@test.com");
agency.setAgencyLatitude("1000");
agency.setAgencyLongitude("2000");
agency.setAgencyName("The Test Agency");
agency.setAgencyPhone("3000");
agency.setAgencyPostCode("CT92LA");
agency.setAgencyRoleName("Admin");
agency.setAgencySecret("1234");
agency.setAgencyStaff("Alao");
agency.setAgencyID(Long.parseLong("0"));
agency.setAgencyStaffName("Alao Akala");
}
@Test
public void testAgencySignup() throws Exception {
given(service.saveAgency(any())).will(invocation -> invocation.getArgument(0));
ResultActions response = mockMvc.perform(post("/app/api/agency/signup")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(agency))
);
response.andExpect(status().isCreated())
.andExpect(jsonPath("$.agencyName", is(agency.getAgencyName())))
.andExpect(jsonPath("$.agencyPhone", is(agency.getAgencyPhone())));
}
1条答案
按热度按时间iyzzxitl1#
我修复了它。我使用Gson将代理DTO转换为JSON,然后将其作为内容对象传递。