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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章