springBoot整合Junit框架

 文章目录

目录

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

二测试类编写

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

2.创建启动类文件

3.创建controller 层的测试类

4.创建测试类

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



    com.sun
    springboot-junit
    1.0-SNAPSHOT
    4.0.0

    
        8
        8
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
            RELEASE
            test
        
    

测试类编写

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("测试问题"));
    }
}

你可能感兴趣的:(springBoot,spring,boot,junit)