当前位置:   article > 正文

spring boot项目从零开始-(5)测试篇-集成Mockito_springboot 继承mockito

springboot 继承mockito

简述

目录

在这里插入图片描述

步骤

pom.xml修改

<!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

service

package com.ydfind.start.service.test;

public interface MyMockService {
    String getName(String id);
}
  • 1
  • 2
  • 3
  • 4
  • 5
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 + "]";
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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 {
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
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");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

测试

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

备注

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/366806
推荐阅读
相关标签
  

闽ICP备14008679号