微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 spring-boot:run 期间启动 MockServer

如何解决在 spring-boot:run 期间启动 MockServer

我们在应用程序中使用了一些 API,由于防火墙的原因,这些 API 无法从本地开发者机器访问。

我想使用 mockServer 来模拟其中一些 API,以便我们可以在本地进行开发。

运行测试时,可以分别使用 maven 构建阶段 process-test-classesverify 启动和停止 mockServer。

当我使用 mvn spring-boot:run 启动应用程序时,如何让它运行?

解决方法

可以在 springboot 上覆盖 bean。 所以你可以使用你的 bean 并根据需要切换模拟值 下面的示例覆盖服务并根据您的喜好使用模拟,但您也可以使用接口。

创建服务

@Service
public class ServiceReal {

    @Autowired(required = false) // must be required=false. May be disabled by using mock configuration
    private JdbcTemplate jdbcTemplate;

    public String getInfo() {
        return  jdbcTemplate...// get a real value from database
    }

}

创建模拟服务


@Service
@Primary
@Profile("mocklocal")
public class ServiceMock extend ServiceReal {

    @Override
    public String getInfo() {
        return  "Mocked value"
    }
}

配置 bean 以便稍后在属性上选择其中之一

@Profile("mocklocal")
@PropertySource("classpath:application-mocklocal.properties")
@Configuration
public class ConfigMock {

    private static final String  PROP_VALUE_TRUE = "true";
    private static final boolean PROP_FALSE_DEFAULT_MISSING = false;
    private static final String  PROP_SERVICE_REAL = "mocklocal.service.real";
    private static final String  PROP_SERVICE2_REAL = "mocklocal.service2.real";
    
    @Bean
    @ConditionalOnProperty( value = PROP_SERVICE_REAL,havingValue = PROP_VALUE_TRUE,matchIfMissing = PROP_FALSE_DEFAULT_MISSING)
    public ServiceReal serviceReal(){
        return new ServiceMock();
    }
    
    @Bean
    @ConditionalOnProperty( value = PROP_SERVICE2_REAL,matchIfMissing = PROP_FALSE_DEFAULT_MISSING)
    public Service2Real service2Real(){
        return new Service2Mock();
    }   
}

配置您的 application-mocklocal.properties 以使用模拟

# using ConfigMock
spring.profiles.active=mocklocal

# settig spring to override service and use mock
spring.main.allow-bean-definition-overriding=true

# disable some configuration not required in mocks. you can adjust for amqp,database or other configuration
spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
spring.autoconfigure.exclude[2]=org.springframework.boot.autoconfigure.orm.jpa.DataSourceTransactionManagerAutoConfiguration

# enable your service to use mocks not real services
mocklocal.service.real=true
mocklocal.service2.real=true

因此,如果您使用 --spring.profiles.active=mocklocal 启动您的应用程序,您将获得模拟值

你也可以在测试中使用

@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:application-mocklocal.properties")
public class RunIntegrationTests {

    @Autowired
    private MockMvc mockMvc;
    
    @Test
    public void run() throws Exception{
        ...
    }
}
,

我曾经为我的团队创建了一个 MockServer,用于非常相似的目的 here(幸运的是一个简短的 demo 也可用)。您可以独立设置此服务器(例如在本地主机上),并将请求(url 和有效负载)与您想要的相应响应 json 添加到此服务器。

您需要在项目内部进行的一次更改是在开发/测试期间将所有 API 请求路由到此 Mockserver,这可以通过更改您将使用和设置的所有 API 的基本 URL 来完成具有适当 json 请求和响应的模拟服务器。它可以像这样简单地完成:

public class BaseUrlLoader {

    public static String NEWSRIVER_BASE_URL;
    public static String FACEBOOK_BASE_URL;
    public static String TWITTER_BASE_URL;

    private static final String MOCKSERVER_BASE_URL = "mocksrvr.herokuapp.com/TEAM-SECRET-KEY";

