Skip to content

Commit

Permalink
refactor: 测试Config调整
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed Dec 25, 2024
1 parent 498fe1c commit db09632
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import com.livk.context.fastexcel.resolver.ReactiveExcelMethodReturnValueHandler;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver;
Expand All @@ -44,9 +42,9 @@ class FastExcelAutoConfigurationTest {
@Test
void testServlet() {
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(FastExcelAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(WebMvcAutoConfiguration.class, FastExcelAutoConfiguration.class));

contextRunner.withUserConfiguration(WebMvcConfig.class).run(context -> {
contextRunner.run(context -> {
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
assertThat(adapter.getArgumentResolvers()).isNotNull()
.hasAtLeastOneElementOfType(ExcelMethodArgumentResolver.class);
Expand All @@ -59,9 +57,9 @@ void testServlet() {
@SuppressWarnings("unchecked")
void testReactive() {
ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(FastExcelAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class, FastExcelAutoConfiguration.class));

contextRunner.withUserConfiguration(WebFluxConfig.class).run(context -> {
contextRunner.run(context -> {
org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter adapter = context
.getBean(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter.class);

Expand All @@ -76,16 +74,4 @@ void testReactive() {
});
}

@TestConfiguration
@ImportAutoConfiguration(WebMvcAutoConfiguration.class)
static class WebMvcConfig {

}

@TestConfiguration
@ImportAutoConfiguration(WebFluxAutoConfiguration.class)
static class WebFluxConfig {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.jupiter.api.Test;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

Expand All @@ -36,7 +35,8 @@ class SqlDataInjectionAutoConfigurationTest {

final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
.withBean(DataSource.class, HikariDataSource::new)
.withConfiguration(AutoConfigurations.of(SqlDataInjectionAutoConfiguration.class));
.withConfiguration(
AutoConfigurations.of(MybatisAutoConfiguration.class, SqlDataInjectionAutoConfiguration.class));

@Test
void sqlDataInjectionConfigurationCustomizer() {
Expand All @@ -49,7 +49,6 @@ void sqlDataInjectionConfigurationCustomizer() {

@TestConfiguration
@EnableSqlPlugin
@ImportAutoConfiguration(MybatisAutoConfiguration.class)
static class Config {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class MybatisLogMonitorAutoConfigurationTest {

final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
.withBean(DataSource.class, HikariDataSource::new)
.withConfiguration(AutoConfigurations.of(MybatisLogMonitorAutoConfiguration.class));
.withConfiguration(
AutoConfigurations.of(MybatisAutoConfiguration.class, MybatisLogMonitorAutoConfiguration.class));

@Test
void mybatisLogMonitorConfigurationCustomizer() {
Expand All @@ -51,7 +52,6 @@ void mybatisLogMonitorConfigurationCustomizer() {

@TestConfiguration
@EnableSqlMonitor
@ImportAutoConfiguration(MybatisAutoConfiguration.class)
@EnableConfigurationProperties(MybatisLogMonitorProperties.class)
static class Config {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.junit.jupiter.api.Test;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import javax.sql.DataSource;
Expand All @@ -34,9 +32,10 @@
*/
class MybatisTypeHandlerAutoConfigurationTest {

final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(DataSource.class, HikariDataSource::new)
.withConfiguration(AutoConfigurations.of(MybatisTypeHandlerAutoConfiguration.class));
.withConfiguration(
AutoConfigurations.of(MybatisAutoConfiguration.class, MybatisTypeHandlerAutoConfiguration.class));

@Test
void testMysql() {
Expand All @@ -51,7 +50,7 @@ void testMysql() {

@Test
void testPgsql() {
this.contextRunner.withUserConfiguration(Config.class).run((context) -> {
this.contextRunner.run((context) -> {
SqlSessionFactory factory = context.getBean(SqlSessionFactory.class);
Collection<TypeHandler<?>> typeHandlers = factory.getConfiguration()
.getTypeHandlerRegistry()
Expand All @@ -60,10 +59,4 @@ void testPgsql() {
});
}

@TestConfiguration
@ImportAutoConfiguration(MybatisAutoConfiguration.class)
static class Config {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import com.livk.context.qrcode.support.GoogleQRCodeGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
Expand Down Expand Up @@ -50,9 +48,9 @@ void googleQRCodeGenerator() {
void testServlet() {
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withBean(Jackson2ObjectMapperBuilder.class, Jackson2ObjectMapperBuilder::new)
.withConfiguration(AutoConfigurations.of(QRCodeAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(WebMvcAutoConfiguration.class, QRCodeAutoConfiguration.class));

contextRunner.withUserConfiguration(WebMvcConfig.class).run(context -> {
contextRunner.run(context -> {
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
assertThat(adapter.getReturnValueHandlers()).isNotNull()
.hasAtLeastOneElementOfType(QRCodeMethodReturnValueHandler.class);
Expand All @@ -63,23 +61,11 @@ void testServlet() {
void testReactive() {
ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
.withBean(Jackson2ObjectMapperBuilder.class, Jackson2ObjectMapperBuilder::new)
.withConfiguration(AutoConfigurations.of(QRCodeAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class, QRCodeAutoConfiguration.class));

contextRunner.withUserConfiguration(WebFluxConfig.class).run(context -> {
contextRunner.run(context -> {
assertThat(context).hasSingleBean(ReactiveQRCodeMethodReturnValueHandler.class);
});
}

@TestConfiguration
@ImportAutoConfiguration(WebMvcAutoConfiguration.class)
static class WebMvcConfig {

}

@TestConfiguration
@ImportAutoConfiguration(WebFluxAutoConfiguration.class)
static class WebFluxConfig {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testcontainers.properties.TestcontainersPropertySourceAutoConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
Expand Down Expand Up @@ -71,14 +70,14 @@ static void redisProperties(DynamicPropertyRegistry registry) {

@Test
void redissonClient() {
this.contextRunner.withUserConfiguration(Config.class).run((context) -> {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(RedissonClient.class);
});
}

@Test
void redissonConnectionFactory() {
this.contextRunner.withUserConfiguration(Config.class).run((context) -> {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(RedissonConnectionFactory.class);
});
}
Expand All @@ -93,9 +92,4 @@ ConfigProperties properties() {
return binder.bind(ConfigProperties.PREFIX, ConfigProperties.class).get();
}

@TestConfiguration(proxyBeanMethods = false)
static class Config {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
import nl.basjes.parse.useragent.UserAgentAnalyzer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
Expand Down Expand Up @@ -78,9 +76,9 @@ void testYauaa() {
@Test
void testServlet() {
WebApplicationContextRunner runner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(UserAgentAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(WebMvcAutoConfiguration.class, UserAgentAutoConfiguration.class));

runner.withUserConfiguration(WebMvcConfig.class).run(context -> {
runner.run(context -> {
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
assertThat(adapter.getArgumentResolvers()).isNotNull().hasAtLeastOneElementOfType(UserAgentResolver.class);

Expand All @@ -98,9 +96,9 @@ void testServlet() {
@SuppressWarnings("unchecked")
void testReactive() {
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(UserAgentAutoConfiguration.class));
.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class, UserAgentAutoConfiguration.class));

runner.withUserConfiguration(WebFluxConfig.class).run(context -> {
runner.run(context -> {
org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter adapter = context
.getBean(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter.class);

Expand All @@ -114,16 +112,4 @@ void testReactive() {
});
}

@TestConfiguration
@ImportAutoConfiguration(WebMvcAutoConfiguration.class)
static class WebMvcConfig {

}

@TestConfiguration
@ImportAutoConfiguration(WebFluxAutoConfiguration.class)
static class WebFluxConfig {

}

}

0 comments on commit db09632

Please sign in to comment.