英文:
Bean serialization issue while running test case
问题
当从浏览器访问端点时,它可以正常工作,但测试用例失败。请查看以下代码:
Controller类:
@RestController
public class EmployeeController {
@Autowired
private EmployeeDTO employeeDTO;
@GetMapping("/getEmployee")
public EmployeeDTO getEmployee(){
employeeDTO.setEmpId(1);
employeeDTO.setEmpName("Bob");
return employeeDTO;
}
EmployeeDTO类:
@Getter
@Setter
@NoArgsConstructor
@Component
public class EmployeeDTO {
private int empId;
private String empName;
}
EmployeeControllerTest类:
@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"])
测试用例失败,不确定原因。
英文:
when hit the endpoint from the browser it is working fine but test case failing. please find the code below:
Controller class:
@RestController
public class EmployeeController {
@Autowired
private EmployeeDTO employeeDTO;
@GetMapping("/getEmployee")
public EmployeeDTO getEmployee(){
employeeDTO.setEmpId(1);
employeeDTO.setEmpName("Bob");
return employeeDTO;
}
EmployeeDTOClass:
@Getter
@Setter
@NoArgsConstructor
@Component
public class EmployeeDTO {
private int empId;
private String empName;
}
EmployeeControllerTest class:
@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>
Exception when testcase is run:
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"])
test case failing not sure why.
答案1
得分: 0
在你的示例中,EmployeeDTO 是一个组件。通常不习惯将 bean 作为方法的响应对象返回。虽然Spring内置的对象映射器处理了你的对象并将其序列化为JSON,但显然Mockito内置的序列化器/反序列化器无法处理它。
所以,如果从 EmployeeDTO 中删除 @Component 注解,那么一切都将正常工作。
在控制器中,可以这样做:
@GetMapping("/getEmployee")
public EmployeeDTO getEmployee(){
var employeeDTO = new EmployeeDTO();
employeeDTO.setEmpId(1);
employeeDTO.setEmpName("Bob");
return employeeDTO;
}
不要忘记删除 @Autowired 部分。
希望这对你有帮助。
英文:
In your example, EmployeeDTO is a component. Usually it is not customary to return a bean as a response object for method. Although spring built-in object mapper handled your object and serialized it in json, apparently mockito built-in serializer/deserializer could not managed with it.
So, if you remove @Component annotation from EmployeeDTO then everything will work fine.
In a contoller, do something like:
@GetMapping("/getEmployee")
public EmployeeDTO getEmployee(){
var employeeDTO = new EmployeeDTO();
employeeDTO.setEmpId(1);
employeeDTO.setEmpName("Bob");
return employeeDTO;
}
And do not forget to remove @Autowired part.
Hope it will helps you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论