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

Asked

Viewed 498 times

0

I am developing a Restful web service using Spring Boot and I would like to know how to run unit tests with requests directed to web services without first having to raise the server manually. I would like the tests to automatically open the server. Also, I would like the application to use a specific profile called test with settings in application-test.properties which contains the address of a local bank and a specific port. This is possible?

1 answer

0


It is. First, let’s think about automatically raising your services. For this, we will use two Annotations, one informing the "executor" of Spring, which is @RunWith(SpringRunner.class), and another stating that our port is defined by the configuration file and not random, 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, supposing we test the Resource person of your web service Restful:

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

//...

}

But this does not fix everything. The settings that will be used to launch the service will be the default in the file application.properties and the desired is the profile test. 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 be launched automatically using the profile test before the tests are carried out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.