Migrate from ExpectedException rule to AssertJ

Replace ExpectedException JUnit rules with AssertJ exception
assertions.

Closes gh-14336
This commit is contained in:
Phillip Webb
2018-10-01 11:18:16 -07:00
parent 42cb0effc4
commit d76bba5e6f
273 changed files with 2752 additions and 3624 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package org.springframework.boot.test.autoconfigure;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
@@ -32,7 +31,7 @@ import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -47,9 +46,6 @@ public class SpringBootDependencyInjectionTestExecutionListenerTests {
@Rule
public OutputCapture out = new OutputCapture();
@Rule
public ExpectedException thrown = ExpectedException.none();
private SpringBootDependencyInjectionTestExecutionListener reportListener = new SpringBootDependencyInjectionTestExecutionListener();
@Test
@@ -86,8 +82,9 @@ public class SpringBootDependencyInjectionTestExecutionListenerTests {
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
given(testContext.getApplicationContext()).willThrow(new RuntimeException());
this.thrown.expect(is(originalFailure));
this.reportListener.prepareTestInstance(testContext);
assertThatIllegalStateException()
.isThrownBy(() -> this.reportListener.prepareTestInstance(testContext))
.isEqualTo(originalFailure);
}
@Configuration

View File

@@ -18,9 +18,7 @@ package org.springframework.boot.test.autoconfigure.data.jdbc;
import javax.sql.DataSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -33,6 +31,7 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
@@ -45,9 +44,6 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor
@TestPropertySource(properties = "spring.datasource.schema=classpath:org/springframework/boot/test/autoconfigure/data/jdbc/schema.sql")
public class DataJdbcTestIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private ExampleRepository repository;
@@ -78,8 +74,8 @@ public class DataJdbcTestIntegrationTests {
@Test
public void didNotInjectExampleComponent() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleComponent.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(
() -> this.applicationContext.getBean(ExampleComponent.class));
}
@Test

View File

@@ -18,9 +18,7 @@ package org.springframework.boot.test.autoconfigure.data.ldap;
import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -34,6 +32,7 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Sample test for {@link DataLdapTest @DataLdapTest}
@@ -46,9 +45,6 @@ import static org.assertj.core.api.Assertions.assertThat;
"spring.ldap.embedded.ldif=classpath:org/springframework/boot/test/autoconfigure/data/ldap/schema.ldif" })
public class DataLdapTestIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private LdapTemplate ldapTemplate;
@@ -72,8 +68,8 @@ public class DataLdapTestIntegrationTests {
@Test
public void didNotInjectExampleService() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleService.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,7 @@
package org.springframework.boot.test.autoconfigure.data.mongo;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -28,6 +26,7 @@ import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Sample test for {@link DataMongoTest @DataMongoTest}
@@ -38,9 +37,6 @@ import static org.assertj.core.api.Assertions.assertThat;
@DataMongoTest
public class DataMongoTestIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private MongoTemplate mongoTemplate;
@@ -61,8 +57,8 @@ public class DataMongoTestIntegrationTests {
@Test
public void didNotInjectExampleService() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleService.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}
}

View File

