spring boot项目从零开始-(5)测试篇-集成Mockito

spring boot项目从零开始-测试篇-Mockito

简述

目录

在这里插入图片描述

步骤

pom.xml修改

<!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

MyMockController.java

package com.ydfind.start.controller.test;

import com.ydfind.start.service.test.MyMockService;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * mock测试controller类
 * @author ydfind
 * @date 2020.1.17
 */
@RestController
@RequestMapping("/mock")
public class MyMockController {

    @Autowired
    MyMockService myMockService;


    @Autowired
    MyMockService myMockService1;

    @GetMapping("/name")
    @ApiImplicitParam(value = "获取名称")
    public String getName(String id) {
        return myMockService.getName(id);
    }

    @GetMapping("/name1")
    @ApiImplicitParam(value = "获取名称")
    public String getName1(String id) {
        return myMockService1.getName(id);
    }
}

service

package com.ydfind.start.service.test;

public interface MyMockService {
    String getName(String id);
}
package com.ydfind.start.service.test.impl;

import com.ydfind.start.service.test.MyMockService;
import org.springframework.stereotype.Service;

@Service
public class MyMockServiceImpl implements MyMockService {

    @Override
    public String getName(String id) {
        System.out.println(id + ": [" + id + "]");
        return id + ": [" + id + "]";
    }

}

test

package com.ydfind.start;

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = MainApplication.class)
@RunWith(SpringRunner.class)
public abstract class BaseTest {
}
package com.ydfind.start.controller.test;

import com.ydfind.start.BaseTest;
import com.ydfind.start.service.test.MyMockService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

public class MyMockControllerTest extends BaseTest {

    @Autowired
    MyMockController myMockController;

    @MockBean
    MyMockService myMockService;

    @Before
    public void init() {
        Mockito.doReturn("default_name").when(myMockService).getName(Mockito.anyString());
    }

    @Test
    public void testGetName() {
        Assert.assertEquals(myMockController.getName("1"), "default_name");

    }

    @Test
    public void testGetName1() {
        Assert.assertEquals(myMockController.getName1("1"), "default_name");
    }
}

测试

因为对service进行了mock,所有service里面的代码是不会执行的。测试用例都会通过

备注

更多mockito请看:https://blog.csdn.net/sndayYU/article/details/103887552
测试相关请看:https://blog.csdn.net/sndayYU/article/details/103863847

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值