idea自动生成单元测试工具

idea自动生成单元测试工具

Squaretest插件(收费)

1.File------>Settings------>Plugins,搜索Squaretest,然后install就好了,插件安装完成后需要重启一下

2.打开class-右键--选择Generate--生成test

TestMe插件(免费)

TestMe插件可以智能分析被测试类的依赖类,结合Mockito+Junit等单元测试框架,生成Mock/InjectMocks依赖关系,自动生成单元测试类。

1.File------>Settings------>Plugins,搜索TestMe,然后install就好了,插件安装完成后需要重启一下。

2.打开class-右键--选择testme--生成test

启动springboot应用调用rest接口

  • 使用@SpringBootTest启动整个应用。
  • 使用@TestPropertySource来指定一个名为application-test.properties的配置文件,该文件应该位于classpath:下(即类路径根目录)。
  • 使用TestRestTemplate来发送一个POST请求到/home/update端点,并检查响应状态和数据。
  • 使用 @LocalServerPort注解用于注入Spring Boot应用随机启动的端口号。

测试中的URL字符串现在包含${port}占位符,它会被实际的端口号替换。

确保你的HomeController和其他相关组件(如User和ResponseResult类)正确配置并且能够被Spring Boot应用识别。如果问题仍然存在,检查是否有防火墙规则阻止了本地端口的连接,或者是否还有其他服务正在占用相同的端口。如果使用的是Docker容器或其他虚拟化环境,还需要检查网络设置是否允许容器内部的服务与宿主机进行通信。

java 复制代码
package com.zrj.tools.platform.controller;

import com.zrj.tools.platform.entity.ResponseResult;
import com.zrj.tools.platform.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:application.properties")
class HomeControllerTest {

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void testUpdateEndpoint() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType( org.springframework.http.MediaType.APPLICATION_JSON );

        User user = new User();
        user.setName( "John Doe" );

        HttpEntity<User> entity = new HttpEntity<>( user, headers );
        ResponseEntity<ResponseResult<User>> response = restTemplate.exchange(
                "http://localhost:" + port + "/home/update", HttpMethod.POST, entity,
                new ParameterizedTypeReference<ResponseResult<User>>() {
                }
        );

        assertThat( response.getStatusCodeValue() ).isEqualTo( 200 );
        assertThat( response.getBody().getData() ).isEqualTo( user );
    }
}
相关推荐
潇凝子潇6 小时前
IntelliJ IDEA设置编码集
java·ide·intellij-idea
❀͜͡傀儡师6 小时前
IntelliJ IDEA 接入 DeepSeek帮助你更好编码
java·ide·intellij-idea
我命由我123456 小时前
IDEA - Windows IDEA 代码块展开与折叠(基础折叠操作、高级折叠操作)
java·笔记·后端·java-ee·intellij-idea·学习方法·intellij idea
biubiubiu07066 小时前
idea无法识别Maven项目
java·maven·intellij-idea
种棵二叉树7 小时前
Idea 查找引用jar包依赖来源的Maven pom坐标
maven·intellij-idea·jar
雪碧聊技术8 小时前
在SpringBoot项目中,使用单元测试@Test
java·spring boot·单元测试
爱吃烤鸡翅的酸菜鱼12 小时前
【Spring Boot】配置实战指南:Properties与YML的深度对比与最佳实践
java·spring boot·后端·spring·java-ee·intellij-idea
川石教育13 小时前
测试工程师要如何开展单元测试
软件测试·功能测试·单元测试·软件测试培训·软件测试教程
young log14 小时前
idea查看class文件源码
java·ide·intellij-idea
珹洺2 天前
MyBatis实战指南(一)MyBatis入门基础与利用IDEA从零开始搭建你的第一个MyBatis系统
java·数据库·ide·intellij-idea·mybatis