Convert MustacheAutoConfigurationTests to application context runner

Closes gh-30252
pull/30505/head
Andy Wilkinson 3 years ago
parent 59f4004fa6
commit 0accc3d67d

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,13 +19,13 @@ package org.springframework.boot.autoconfigure.mustache;
import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; import org.springframework.boot.test.context.runner.AbstractApplicationContextRunner;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.view.MustacheViewResolver; import org.springframework.boot.web.servlet.view.MustacheViewResolver;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -33,77 +33,57 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link MustacheAutoConfiguration}. * Tests for {@link MustacheAutoConfiguration}.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Andy Wilkinson
*/ */
class MustacheAutoConfigurationTests { class MustacheAutoConfigurationTests {
private AnnotationConfigServletWebApplicationContext webContext;
private AnnotationConfigReactiveWebApplicationContext reactiveWebContext;
@Test @Test
void registerBeansForServletApp() { void registerBeansForServletApp() {
loadWithServlet(null); configure(new WebApplicationContextRunner()).run((context) -> {
assertThat(this.webContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1); assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(this.webContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(this.webContext.getBeansOfType(MustacheViewResolver.class)).hasSize(1); assertThat(context).hasSingleBean(MustacheViewResolver.class);
});
} }
@Test @Test
void registerCompilerForServletApp() { void registerCompilerForServletApp() {
loadWithServlet(CustomCompilerConfiguration.class); configure(new WebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class)
assertThat(this.webContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); .run((context) -> {
assertThat(this.webContext.getBeansOfType(MustacheViewResolver.class)).hasSize(1); assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(this.webContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1); assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(this.webContext.getBean(Mustache.Compiler.class).standardsMode).isTrue(); assertThat(context).hasSingleBean(MustacheViewResolver.class);
assertThat(context.getBean(Mustache.Compiler.class).standardsMode).isTrue();
});
} }
@Test @Test
void registerBeansForReactiveApp() { void registerBeansForReactiveApp() {
loadWithReactive(null); configure(new ReactiveWebApplicationContextRunner()).run((context) -> {
assertThat(this.reactiveWebContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1); assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(this.reactiveWebContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(this.reactiveWebContext.getBeansOfType(MustacheViewResolver.class)).isEmpty(); assertThat(context).doesNotHaveBean(MustacheViewResolver.class);
assertThat(this.reactiveWebContext assertThat(context)
.getBeansOfType(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class)) .hasSingleBean(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class);
.hasSize(1); });
} }
@Test @Test
void registerCompilerForReactiveApp() { void registerCompilerForReactiveApp() {
loadWithReactive(CustomCompilerConfiguration.class); configure(new ReactiveWebApplicationContextRunner()).withUserConfiguration(CustomCompilerConfiguration.class)
assertThat(this.reactiveWebContext.getBeansOfType(Mustache.Compiler.class)).hasSize(1); .run((context) -> {
assertThat(this.reactiveWebContext.getBeansOfType(MustacheResourceTemplateLoader.class)).hasSize(1); assertThat(context).hasSingleBean(Mustache.Compiler.class);
assertThat(this.reactiveWebContext.getBeansOfType(MustacheViewResolver.class)).isEmpty(); assertThat(context).hasSingleBean(MustacheResourceTemplateLoader.class);
assertThat(this.reactiveWebContext assertThat(context).doesNotHaveBean(MustacheViewResolver.class);
.getBeansOfType(org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class)) assertThat(context).hasSingleBean(
.hasSize(1); org.springframework.boot.web.reactive.result.view.MustacheViewResolver.class);
assertThat(this.reactiveWebContext.getBean(Mustache.Compiler.class).standardsMode).isTrue(); assertThat(context.getBean(Mustache.Compiler.class).standardsMode).isTrue();
} });
private void loadWithServlet(Class<?> config) {
this.webContext = new AnnotationConfigServletWebApplicationContext();
TestPropertyValues.of("spring.mustache.prefix=classpath:/mustache-templates/").applyTo(this.webContext);
if (config != null) {
this.webContext.register(config);
}
this.webContext.register(BaseConfiguration.class);
this.webContext.refresh();
} }
private void loadWithReactive(Class<?> config) { private <T extends AbstractApplicationContextRunner<T, ?, ?>> T configure(T runner) {
this.reactiveWebContext = new AnnotationConfigReactiveWebApplicationContext(); return runner.withPropertyValues("spring.mustache.prefix=classpath:/mustache-templates/")
TestPropertyValues.of("spring.mustache.prefix=classpath:/mustache-templates/").applyTo(this.reactiveWebContext); .withConfiguration(AutoConfigurations.of(MustacheAutoConfiguration.class));
if (config != null) {
this.reactiveWebContext.register(config);
}
this.reactiveWebContext.register(BaseConfiguration.class);
this.reactiveWebContext.refresh();
}
@Configuration(proxyBeanMethods = false)
@Import({ MustacheAutoConfiguration.class })
static class BaseConfiguration {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

Loading…
Cancel
Save