当点击端点从浏览器它是工作fine但是测试用例失败.请发现这代码下面:
控制器类:
@RestController
public class EmployeeController {
@Autowired
private EmployeeDTO employeeDTO;
@GetMapping("/getEmployee")
public EmployeeDTO getEmployee(){
employeeDTO.setEmpId(1);
employeeDTO.setEmpName("Bob");
return employeeDTO;
}
员工DTO类:
@Getter
@Setter
@NoArgsConstructor
@Component
public class EmployeeDTO {
private int empId;
private String empName;
}
雇员控制器测试类:
@WebMvcTest
public class EmployeeControllerTest {
@Autowired
private MockMvc mockmvc;
@MockBean
private EmployeeDTO employeeDTO;
@Test
public void testGetEmployee() throws Exception {
RequestBuilder request = MockMvcRequestBuilders.get("/getEmployee");
mockmvc.perform(request)
.andExpect(status().isOk());
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spring</groupId>
<artifactId>tennis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tennis</name>
<description>Demo project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
运行测试用例时出现异常:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.mockito.internal.creation.bytebuddy.ByteBuddyCrossClassLoaderSerializationSupport]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.mockito.internal.creation.bytebuddy.ByteBuddyCrossClassLoaderSerializationSupport and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.spring.tennis.model.EmployeeDTO$MockitoMock$MCOb7jmM["mockitoInterceptor"]->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["serializationSupport"])
测试用例失败,不确定原因。
1条答案
按热度按时间d6kp6zgx1#
在你的例子中,EmployeeDTO是一个组件。通常返回一个 bean 作为方法的响应对象是不习惯的。尽管spring内置的对象Map器处理了你的对象并在json中序列化了它,但显然mockito内置的序列化器/反序列化器不能用它来管理。
因此,如果从 EmployeeDTO 中删除 @Component 注解,则一切都将正常工作。
在控制器中,执行以下操作:
不要忘记拆下 @Autowired 零件。
希望对你有帮助。