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容器或其他虚拟化环境,还需要检查网络设置是否允许容器内部的服务与宿主机进行通信。

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 );
    }
}

相关推荐

  1. idea自动生成单元测试工具

    2024-06-12 11:32:01       31 阅读
  2. Spring Boot项目Service类单元测试自动生成

    2024-06-12 11:32:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-12 11:32:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 11:32:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 11:32:01       82 阅读
  4. Python语言-面向对象

    2024-06-12 11:32:01       91 阅读

热门阅读

  1. linux中sed命令和awk命令如何使用??????

    2024-06-12 11:32:01       29 阅读
  2. Django里的模板变量

    2024-06-12 11:32:01       27 阅读
  3. SQL题——连续问题

    2024-06-12 11:32:01       19 阅读
  4. 【编译安卓ROM常见错误和注意事项】

    2024-06-12 11:32:01       24 阅读
  5. C#(C Sharp)学习笔记_继承【十七】

    2024-06-12 11:32:01       35 阅读
  6. C++中的装饰器模式

    2024-06-12 11:32:01       25 阅读
  7. fabric.util.enlivenObjects是什么意思

    2024-06-12 11:32:01       22 阅读
  8. Mongodb篇(中)(3)

    2024-06-12 11:32:01       27 阅读
  9. 数据仓库之分层存储

    2024-06-12 11:32:01       26 阅读
  10. PHP框架详解-symfony框架

    2024-06-12 11:32:01       24 阅读
  11. Kotlin 协程:从基础概念到开发实践

    2024-06-12 11:32:01       29 阅读
  12. 架构演化过程中,如何确保核心功能不受影响?

    2024-06-12 11:32:01       23 阅读
  13. 如何在小程序中实现页面之间的跳转

    2024-06-12 11:32:01       24 阅读
  14. Web前端推送功能:深入剖析与应用实践

    2024-06-12 11:32:01       26 阅读