0

Assume i have following SoapApplication starter:

  @SpringBootApplication
    public class Application {

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

so where is some properties in application.properties

In test I have:

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();
    }

    }

So i'm not glad with "--a=1","--b=2"

I prefer to setup test.properties

I have tryed to make something like this:

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

     }

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

But it still launches with application.properties.

How to do it well???

I think i cannot use suggestons from Override default Spring-Boot application.properties settings in Junit Test while it's not for Junit5 what i'm using (?).

Have done this way:

System.setProperty("spring.config.location", "file:testdata/test.properties"); server = SpringApplication.run(Application.class);

Is it correct? It works for me, but may be it's not much in best practice?

Community
  • 1
  • 1
comdiv
  • 865
  • 7
  • 26

1 Answers1

1

If you are choosing to use Spring I think you should consider to make use of Spring WebServices with which you have full test support for WebServices like this

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));

  }

}
Hubert Ströbitzer
  • 1,745
  • 2
  • 15
  • 19