Bean serialization issue while running test case.

huangapple go评论71阅读模式
英文:

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(&quot;/getEmployee&quot;)
public EmployeeDTO getEmployee(){
employeeDTO.setEmpId(1);
employeeDTO.setEmpName(&quot;Bob&quot;);
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(&quot;/getEmployee&quot;);
mockmvc.perform(request)
.andExpect(status().isOk());
}
}

pom.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.7.8&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.spring&lt;/groupId&gt;
&lt;artifactId&gt;tennis&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;tennis&lt;/name&gt;
&lt;description&gt;Demo project&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
&lt;version&gt;5.7.0&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
&lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
&lt;version&gt;5.7.0&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;version&gt;1.18.26&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

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[&quot;mockitoInterceptor&quot;]-&gt;org.mockito.internal.creation.bytebuddy.MockMethodInterceptor[&quot;serializationSupport&quot;])

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(&quot;/getEmployee&quot;)
public EmployeeDTO getEmployee(){
var employeeDTO = new EmployeeDTO();
employeeDTO.setEmpId(1);
employeeDTO.setEmpName(&quot;Bob&quot;);
return employeeDTO;
}

And do not forget to remove @Autowired part.

Hope it will helps you.

huangapple
  • 本文由 发表于 2023年2月26日 22:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定