    public static void load(){
        Properties properties= new Properties();
        String activeProfile;
        try{
            properties.load(ClassLoader.getSystemResourceAsStream("application.properties"));
        } catch (IOException e) {
            System.out.println("Not able to load the application.properties file");
            return;
        }
        activeProfile = properties.getProperty("spring.profiles.active");
        System.out.println("Using "+activeProfile);
        if(activeProfile.equals("Test")){
            NEWSRIVER_BASE_URL=MOCKSERVER_BASE_URL;
            FACEBOOK_BASE_URL= MOCKSERVER_BASE_URL;
            TWITTER_BASE_URL= MOCKSERVER_BASE_URL;
        }else{
            NEWSRIVER_BASE_URL="api.newsriver.io";
            FACEBOOK_BASE_URL="api.facebook.com";
            TWITTER_BASE_URL="api.twitter.com";
        }
        System.out.println(NEWSRIVER_BASE_URL);
    }

}


// Example- Use APIs as
public class NewsFetch {
    
    ...
    
    public NewsFetch(){ BaseUrlLoader.load(); }
    
    private URI buildURL(APIQuery apiQuery) throws URISyntaxException {
        String mainURL = BaseUrlLoader.NEWSRIVER_BASE_URL+"v2/search";
        URIBuilder url = new URIBuilder(mainURL);
        url.addParameter("query",apiQuery.getLuceneQuery());
        url.addParameter("soryBy",apiQuery.getSortBy());
        url.addParameter("sortOrder",apiQuery.getSortOrder());
        url.addParameter("limit",apiQuery.getLimit());
        return url.build();
    }

    public HttpResponse <String> fetch(APIQuery apiQuery) throws URISyntaxException,IOException,InterruptedException {
        URI uri = buildURL(apiQuery);
        HttpRequest request = HttpRequest.newBuilder()
                .GET()
                .header("Authorization",KEY)
                .uri(uri)
                .build();
        ...

    }
}
// and add the request like http://mocksrvr.herokuapp.com/TEAM-SECRET-KEY/v2/search/... to the Mockserver with the response you want.

baseurl 将根据当前的活动配置文件更改。这个模拟服务器很简单,甚至可以与 Slackbot 集成。在自述文件中查看更多信息。项目中可能存在许多错误,我们将不胜感激。

,

运行测试时,可以使用 maven 构建阶段 process-test-classes 和 verify 分别启动和停止 mockServer。

所以必须有一些(pom)配置,如:

<plugin>
  <groupId>org.mock-server</groupId>
  <artifactId>mockserver-maven-plugin</artifactId>
  <version>3.10.8</version>
  <configuration>
    <serverPort>1080</serverPort>
    <proxyPort>1090</proxyPort>
    <logLevel>DEBUG</logLevel>
    <initializationClass>org.mockserver.maven.ExampleInitializationClass</initializationClass>
  </configuration>
  <executions>
      <execution>
        <id>process-test-classes</id>
        <phase>process-test-classes</phase>
        <goals>
            <goal>start</goal>
        </goals>
      </execution>
      <execution>
        <id>verify</id>
        <phase>verify</phase>
        <goals>
            <goal>stop</goal>
        </goals>
      </execution>
  </executions>
</plugin>

这将在 process-test-classes(因此在 test 阶段之前)和 stopvalidate(因此在(post-)之后)启动一个模拟服务器{{ 1}} 阶段)。 (link1,link2)

当我使用 mvn spring-boot:run 启动应用程序时如何让它运行?

使用 integration-test 运行它:

  1. 只需运行mvn spring-boot:run! (将其打包到脚本/IDE 启动中..)(推荐
  2. 实现自定义插件,它结合了 spring-boot-maven 和 mockserver-maven-plugin...(然后运行 ​​mvn mockserver:start spring-boot:run

.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?