Newbe to Spring:如何覆盖Test应用程序的属性

时间:2016-11-23 19:30:20

标签: 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"," file:testdata / test.properties");         server = SpringApplication.run(Application.class);

这是对的吗?它适用于我,但可能在最佳实践中并不多?

1 个答案:

答案 0 :(得分:1)

如果您选择使用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));

  }

}