Spring Boot生成单元测试文件和打包

1,372 阅读1分钟

一、生成单元测试文件

1)在需要单元测试的类上点击鼠标,使用Alt+Enter键

2)选择 Create Test

package com.example.demo.service.impl;import com.example.demo.model.TestModel;
import lombok.extern.slf4j.Slf4j;import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;@Slf4j@SpringBootTestclass TestServiceImplTest {    
@Autowired    
private TestServiceImpl testService;    
@BeforeEach    
void setUp() {        
    log.info("setUp");    
}    
@AfterEach    
void tearDown() {        
    log.info("tearDown");    
}    
@Test    
void testMethod() {        
    List<TestModel> a = testService.testMethod();       
for (TestModel b:a             
    ) {            
        log.info("{}",b);        
    }    
}}

3)勾选需要测试的方法

4)重写生成的方法

5)点击方法做出即可运行单元测试方法

二、打包

打包两种选择:

第一种是双击package直接打出jar包,打包后文件在项目更目录/target/ 下

第二种是双击install,这种是会将原来target目录下的文件全部删除再打包