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配置类
@Configurationpublic 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: 80spring: datasource: druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/ssmp_db?serverTimezone=GMT username: root password: 123456mybatis-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;@Datapublic 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的泛型 */@Mapperpublic interface BookDao extends BaseMapper {}
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;@Servicepublic 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); }}
可以看到,测试之后不会对数据库产生新数据,这样我在打包部署项目的时候,就可以对项目进行直接测试了。
四、注入随机数据进行测试
我们使用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;@SpringBootTestpublic class BookCaseTest { @Autowired private BookCase bookCase; @Test void test(){ System.out.println(bookCase); }
测试结果: