首页 > 其他分享 >单元测试

单元测试

时间:2023-04-07 15:11:27浏览次数:32  
标签:spring 单元测试 class sql test public mock

一。使用Mockito

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>3.8.0</version>
    <scope>test</scope>
</dependency>
@RunWith(MockitoJUnitRunner.class)
public class HelloTest {

    @Mock
    private StuDao stuDao;

    //InjectMocks 注解会把上面的mock的stuDao对象注入到stuService中,这样我们就能测试insertStu方法了
    @InjectMocks
    private StuService stuService;

    @Test
    public void testInsertStu(){
        String name = "jack";
        Stu stu = new Stu();
        stu.setName(name);
        //mock 调stuDao.insertUser(stu)的返回
        when(stuDao.insertUser(stu)).thenReturn(1);
        stuService.insertStu(name);
    }

}

这里再介绍一个注解@Spy

相同点
都可以完成一个对象的mock,另外spy和mock生成的对象不受spring管理

不同点
1.默认行为不同

对于未指定mock的方法,spy默认会调用真实的方法,有返回值的返回真实的返回值,而mock默认不执行,有返回值的,默认返回null

也就是说如果没有mock或者

Mockito.when(adminService.insertUser("jack")).thenReturn(1);这样的情况下,被mock的方法会真实执行,像个间谍一样。

但下面的mock就不会执行真正的方法了。

Mockito.doReturn(1).when(adminService).insertUser("jack");

二。启动spring容器,注入对象进行测试

这种方式依赖启动spring容器,启动过程比较长,依赖的对象可以使用spring注解注入

@RunWith(SpringRunner.class)//代表运行在什么测试容器下
@SpringBootTest
public class SpringContextTest {

    @Resource
    private StuService stuService;

    @Test
    public void testInsertStu(){
        stuService.insertStu("jack");
    }
}

三、DAO层单测

   这层的单元测试,主要是测试sql,如果我们使用我们真实的测试数据库,会破坏我们的测试数据,这种单元测试我们可以利用内存数据库H2进行单元测试

引入相关jar包

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

test->resource 下新建配置文件

logging.level.com.ymm.crm=debug
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=MySQL
spring.datasource.driver-class-name=org.h2.Driver

#初始化表sql
spring.datasource.schema=classpath:init_table.sql
#单元测试数据
spring.datasource.data=classpath:init_data.sql
spring.datasource.sql-script-encoding=UTF-8

#mybatis配置
mybatis.mapper-locations=classpath:mapper/*.xml

test包下分别新建init_table.sql和init_data.sql,分别是新建表和初始化数据用的

init_table.sql

 

CREATE TABLE `user` (
  `id` varchar(32) NOT NULL,
  `version` int(11) NOT NULL DEFAULT 1,
  UNIQUE KEY `uk_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

init_data.sql

insert into user(1,1)

 

test->java 新建测试类,指明dao的路径

@SpringBootApplication(scanBasePackages = "com.test.mapper")
@MapperScan({"com.test.mapper"})
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

 

测试类

public class UserMapperTest extends BaseMapperTest {

    @Resource
    private UserMapper userMapper;


    @Test
    public void testQueryByCustomerIdAndCurrency(){
        userMapper.insert("86");
    }
}

 

标签:spring,单元测试,class,sql,test,public,mock
From: https://www.cnblogs.com/jack1990/p/17124043.html

相关文章

  • 单元测试案例
    在日常的springboot项目开发中,总会需要写一些单元测试用例,一些单元测试的方法用的比较少,编写时又需要去查询,因此在此总结一些测试案例Junit是目前主流的单元测试框架,我,常用的为Junit4,以下测试案例是基于Junit4来编写代码地址:https://github.com/x104859/test-case单元测试的目......
  • Python——单元测试的实现
    摘要单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。在软件开中的测试是很重要的一部分。python测试相关库unittest,内置库,模仿PyUnit写的,简洁易用,缺点是比较繁琐。nose,测试发现,发现并运行测试。pytest,笔者目前喜欢用这个,写起来很方便,并且很多知名开源项......
  • Java SpringBoot Test 单元测试中包括多线程时,没跑完就结束了
    如何阻止JavaSpringBootTest单元测试中包括多线程时,没跑完就结束了使用CountDownLatchCountDownLatch、CyclicBarrier使用区别多线程ThreadPoolTaskExecutor应用JavaBasePooledObjectFactory对象池化技术@SpringBootTestpublicclassPoolTest{@Test......
  • ChatGPT如何帮助编写代码文档和单元测试
    ChatGPT如何帮助编写代码文档和单元测试    有多少次你专注于编程而忘记了写函数、方法、类的非常简单的代码文档?我不是在问单元测试.直到我发现ChatGPT可以做到这一点:除了代码文档,它在编写单元测试方面也做得很好。此外,在最后,我可以要求他为其他想使用我的代码的贡献者生......
  • junit单元测试报错:java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
    今天在复习的时候对对一些知识点进行巩固,用到了junit-4.12.jar,手动导入jar包,然后运行然后报错:java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing。刚开始我以为代码错了,看了看发现不是代码的问题,是导包的问题。然后查询了百度,发现了是版本的问题:然后说换个低版本的就......
  • 单元测试
    单元测试Android开发中如何进行单元测试和UI测试?在Android开发中,单元测试和UI测试是非常重要的,可以保证代码的质量和稳定性。以下是Android开发中常用的测试框架......
  • Go06-文件操作+单元测试+goroutine+channel+反射
    Go06-文件操作+单元测试+goroutine+channel+反射1.打开和关闭文件funcmain(){ //1打开文件。 //file可以称为file对象、file指针、file文件句柄。 file,err:=......
  • 程序员为什么不写单元测试?
    一、为了单元测试而写单元测试   最近笔者曾经做过一次“程序员在项目开发中编写单元测试的情况”的调查。    调查结果显示:1.几乎没有严格在项目中执行TDD(,TD......
  • 云原生引擎单元测试实践
    作者:京东零售王雷单元测试概念单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。单元测试是一种白盒测试技术,一般都是由开发人员在编码阶段......
  • 【单元测试】Junit 4(九)--junit4 测试private方法
    直接上代码Calculator.javapublicclassCalculator{ privateStringname; privateintage; publicCalculator(Stringname,intage){ this.name=name; ......