From 17f60d5aa3e61f0e2ce127bf883181ffa50f08a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=A9=AC=E5=93=A5=EF=BC=88mercyblitz=EF=BC=89?= Date: Sat, 1 Jun 2024 13:44:35 +0800 Subject: [PATCH] Update I18nEndpointTest.java --- .../spring/boot/actuate/I18nEndpointTest.java | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java index 7d8c7ba..506b91a 100644 --- a/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java +++ b/microsphere-i18n-spring-boot/src/test/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpointTest.java @@ -2,12 +2,15 @@ import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.DocumentContext; -import io.microsphere.i18n.spring.annotation.EnableI18n; +import io.microsphere.i18n.spring.boot.actuate.autoconfigure.I18nEndpointAutoConfiguration; +import io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import java.util.Locale; @@ -18,37 +21,42 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +@SpringBootTest( + classes = I18nEndpointTest.EndpointConfiguration.class, + properties = { + "management.endpoint.i18n.enabled=true", + "management.endpoints.web.exposure.include=*" + } +) class I18nEndpointTest { - ApplicationContextRunner applicationContextRunner; @BeforeAll static void init() { Locale.setDefault(Locale.CHINA); } - @BeforeEach - void setup() { - applicationContextRunner = new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(EndpointConfiguration.class)); - } + @Autowired + private ApplicationContext context; @Test void shouldReturnCommonI18nMessages() { - applicationContextRunner.run(context -> { - Map> invoke = context.getBean(I18nEndpoint.class).invoke(); - DocumentContext parse = using(Configuration.defaultConfiguration()).parse(invoke); - assertThat(parse, hasJsonPath("$.['common.i18n_messages_en.properties']")); - assertThat(parse, hasJsonPath("$.['common.i18n_messages_zh.properties']")); - assertThat(parse, hasJsonPath("$.['common.i18n_messages_zh_CN.properties']")); - assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_en.properties']")); - assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_en.properties'].['common.a']", equalTo("a"))); - assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh.properties']")); - assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh_CN.properties']")); - assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh_CN.properties'].['common.a']", equalTo("啊"))); - }); + Map> invoke = context.getBean(I18nEndpoint.class).invoke(); + DocumentContext parse = using(Configuration.defaultConfiguration()).parse(invoke); + assertThat(parse, hasJsonPath("$.['common.i18n_messages_en.properties']")); + assertThat(parse, hasJsonPath("$.['common.i18n_messages_zh.properties']")); + assertThat(parse, hasJsonPath("$.['common.i18n_messages_zh_CN.properties']")); + assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_en.properties']")); + assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_en.properties'].['common.a']", equalTo("a"))); + assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh.properties']")); + assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh_CN.properties']")); + assertThat(parse, hasJsonPath("$.['META-INF/i18n/common/i18n_messages_zh_CN.properties'].['common.a']", equalTo("啊"))); } - @EnableI18n + @ImportAutoConfiguration(classes = { + I18nAutoConfiguration.class, + I18nEndpointAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class + }) static class EndpointConfiguration { @Bean I18nEndpoint i18nEndpoint() {