springBoot整合Junit框架

 文章目录

目录

一、springBoot相关依赖与Junit相关依赖

二测试类编写

1.创建配置文件application.yml文件

2.创建启动类文件

3.创建controller 层的测试类

4.创建测试类

一、springBoot相关依赖与Junit相关依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <groupId>com.sun</groupId>
    <artifactId>springboot-junit</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
<!--    锁定版本-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
    </parent>
    <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>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

测试类编写

1.创建配置文件application.yml文件

#端口号
server:
  port: 8089
#服务名称
spring:
  application:
    name: junit

2.创建启动类文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * TODO
 *
 * @author xiaowensun
 * @date 2023/6/11  21:47
 */
@SpringBootApplication
public class JunitApplication {
    public static void main(String[] args) {
        SpringApplication.run(JunitApplication.class,args);
    }
}

3.创建controller 层的测试类


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author xiaowensun
 * @date 2023/6/11  21:48
 */
@RestController
@RequestMapping("/junit")
public class JunitController {
    @GetMapping("/test")
    public String junitTest(String test){
        return  test;
    }
}

4.创建测试类

package com.sun.junit;

import com.sun.controller.JunitController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * TODO
 *
 * @author xiaowensun
 * @date 2023/6/11  16:10
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSun {
    @Autowired
    private JunitController junitController;

    @Test
    public void test(){
        System.out.println(junitController.junitTest("测试问题"));
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当前可以使用Spring Boot整合SSM框架。首先,你可以将Spring和MyBatis整合Spring Boot中。Spring Boot会自动为你处理Spring和MyBatis的配置,你只需要保留一些必要的配置即可。具体来说,你可以使用Spring Boot的starter依赖来引入Spring和MyBatis的相关模块。这样,你就可以使用Spring的依赖注入和事务管理功能,以及MyBatis的ORM功能。 接下来,你可以使用Spring Boot整合Spring MVC。Spring Boot提供了一个自动配置的MVC模块,你可以直接使用它来处理HTTP请求和响应。你只需要在Controller类上添加@Controller注解,并在方法上添加@RequestMapping注解来定义请求映射。此外,你还可以使用Spring Boot的starter依赖来引入其他需要的模块,比如JSON序列化和数据校验等。 最后,你可以使用Spring Boot整合JUnit进行测试。你可以使用@SpringBootTest注解标记测试类,并指定主程序引导类。通过自动装配业务方法,你可以在测试方法中直接调用你的业务逻辑。这样,你就可以方便地进行单元测试和集成测试了。 综上所述,你可以使用Spring Boot整合SSM框架,实现快速开发和测试。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [基于SpringBoot的SSM整合](https://blog.csdn.net/ware00/article/details/125689235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值