i don't get it: The Application
code is executed during my integration tests.
Here is my Application
class:
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
SurveyService surveyService;
@Override
public void run(String... args) throws Exception {
System.out.println("Hello world");
// some useage of the autowired service (do all the stuff)
}
}
The SurveyService
consume just some REST API.
My test looks something like that:
@ExtendWith(SpringExtension.class)
@RestClientTest({SurveyRestService.class})
@ComponentScan("com.example.app")
@TestPropertySource(properties = "uri=http://testMe.com/")
class SurveyRestServiceTest {
@Autowired
SurveyService classUnderTest;
@Autowired
MockRestServiceServer mockServer;
private void setupMockServerAndRespond(String response) {
mockServer.expect(requestTo("http://testMe.com/surveys")).andRespond(withSuccess(response, APPLICATION_JSON));
}
@Test
void shouldDeserialzeAllFields() {
setupMockServerAndRespond(VALID_JSON_ONE_ENTRY);
List<Survey> surveys = classUnderTest.listSurveys();
assertThat(surveys).hasSize(1);
// ...
}
}
If i execute the test i always see Hello world
(see Application
class). Why is the Application
code executed? It also executed, when I remote the SpringApplication.run
call.
In production mode my App should start, execute some REST calls and than terminate. So I put all the executions in my Application
class. But this executions should not be called in test case. How can I achieve this?
Thanks :)