十二 . Springboot-Junit test

本文介绍了如何在Spring Boot项目中进行单元测试,包括配置依赖、编写测试代码等关键步骤,并提供了具体的测试案例。

​ 使用了Springboot后,就不能用原来的Spring的测试了,需要使用springboot的测试

12.1 步骤分析

1 准备一个springboot项目
	略过
2 pom导入对应stater
3 写代码测试

12.2 pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

12.3 写代码测试

package org.yaosang.org.service.impl;

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;
import org.yaosang.App;
import org.yaosang.org.domain.Department;
import org.yaosang.org.service.IDepartmentService;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class DepartmentServiceImplTest {
    @Autowired
    private IDepartmentService departmentService;

    @Test
    public void add() {
        departmentService.add(new Department("屌丝部"));
    }

    @Test
    public void del() {
        Department department = departmentService.getById(1L);
        System.out.println(department);
        departmentService.del(1L);
        department = departmentService.getById(1L);
        System.out.println(department);

    }

    @Test
    public void update() {
        Department department = departmentService.getById(1L);
        System.out.println(department);
        department.setName(department.getName()+"-edit");
        departmentService.update(department);
        department = departmentService.getById(1L);
        System.out.println(department);
    }

    @Test
    public void getById() {
        System.out.println(departmentService.getById(1L));
    }

    @Test
    public void getAll() {
        departmentService.getAll().forEach(System.out::println);
    }
}

12.4 小结

​ 本章节讲了Springboot-test的实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值