【springboot】关于@Test的代码测试

常用的测试框架

Junit

Junit4

参考资料

  • 官网:https://junit.org/junit4/
  • 引入:https://blog.csdn.net/gakki_200/article/details/106413351
  • 使用说明:https://blog.csdn.net/weixin_39584888/article/details/111938896

在这里插入图片描述

@Test注解

import org.junit.Test;

import static org.junit.Assert.*;

public class StudentOperationMySqlImplTest {
    IStudentDAO studentDAO = (IStudentDAO) DAOFactory.newInstance("IStudentDAO");

    @Test//@Test注解
    public void addStudent() {
        Student student = new Student(1007l, "赵六", 17, "女", DateUtils.strToUtil("2002-02-13"), "北京市海淀区");
        assertEquals(new Integer(1),studentDAO.addStudent(student));//断言测试
    }
    }

除此之外,还有@Before(每一个测试方法调用前必执行的方法)等注解
详见:https://zhuanlan.zhihu.com/p/86624354

Springboot

在Springboot2.2之前,集成了Junit4(之后是junit5)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
Service层
@RunWith(SpringRunner.class)
@SpringBootTest
public class LearnServiceTest {

    @Autowired
    private LearnService learnService;
    
    @Test
    public void getLearn(){
        LearnResource learnResource=learnService.selectByKey(1001L);
        Assert.assertThat(learnResource.getAuthor(),is("博客"));
    }
}
Controller层

MockMvc实现了对Http请求的模拟

@RunWith(SpringRunner.class)
@SpringBootTest

public class LearnControllerTest {
    @Autowired
    private WebApplicationContext wac;

    private MockMvc mvc;
    


    @Before
    public void setupMockMvc(){
        mvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象
        
    }

    /**
     * 新增教程测试用例
     * @throws Exception
     */
    @Test
    public void addLearn() throws Exception{
        String json="{\"author\":\"HAHAHAA\",\"title\":\"Spring\",\"url\":\"http://tengj.top/\"}";
        mvc.perform(MockMvcRequestBuilders.post("/learn/add")
                    .accept(MediaType.APPLICATION_JSON_UTF8)
                    .content(json.getBytes()) //传json参数
                    .session(session)
            )
           .andExpect(MockMvcResultMatchers.status().isOk())
           .andDo(MockMvcResultHandlers.print());
    }
    }

Junit4和Junit5的区别

  • 忽略测试用例执行
  • RunWith 配置
  • @Before、@BeforeClass、@After、@AfterClass 被替换

Junit5

参考资料

  • 官网:https://junit.org/junit5/

SpringBoot

在Springboot2.2之后,集成了Junit5

参考资料:

  • https://blog.csdn.net/weixin_59654772/article/details/123309325
  • https://cloud.tencent.com/developer/article/1879367
Controller层
@SpringBootTest
class UploadFileControllerTest {
    @Autowired
    private UploadFileController fileController;

    @Test
    void getFileList() throws Exception {
        Result result = fileController.getFileList();
        System.out.println(result);

    }

}

或者

@SpringBootTest
class UploadFileControllerTest {
    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @BeforeEach
    void init() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).alwaysDo(print()).build();
    }

    @Test
    void getFileList() throws Exception {
        mockMvc.perform(get("/fileList")).andExpect(status().isOk());
    }
    

}

或者

@SpringBootTest
@AutoConfigureMockMvc
class UploadFileControllerTest {
    @Autowired
    private MockMvc mockMvc;
    
    @Test
    void getFileList() throws Exception {
        mockMvc.perform(get("/fileList")).andExpect(status().isOk());
    }
    
}
Service层/dao层

一般是对接口进行测试

@SpringBootTest
class FileMapperTest {
    @Autowired
    private FileMapper fileMapper;

    @Test
    void selectFileList() {
        List<FilePO> filePOS = fileMapper.selectFileList();
        for (FilePO filePO : filePOS) {
            System.out.println(filePO);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值