java - Spring新手: how to override properties for Test application

标签 java spring soap

假设我有以下 SoapApplication 启动器:

  @SpringBootApplication
    public class Application {

     public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
     }
    }

application.properties 中的一些属性在哪里

在测试中我有:

public abstract class SoapTest {
    protected static ConfigurableApplicationContext server;
    protected static HttpClient client;

    @BeforeAll
    public static void setUp() {
        server = SpringApplication.run(Application.class,"--a=1","--b=2");
        server.start();

    }

    @AfterAll
    public static void tearDown() {

        server.stop();
    }

    }

所以我对“--a=1”、“--b=2”不满意

我更喜欢设置 test.properties

我尝试过做这样的事情:

   @Configuration
    @EnableAutoConfiguration
    @PropertySource("file:testdata/test.properties")
    public class TestConfig {

     }

和 SpringApplication.run(TestConfig.class, args);

但它仍然通过 application.properties 启动。

怎样才能做得好???

我想我不能使用来自 Override default Spring-Boot application.properties settings in Junit Test 的建议 虽然我使用的不是 Junit5(?)。

这样做了:

System.setProperty("spring.config.location", "文件:testdata/test.properties"); 服务器 = SpringApplication.run(Application.class);

正确吗?它对我有用,但可能在最佳实践中并没有多大用处?

最佳答案

如果您选择使用 Spring,我认为您应该考虑使用 Spring WebServices,您可以通过它对像这样的 WebServices 提供完整的测试支持

import static org...ws.test.server.RequestCreators.*;
import static org...ws.test.server.ResponseMatchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-ws-servlet.xml")
public class ServerTest {

  @Autowired ApplicationContext ac;

  MockWebServiceClient mockClient;

  @Before 
  public void setUp() {
    mockClient = MockWebServiceClient.createClient(ac);
  }

  @Test
  public void test() {

    //given
    Source request = new StringSource("<MyRequest>...");

    //when, then
    Source response = new StringSource("<MyResponse>...");
    mockClient.sendRequest(withPayload(request)).andExpect(payload(response));

  }

}

关于java - Spring新手: how to override properties for Test application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40772726/

相关文章:

java - 如何将字符串 yyyy-MM-ssThh-mm-ss 转换为 LocalDataTime yyyy-MM-ss hh-mm-ss?

spring - 如何在 Spring Data 中使用 OrderBy 和 findAll

java - Spring-Roo 删除 Mysql,尽管 hibernate.hbm2ddl.auto

java - 使用 RServe 从 Java 调用 R 奇怪的错误

java - google-http-java-client json 更新现有对象

Spring Boot 响应已提交资源请求异常

node.js - 如何通过node js消费wsdl webservice

java - 如何在 wsimport 中映射未知的复杂类型

php - PHP SoapClient 的复杂 header

java - 如何在 Avro 中处理具有循环引用的对象