亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

彈簧啟動如何測試 Web 服務器初始化事件

彈簧啟動如何測試 Web 服務器初始化事件

慕桂英4014372 2022-09-14 17:38:29
我正在嘗試測試應用程序向服務注冊表注冊時發生的應用程序中的功能。僅當應用具有完整的 Web 上下文(即。 位于類路徑上,并且 servlet 不會被嘲笑)。這是通過抽象自動服務注冊控制的。spring-boot-starter-webspring-cloud-commons簡單測試所有測試應該做的是以下幾點:1) Bring up Web App2) Verify auto-registration w/ service registry event fired3) Manually force close app4) Verify auto-deregistratoin occurred方法 1:@SpringBootTestSpringBootTest使創建完整的Web上下文變得容易,這很棒。但我無法在測試中關閉應用以強制取消注冊@RunWith(SpringRunner.class)@SpringBootTest(        classes = MyAutoConfig.class,        webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)@EnableAutoConfigurationpublic class DiscoverySpringCloudBootMinimalRegistrationTest {@Testpublic void register_deregister {    // Force-close app to trigger dereigster (causes exception)    ((ConfigurableApplicationContext) context).close();    verify(registry, times(1)).register(autoRegistrationServiceRecord);    verify(registry, times(1)).deregister(autoRegistrationServiceRecord);}調用會導致一個長錯誤,基本上說不要像這樣手動關閉上下文。context.close()..... contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]]] is not active. This may be due to one of the following reasons: 1) the context was closed programmatically by user code; 2) the context was closed during parallel test execution either according to @DirtiesContext semantics or due to automatic eviction from the ContextCache due to a maximum cache size policy.方法 2:Web上下文運行器在這種方法中,我避免并手動配置上下文運行器。這非常適合調用,但配置中的 Web 上下文具有模擬 servlet,并且不會觸發自動注冊所需的內容。@SpringBootTestcontext.close()WebInitializedEventpublic class BasicAutoConfigTests {    private WebApplicationContextRunner runner;    @Test    public void register_deregister() {       runner = new WebApplicationContextRunner()              .withConfiguration(                    AutoConfigurations.of(MyAutoConfig.class));      });  }這幾乎有效,但會導致豆子,我推測它未能觸發所需的。這種方法如何引導真實、完整的嵌入式 tomcat 服務器?MockServletContextWebServerInitializedEventspring-cloud-commons
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

根據Spencer的建議,我使用彈簧應用程序構建器創建了一個完整的Web應用程序。我也在自動配置模塊之外執行此操作 - 在類路徑上創建了一個名為“集成測試”的新 maven 子模塊。spring-boot-starter-web


@Import(MyAutoConfig.class)

@SpringBootApplication

public class MinStarterBasicApp {


@Bean

ServiceRegistry serviceRegistry() {

    return mock(ServiceRegistry.class);

    }


    static ConfigurableApplicationContext setupWebApp(String... profiles){

        System.setProperty("spring.main.allow-bean-definition-overriding", "true");


        SpringApplication app = new SpringApplicationBuilder(MinStarterBasicApp.class)

                .web(WebApplicationType.SERVLET)

                .profiles(profiles)

                .build();

        return app.run();

    }

}

其中允許我按名稱傳入應用程序.properties文件,如下所示。此外,請務必確保為每個測試手動提供應用上下文。profilesclose


public class StarterBasicAutoconfigTest {


    ConfigurableApplicationContext context;


    @After

    public void teardown() {

        if (context != null && context.isRunning())

            context.close();

    }



    @Test

    public void sometest() {

        context = MinStarterBasicApp.setupWebApp("profile1");


        ServiceRegistry registry = context.getBean(ServiceRegistry.class);

        context.close();


        Mockito.verify(registry, times(1)).register(any());

        Mockito.verify(registry, times(1)).deregister(any());

}


查看完整回答
反對 回復 2022-09-14
  • 1 回答
  • 0 關注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號