@ -18,18 +18,15 @@ package org.springframework.boot.test.autoconfigure.web.reactive;
import java.time.Duration ;
import java.time.Duration ;
import java.time.temporal.ChronoUnit ;
import java.time.temporal.ChronoUnit ;
import java.util.Collections ;
import org.junit.After ;
import org.junit.Test ;
import org.junit.Test ;
import org.springframework.boot.autoconfigure.AutoConfigurations ;
import org.springframework.boot.test.context.runner.ApplicationContextRunner ;
import org.springframework.boot.web.codec.CodecCustomizer ;
import org.springframework.boot.web.codec.CodecCustomizer ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
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 org.springframework.context.annotation.Import ;
import org.springframework.core.env.MapPropertySource ;
import org.springframework.core.env.PropertySource ;
import org.springframework.http.codec.CodecConfigurer ;
import org.springframework.http.codec.CodecConfigurer ;
import org.springframework.test.util.ReflectionTestUtils ;
import org.springframework.test.util.ReflectionTestUtils ;
import org.springframework.test.web.reactive.server.WebTestClient ;
import org.springframework.test.web.reactive.server.WebTestClient ;
@ -44,50 +41,43 @@ import static org.mockito.Mockito.verify;
* Tests for { @link WebTestClientAutoConfiguration }
* Tests for { @link WebTestClientAutoConfiguration }
*
*
* @author Brian Clozel
* @author Brian Clozel
* @author Stephane Nicoll
* /
* /
public class WebTestClientAutoConfigurationTests {
public class WebTestClientAutoConfigurationTests {
private AnnotationConfigApplicationContext context ;
private ApplicationContextRunner contextRunner = new ApplicationContextRunner ( )
. withConfiguration ( AutoConfigurations . of (
WebTestClientAutoConfiguration . class ) ) ;
@After
@Test
public void close ( ) {
public void shouldNotBeConfiguredWithoutWebHandler ( ) {
if ( this . context ! = null ) {
this . contextRunner . run ( ( context ) - > {
this . context . close ( ) ;
assertThat ( context ) . hasNotFailed ( ) ;
}
assertThat ( context ) . doesNotHaveBean ( WebTestClient . class ) ;
} ) ;
}
}
@Test
@Test
public void shouldCustomizeClientCodecs ( ) {
public void shouldCustomizeClientCodecs ( ) {
load ( CodecConfiguration . class ) ;
this . contextRunner . withUserConfiguration ( CodecConfiguration . class )
WebTestClient webTestClient = this . context . getBean ( WebTestClient . class ) ;
. run ( ( context ) - > {
CodecCustomizer codecCustomizer = this . context . getBean ( CodecCustomizer . class ) ;
assertThat ( context ) . hasSingleBean ( WebTestClient . class ) ;
assertThat ( webTestClient ) . isNotNull ( ) ;
assertThat ( context ) . hasSingleBean ( CodecCustomizer . class ) ;
verify ( codecCustomizer ) . customize ( any ( CodecConfigurer . class ) ) ;
verify ( context . getBean ( CodecCustomizer . class ) ) . customize (
any ( CodecConfigurer . class ) ) ;
} ) ;
}
}
@Test
@Test
public void shouldCustomizeTimeout ( ) {
public void shouldCustomizeTimeout ( ) {
PropertySource < ? > propertySource = new MapPropertySource ( "test" , Collections
this . contextRunner . withUserConfiguration ( BaseConfiguration . class )
. singletonMap ( "spring.test.webtestclient.timeout" , ( Object ) "PT15M" ) ) ;
. withPropertyValues ( "spring.test.webtestclient.timeout=15m" )
load ( propertySource , BaseConfiguration . class ) ;
. run ( ( context ) - > {
WebTestClient webTestClient = this . context . getBean ( WebTestClient . class ) ;
WebTestClient webTestClient = context . getBean ( WebTestClient . class ) ;
Object duration = ReflectionTestUtils . getField ( webTestClient , "timeout" ) ;
Object duration = ReflectionTestUtils . getField ( webTestClient ,
assertThat ( duration ) . isEqualTo ( Duration . of ( 15 , ChronoUnit . MINUTES ) ) ;
"timeout" ) ;
}
assertThat ( duration ) . isEqualTo ( Duration . of ( 15 , ChronoUnit . MINUTES ) ) ;
} ) ;
private void load ( Class < ? > . . . config ) {
load ( null , config ) ;
}
private void load ( PropertySource < ? > propertySource , Class < ? > . . . config ) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ( ) ;
if ( propertySource ! = null ) {
context . getEnvironment ( ) . getPropertySources ( ) . addFirst ( propertySource ) ;
}
context . register ( config ) ;
context . register ( WebTestClientAutoConfiguration . class ) ;
context . refresh ( ) ;
this . context = context ;
}
}
@Configuration
@Configuration