0

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

user5012221
  • 13
  • 1
  • 4

1 Answers1

0

add to SurveyRestServiceTest :

@SpringBootTest(classes = Application.class)

tsarenkotxt
  • 3,231
  • 4
  • 22
  • 38
  • Thanks for reply. `Hello world` (stdout in the `Application` class) is still visible in the logs :( Therefore my production code would be executed during test. – user5012221 Oct 18 '18 at 04:58