How to test my web service made under the spring boot using a specific profile?

0

I'm developing a RESTFul web service using Spring Boot and would like to know how to run unit tests with requests directed to web services without having to manually lift the server first. I would like the tests to raise the server automatically. In addition, I would like the application to use a specific profile called test with the settings in application-test.properties that contains the address of a local bank and a specific port. Is this possible?

    
asked by anonymous 28.03.2018 / 19:46

1 answer

0

Yes it is. First, let's think about getting your services up automatically. For this, we will use two annotations, one informing the Spring "executor", which is @RunWith(SpringRunner.class) , and another informing that our port is defined by the non-random configuration file, besides informing our class with the main method that publishes the web service, which is @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = RestfulApplication.class) . So assuming we're going to test the resource person of your RESTFul web service:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
        classes = RestfulApplication.class)
public class PessoaResourceTest {

//...

}

But that does not solve everything. The settings that will be used to launch the service will default to the application.properties file and the test profile is desired. For this we will use the tag @ActiveProfiles("test") :

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
            classes = RestfulApplication.class)
    @ActiveProfiles("test")
    public class PessoaResourceTest {

    //...

    }

Now the service will automatically launch using the test profile before the tests are run.

    
28.03.2018 / 19:46