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.