@@ -17,9 +17,7 @@
package org.springframework.boot.test.autoconfigure.data.neo4j;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.neo4j.ogm.session.Session;
@@ -34,6 +32,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Integration test for {@link DataNeo4jTest}.
@@ -49,9 +48,6 @@ public class DataNeo4jTestIntegrationTests {
@ClassRule
public static Neo4jContainer neo4j = new Neo4jContainer();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private Session session;
@@ -73,8 +69,8 @@ public class DataNeo4jTestIntegrationTests {
@Test
public void didNotInjectExampleService() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleService.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}
static class Initializer

View File

@@ -20,9 +20,7 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -38,6 +36,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Integration test for {@link DataRedisTest}.
@@ -52,9 +51,6 @@ public class DataRedisTestIntegrationTests {
@ClassRule
public static RedisContainer redis = new RedisContainer();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private RedisOperations<Object, Object> operations;
@@ -80,8 +76,8 @@ public class DataRedisTestIntegrationTests {
@Test
public void didNotInjectExampleService() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleService.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
}
static class Initializer

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,9 +20,7 @@ import java.util.Collection;
import javax.sql.DataSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -35,6 +33,7 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
@@ -47,9 +46,6 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor
@TestPropertySource(properties = "spring.datasource.schema=classpath:org/springframework/boot/test/autoconfigure/jdbc/schema.sql")
public class JdbcTestIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private JdbcTemplate jdbcTemplate;
@@ -79,8 +75,8 @@ public class JdbcTestIntegrationTests {
@Test
public void didNotInjectExampleRepository() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleRepository.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(
() -> this.applicationContext.getBean(ExampleRepository.class));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,9 +20,7 @@ import javax.sql.DataSource;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -34,6 +32,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
@@ -45,9 +44,6 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor
@JooqTest
public class JooqTestIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private DSLContext dsl;
@@ -73,8 +69,8 @@ public class JooqTestIntegrationTests {
@Test
public void didNotInjectExampleComponent() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleComponent.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(
() -> this.applicationContext.getBean(ExampleComponent.class));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,9 +18,7 @@ package org.springframework.boot.test.autoconfigure.orm.jpa;
import javax.sql.DataSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -33,6 +31,7 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration;
/**
@@ -46,9 +45,6 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor
@TestPropertySource(properties = "spring.jpa.hibernate.use-new-id-generator-mappings=false")
public class DataJpaTestIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private TestEntityManager entities;
@@ -102,8 +98,8 @@ public class DataJpaTestIntegrationTests {
@Test
public void didNotInjectExampleComponent() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.applicationContext.getBean(ExampleComponent.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(
() -> this.applicationContext.getBean(ExampleComponent.class));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,9 +21,7 @@ import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnitUtil;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -31,6 +29,8 @@ import org.springframework.orm.jpa.EntityManagerHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
@@ -41,9 +41,6 @@ import static org.mockito.Mockito.verify;
*/
public class TestEntityManagerTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Mock
private EntityManagerFactory entityManagerFactory;
@@ -65,9 +62,8 @@ public class TestEntityManagerTests {
@Test
public void createWhenEntityManagerIsNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("EntityManagerFactory must not be null");
new TestEntityManager(null);
assertThatIllegalArgumentException().isThrownBy(() -> new TestEntityManager(null))
.withMessageContaining("EntityManagerFactory must not be null");
}
@Test
@@ -191,9 +187,10 @@ public class TestEntityManagerTests {
public void getIdForTypeWhenTypeIsWrongShouldThrowException() {
TestEntity entity = new TestEntity();
given(this.persistenceUnitUtil.getIdentifier(entity)).willReturn(123);
this.thrown.expectMessage("ID mismatch: Object of class [java.lang.Integer] "
+ "must be an instance of class java.lang.Long");
this.testEntityManager.getId(entity, Long.class);
assertThatIllegalArgumentException()
.isThrownBy(() -> this.testEntityManager.getId(entity, Long.class))
.withMessageContaining("ID mismatch: Object of class [java.lang.Integer] "
+ "must be an instance of class java.lang.Long");
}
@Test
@@ -213,9 +210,9 @@ public class TestEntityManagerTests {
@Test
public void getEntityManagerWhenNotSetShouldThrowException() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("No transactional EntityManager found");
this.testEntityManager.getEntityManager();
assertThatIllegalStateException()
.isThrownBy(this.testEntityManager::getEntityManager)
.withMessageContaining("No transactional EntityManager found");
}
private void bindEntityManager() {

View File

@@ -16,9 +16,7 @@
package org.springframework.boot.test.autoconfigure.override;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -33,6 +31,7 @@ import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Integration tests for {@link OverrideAutoConfiguration} when {@code enabled} is
@@ -46,9 +45,6 @@ import static org.assertj.core.api.Assertions.assertThat;
@ImportAutoConfiguration(ExampleTestConfig.class)
public class OverrideAutoConfigurationEnabledFalseIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private ApplicationContext context;
@@ -56,8 +52,8 @@ public class OverrideAutoConfigurationEnabledFalseIntegrationTests {
public void disabledAutoConfiguration() {
ApplicationContext context = this.context;
assertThat(context.getBean(ExampleTestConfig.class)).isNotNull();
this.thrown.expect(NoSuchBeanDefinitionException.class);
context.getBean(ConfigurationPropertiesBindingPostProcessor.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(
() -> context.getBean(ConfigurationPropertiesBindingPostProcessor.class));
}
}

View File

@@ -19,13 +19,12 @@ package org.springframework.boot.test.autoconfigure.properties;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.annotation.AliasFor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link AnnotationsPropertySource}.
@@ -35,14 +34,11 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class AnnotationsPropertySourceTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void createWhenSourceIsNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Property source must not be null");
new AnnotationsPropertySource(null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new AnnotationsPropertySource(null))
.withMessageContaining("Property source must not be null");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,9 +19,7 @@ package org.springframework.boot.test.autoconfigure.properties;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -32,6 +30,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ContextCustomizer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -43,9 +42,6 @@ import static org.mockito.Mockito.verifyZeroInteractions;
*/
public class PropertyMappingContextCustomizerFactoryTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private PropertyMappingContextCustomizerFactory factory = new PropertyMappingContextCustomizerFactory();
@Test
@@ -106,11 +102,11 @@ public class PropertyMappingContextCustomizerFactoryTests {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ConfigMapping.class);
customizer.customizeContext(context, null);
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("The @PropertyMapping annotation "
+ "@PropertyMappingContextCustomizerFactoryTests.TypeMappingAnnotation "
+ "cannot be used in combination with the @Component annotation @Configuration");
context.refresh();
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(context::refresh)
.withMessageContaining("The @PropertyMapping annotation "
+ "@PropertyMappingContextCustomizerFactoryTests.TypeMappingAnnotation "
+ "cannot be used in combination with the @Component annotation @Configuration");
}
@NoMappingAnnotation

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,7 @@
package org.springframework.boot.test.autoconfigure.web.client;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,6 +26,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@@ -40,9 +39,6 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
@RestClientTest({ ExampleRestClient.class, AnotherExampleRestClient.class })
public class RestClientTestTwoComponentsIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private ExampleRestClient client1;
@@ -57,10 +53,10 @@ public class RestClientTestTwoComponentsIntegrationTests {
@Test
public void serverShouldNotWork() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Unable to use auto-configured");
this.server.expect(requestTo("/test"))
.andRespond(withSuccess("hello", MediaType.TEXT_HTML));
assertThatIllegalStateException()
.isThrownBy(() -> this.server.expect(requestTo("/test"))
.andRespond(withSuccess("hello", MediaType.TEXT_HTML)))
.withMessageContaining("Unable to use auto-configured");
}
@Test

View File

@@ -18,9 +18,7 @@ package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
import javax.validation.ConstraintViolationException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -29,9 +27,10 @@ import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.util.NestedServletException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.isA;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -47,9 +46,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@WithMockUser
public class WebMvcTestAllControllersIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private MockMvc mvc;
@@ -82,8 +78,9 @@ public class WebMvcTestAllControllersIntegrationTests {
@Test
public void shouldRunValidationFailure() throws Exception {
this.thrown.expectCause(isA(ConstraintViolationException.class));
this.mvc.perform(get("/three/invalid"));
assertThatExceptionOfType(NestedServletException.class)
.isThrownBy(() -> this.mvc.perform(get("/three/invalid")))
.withCauseInstanceOf(ConstraintViolationException.class);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,9 +17,7 @@
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
import com.gargoylesoftware.htmlunit.WebClient;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
@@ -31,6 +29,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
@@ -45,9 +44,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@AutoConfigureMockMvc(addFilters = false, webClientEnabled = false, webDriverEnabled = false)
public class WebMvcTestWithAutoConfigureMockMvcIntegrationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Autowired
private ApplicationContext context;
@@ -61,14 +57,14 @@ public class WebMvcTestWithAutoConfigureMockMvcIntegrationTests {
@Test
public void shouldNotHaveWebDriver() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(WebDriver.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.context.getBean(WebDriver.class));
}
@Test
public void shouldNotHaveWebClient() {
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(WebClient.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.context.getBean(WebClient.class));
}
}