SpringBoot中使用JNnit4(一)之Mockito的使用

经过入门篇,可以编写出一个简单的测试用例。

这篇讲的是BDDMockito的使用。

BDDMockito用于测试时进行打桩处理;通过它可以指定某个类的某个方法在什么情况下返回什么样的值。

在单元测试时,如果遇到复杂的业务场景,使用多个类时,就需要用到BDDMockito。

需要打桩的对象,只能是@Mock注解的方式

package xx.xx.test;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTests {

    @Before
    public void init(){
        System.out.println("******测试开始");
    }
    
    @After
    public void end(){
        System.out.println("******测试结束");
    }
    
    @BeforeClass
    public static void initClass(){
        System.out.println("******测试开始初始化");
    }
    
    @AfterClass
    public static void endClass(){
        System.out.println("******测试结束初始化");
    }
}
package xx.xx.test;

import org.mockito.BDDMockito;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import xx.xx.test.InfoService;
import xx.xx.test.UserService;

public class Test extends ApplicationTests{
    
    @Mock
    private UserService userService;
    @Autowired
    private InfoService infoService;

    @org.junit.Test
    public void add(){
        int i = userService.add("王五");
        System.out.println("Test.add.i:" + i);
        int j = infoService.update("张三");
        System.out.println("Test.add.j:" + j);
    }
    
    @Override
    public void init(){
        BDDMockito.given(userService.add("王五")).willReturn(9);
        BDDMockito.given(userService.add("")).willReturn(0);
        BDDMockito.given(userService.add(null)).willThrow(NullPointerException.class);
        System.out.println("******重写测试开始");
    }
}
package xx.xx.test;

public interface InfoService {

    Integer update(String name);
    
}
package xx.xx.test.impl;

import org.springframework.stereotype.Service;

import xx.xx.test.InfoService;

@Service("infoService")
public class InfoServiceImpl implements InfoService{
    
    public Integer update(String name){
        return 1;
    }

}
package xx.xx.test;

public interface UserService {

    Integer add(String name);
    
}

打印结果:

******测试开始

******重写测试开始
Test.add.i:9
Test.add.j:1
******测试结束
******测试结束初始化

 

BDDMockito有哪些使用场景呢?

1、可以指定打桩对象的返回值。

  1.1、使用given:

BDDMockito.given(userService.add("王五")).willReturn(9);
BDDMockito.given(userService.add("")).willReturn(0);    
BDDMockito.given(userService.add(null)).willThrow(NullPointerException.class);

    上面代码的意思是:

      当传入的参数是"王五"的时候,返回值是9。

      当传入的参数是""时,返回的值是0。

      当传入的参数是null时,抛出NullPointerException异常。

 

转载于:https://www.cnblogs.com/guochang/p/10431265.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值