• 契约测试SpringCloud Contract遇到的问题


    1、java.lang.IllegalStateException: You haven't configured a MockMVC instance. You can do this statically

    在服务提供方,执行gradle test后,契约的validate失败,错误信息如下图:

    It turns out that I need to add the base class that I am missing(like the FraudBase.java) in the sample. That's where the MockMvc is being instantiated.

    https://stackoverflow.com/questions/42405945/spring-cloud-contract-generated-test-doesnt-have-mockmvc-configured-and-fails

    第一步:我加了一个base class如下:

    package com.xxx.xxx.contract;
    import org.junit.Before;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ActiveProfiles;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.web.context.WebApplicationContext;
    
    import com.xxx.xxx.Application;
    
    import io.restassured.module.mockmvc.RestAssuredMockMvc;
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = Application.class)
    //@ActiveProfiles("unit-test")  //当契约测试时想使用不同的配置文件时,取消注释时会使用loan-unit-test.yml配置文件
    public abstract class ConstractTestBase {
        static{
            System.setProperty("aes.xxx", "abc");
        }
    
        @Autowired
        private WebApplicationContext context;
        @Before
        public void setUp() throws Exception {
            RestAssuredMockMvc.webAppContextSetup(context);
        } 
    }

    第二步:在gradle脚本中,指定

    contracts {
        baseClassForTests = 'com.xxx.xxx.contract.ConstractTestBase' 
    }
  • 相关阅读:
    课堂测试-单元测试(比较大小)
    第三周进度条
    软件工程个人作业02
    构建之法——阅读笔记02
    第二周学习进度条
    第一周学习进度条
    软件工程个人作业01
    构建之法阅读笔记01
    java课堂测试
    Java验证码程序
  • 原文地址:https://www.cnblogs.com/duanxz/p/14919810.html
Copyright © 2020-2023  润新知