• MockBean 单元测试


    案例一   官方文档:https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/


    import org.junit.*;

    import org.junit.runner.*;
    import org.springframework.beans.factory.annotation.*;
    import org.springframework.boot.test.autoconfigure.web.servlet.*;
    import org.springframework.boot.test.mock.mockito.*;
    
    import static org.assertj.core.api.Assertions.*;
    import static org.mockito.BDDMockito.*;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
    
    @RunWith(SpringRunner.class)
    /**使用webMvcTest工具测试,UserVehicleControkker.class代表对
    UserVehicleControkker 进行测试*/
    @WebMvcTest(UserVehicleController.class)

    public class MyControllerTests { @Autowired private MockMvc mvc; @MockBean private UserVehicleService userVehicleService; @Test public void testExample() throws Exception { given(this.userVehicleService.getVehicleDetails("sboot")) .willReturn(new VehicleDetails("Honda", "Civic")); this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN)) .andExpect(status().isOk()).andExpect(content().string("Honda Civic")); } }

    案例二:jsons数据的传递,即传一个对象

    官方文档的推荐:
    import org.junit.*;
    import org.junit.runner.*;
    import org.springframework.beans.factory.annotation.*;
    import org.springframework.boot.test.autoconfigure.json.*;
    import org.springframework.boot.test.context.*;
    import org.springframework.boot.test.json.*;
    import org.springframework.test.context.junit4.*;
    
    import static org.assertj.core.api.Assertions.*;
    
    @RunWith(SpringRunner.class)
    @JsonTest
    public class MyJsonTests {
    
    	@Autowired
    	private JacksonTester<VehicleDetails> json;
    
    /**方式一/ @Test public void testSerialize() throws Exception { VehicleDetails details = new VehicleDetails("Honda", "Civic"); // Assert against a `.json` file in the same package as the test assertThat(this.json.write(details)).isEqualToJson("expected.json"); // Or use JSON path based assertions assertThat(this.json.write(details)).hasJsonPathStringValue("@.make"); assertThat(this.json.write(details)).extractingJsonPathStringValue("@.make") .isEqualTo("Honda"); } /**方式二/ @Test public void testDeserialize() throws Exception {
    /**自己制作一些json数据*/ String content = "{"make":"Ford","model":"Focus"}";
    /**使用预言:第一个:
    assertThat进行数据的json格式的转换,以及传递数据*/
    assertThat(this.json.parse(content)) .isEqualTo(new VehicleDetails("Ford", "Focus"));
    /**使用预言:assertThat:传递数据,并得到自己制作的数据,并且期望返回的数据是否是 “Ford”*/
    assertThat(this.json.parseObject(content).getMake()).isEqualTo("Ford");
    }
    }
    /*个人的json数据:包装成一个对象,传递过去*/
    1):需要的依赖:
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.12</version>
    </dependency>


    2):
    源代码:
    package hsbc.team03.ordersystem.displayproduct;

    import com.alibaba.fastjson.JSONObject;
    import hsbc.team03.ordersystem.displayproduct.common.UUIDUtils;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.http.MediaType;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.web.context.WebApplicationContext;

    import java.util.ArrayList;
    import java.util.List;

    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
    import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

    /**
    * @Author:Evan
    * @Date:2018/8/7 11:01
    * @Describe:
    * @Return:
    * @Param:
    */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ManagerControllerTest {
    @Autowired
    private WebApplicationContext context;
    private MockMvc mvc;

    /**使用mockBean模拟ManagerServiceImpl层 ,注意ManagerServiceImpl 是自己创建的一个类*/
        @MockBean
    private ManagerServiceImpl managerServiceImpl;

    @Before
    public void setUp() {
    mvc = MockMvcBuilders.webAppContextSetup(context).build();
    }
     
     @Test
    public void managerAddProduct() throws Exception {
    Mockito.when(managerServiceImpl.addProduct(Mockito.any(Product.class))).thenReturn(true);

    /**制造数据,即对象*/
    Product product = new Product();
    product.setId(UUIDUtils.getUUID());
    product.setProductCode("231701");
    product.setProductName("中海");
    product.setProductPrice(20.8);
    product.setProductType("稳健型");
    product.setDescription("这是1");
    product.setStatus(1);
    product.setProductDeadline("2018-8-10");

    /**将对象进行转换成json格式*/
    String requestJson = JSONObject.toJSONString(product);

    mvc.perform(
    /**post方法、访问的路径*/
    post("/manager/add/products")
    .contentType(MediaType.APPLICATION_JSON_UTF8) /*数据格式*/
    .content(requestJson) /*内容,即参数*/
    .accept(MediaType.APPLICATION_JSON)) /*接收返回的参数是json格式*/
    .andExpect(status().isOk()) /*status().isOk(),期望返回的状态码是200*/
    .andDo(print()); /*控制台打印*/


    }
    }


  • 相关阅读:
    8.电影推荐
    一.Memcached企业服务
    7.学完linux系统运维到底可以做什么?
    svn+jenkins自动部署
    关于gitlab+jenkins自动部署代码的实现
    Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre
    php实现雪花算法(ID递增)
    php使用rdkafka进行消费
    Burp破解安装(1.7和2.0)
    在已有lnmp环境的基础上安装PHP7
  • 原文地址:https://www.cnblogs.com/qq1141100952com/p/9608730.html
Copyright © 2020-2023  润新知