Mock简单使用

Mock简单使用

原因:

如果要测试一个功能是否完善,但是某个方法又依赖别的接口,别的接口可能没有开发完,此时如果使用正常的测试的话,那么没有开发完的接口可能会阻塞自测的流程

使用mock的优点

  1. 与写死其他接口返回结果不同,Mock不需要修改原方法的代码

不说废话,直接开始

public interface CarService3 {
	String addPetroleum();
}
package cn.withmes.spring.boot.mockito;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public class CarServiceImpl3 implements  CarService3{

    @Autowired
    private PertoleumService pertoleumService;

    @Override
    public String addPetroleum(){
       return "此次一共加油"+pertoleumService.addMl()+"ml";
    };
}

public interface PertoleumService {
    String addMl();
}

package cn.withmes.spring.boot.mockito;

import org.springframework.stereotype.Service;

@Service
public class PertoleumServiceImpl implements  PertoleumService {
    @Override
    public String addMl() {
        throw  new RuntimeException("其他结果抛出异常!");
    }
}

正常测试,结果抛出异常

package cn.withmes.spring.boot.mockito;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import static org.mockito.Mockito.when;

@SpringBootTest
@RunWith(SpringRunner.class)
public  class SpringBootMockitoApplicationTests2 {

    @Autowired
    private PertoleumService pertoleumService;

    @Autowired
    private  CarService3 carService3;

    @Test
    public void mock1 () {
        String s = carService3.addPetroleum();
        System.out.println(s);
        //java.lang.RuntimeException: 其他结果抛出异常!
    }
}

使用mock,这样达到了不修改代码的情况下。对功能进行测试

package cn.withmes.spring.boot.mockito;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import static org.mockito.Mockito.when;

@SpringBootTest
@RunWith(SpringRunner.class)
public  class SpringBootMockitoApplicationTests2 {

    @Mock
    @Autowired
    private PertoleumService pertoleumService;

    @InjectMocks
    @Autowired
    private  CarServiceImpl3 carService3;  //注意这里使用的是Impl

    @Before
    public void before () {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void mock2 () {
        when(pertoleumService.addMl()).thenReturn("111");
        String s = carService3.addPetroleum();
        System.out.println(s);
        //此次一共加油111ml
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值