springboot单元测试

springboot单元测试

一、属性与参数测试

在进行单元测试的时候,需要对相关属性进行注入,我们可以将需要注入的数据写在yml文件里面。

application.yml文件

test:
  prop: testValue

PropertiesAndArgsTest类文件

package com.bubaiwantong;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

//properties 为当前测试添加测试属性值
//args 为当前命令行参数值 记得要加--才可以生效,不然会发生报错

//@SpringBootTest(properties = {"test.prop=testValue2"})
@SpringBootTest(args = {"--test.prop=testValue2"})
public class PropertiesAndArgsTest {

    @Value("${test.prop}")
    private String msg;

    @Test
    public void testProperties(){
        System.out.println(msg);
    }
}

注意:properties 为当前测试添加测试属性值,args 为当前命令行参数值 记得要加–才可以生效,不然会发生报错

二、注入bean测试

MsgConfig配置类

@Configuration
public class MsgConfig {

    @Bean
    public String msg(){
        return "bean msg";
    }

}

ConfigurationTest测试类

package com.bubaiwantong;

import com.bubaiwantong.config.MsgConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;

@SpringBootTest
@Import({MsgConfig.class})
public class ConfigurationTest {

    @Autowired
    private String msg;

    @Test
    public void testConfiguration(){
        System.out.println(msg);
    }

}

三、数据回滚

有时候我们需要用到数据库进行测试,而在测试的时候,测试的相关操作会对数据库进行一些增删改查的相关操作,这个时候测试会在数据库插入许多垃圾数据。而我们的目的是想测试完成之后,数据库里面的内容不发生变化,这个问题可以使用事务来进行解决。

在springboot中,我们可以使用下面两个注解来完成。

@Transactional
@Rollback(true)

application.yml配置文件

server:
  port: 80

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/ssmp_db?serverTimezone=GMT
      username: root
      password: 123456

mybatis-plus:
  global-config:
    db-config:
      table-prefix: tb_
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Book实体类

package com.bubaiwantong.domain;

import lombok.Data;

@Data
public class Book {
    private Integer id;
    private String name;
    private String type;
    private String description;

}

Dao层 :BookDao

package com.bubaiwantong.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bubaiwantong.domain.Book;
import org.apache.ibatis.annotations.Mapper;

/**
 * dao层
 * 使用mybatis-plus提供好的接口,实现basedMapper的泛型
 */

@Mapper
public interface BookDao extends BaseMapper<Book> {

}

Service接口

package com.bubaiwantong.service;

import com.bubaiwantong.domain.Book;

public interface BookService {

    public boolean save(Book book);
}

service实现类

package com.bubaiwantong.service.impl;

import com.bubaiwantong.dao.BookDao;
import com.bubaiwantong.domain.Book;
import com.bubaiwantong.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Override
    public boolean save(Book book) {
        return bookDao.insert(book) > 0;
    }
}

BookServiceTest测试类

package com.bubaiwantong;

import com.bubaiwantong.domain.Book;
import com.bubaiwantong.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest
@Transactional
@Rollback(true)
public class BookServiceTest {

    @Autowired
    private BookService bookService;

    @Test
    void test(){
        Book book = new Book();
        book.setName("springboot");
        book.setType("springboot");
        book.setDescription("springboot");

        bookService.save(book);

    }

}

可以看到,测试之后不会对数据库产生新数据,这样我在打包部署项目的时候,就可以对项目进行直接测试了。

image-20220412203143859

四、注入随机数据进行测试

我们使用springboot进行单元测的时候,总不能一个一个的创造数据吧,我们可以将使用随机函数创建随机值,注入到bean中,然后使用bean进行测试。

application.yml文件

testcase:
  book:
    id: ${random.int}
    name: 黑马${random.value}
    type: ${random.long}
    description: ${random.uuid}

BookCase类

package com.bubaiwantong.test.domian;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


@Component
@Data
@ConfigurationProperties(prefix = "testcase.book")
public class BookCase {
    private int id;
    private String name;
    private String type;
    private String description;
}

BookCaseTest测试类

package com.bubaiwantong;

import com.bubaiwantong.test.domian.BookCase;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BookCaseTest {

    @Autowired
    private BookCase bookCase;

    @Test
    void test(){
        System.out.println(bookCase);
    }


测试结果:

image-20220412205948645

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JavaGPT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值