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
    }
}

发布了96 篇原创文章 · 获赞 42 · 访问量 25万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览