簡體   English   中英

Spring Boot 2:IntegrationTest不應該執行CommandLineRunner impl

[英]Spring Boot 2: IntegrationTest should not execute CommandLineRunner impl

我不明白:在集成測試期間執行了Application代碼。

這是我的Application類:

@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)
}
}

SurveyService僅使用一些REST API。 我的測試看起來像這樣:

@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);
    // ...
}
}

如果執行測試,我總是會看到Hello world (請參閱Application類)。 為什么執行Application代碼? 當我遠程調用SpringApplication.run時,它也執行了。

在生產模式下,我的App應該啟動,執行一些REST調用,然后終止。 因此,我將所有執行都放在了Application類中。 但是,這種執行不應在測試用例中調用。 我該如何實現?

謝謝 :)

添加到SurveyRestServiceTest

@SpringBootTest(classes = Application.class)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM