Migrate away from ExpectedException (#22922)

* Add limited checkstyles to test code

Add a limited set of checkstyle rules to the test codebase to improve
code consistency.

* Fix checksyle violations in test code

* Organize imports to fix checkstyle for test code

* Migrate to assertThatExceptionOfType

Migrate aware from ExpectedException rules to AssertJ exception
assertions. Also include a checkstyle rules to ensure that the
the ExpectedException is not accidentally used in the future.

See gh-22894
This commit is contained in:
Phil Webb
2019-05-08 07:25:52 -07:00
committed by Sam Brannen
parent 7e6e3d7027
commit d7320de871
671 changed files with 3861 additions and 4601 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -22,7 +22,7 @@ import org.junit.Test;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Unit tests for {@link MockServerHttpRequest}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -22,7 +22,7 @@ import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Unit tests for {@link MockServerHttpResponse}.

View File

@@ -16,10 +16,9 @@
package org.springframework.mock.web;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.*;
/**
@@ -31,9 +30,6 @@ import static org.junit.Assert.*;
*/
public class MockCookieTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void constructCookie() {
@@ -86,25 +82,25 @@ public class MockCookieTests {
@Test
public void parseNullHeader() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Set-Cookie header must not be null");
MockCookie.parse(null);
assertThatIllegalArgumentException().isThrownBy(() ->
MockCookie.parse(null))
.withMessageContaining("Set-Cookie header must not be null");
}
@Test
public void parseInvalidHeader() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Invalid Set-Cookie header 'BOOM'");
MockCookie.parse("BOOM");
assertThatIllegalArgumentException().isThrownBy(() ->
MockCookie.parse("BOOM"))
.withMessageContaining("Invalid Set-Cookie header 'BOOM'");
}
@Test
public void parseInvalidAttribute() {
String header = "SESSION=123; Path=";
exception.expect(IllegalArgumentException.class);
exception.expectMessage("No value in attribute 'Path' for Set-Cookie header '" + header + "'");
MockCookie.parse(header);
assertThatIllegalArgumentException().isThrownBy(() ->
MockCookie.parse(header))
.withMessageContaining("No value in attribute 'Path' for Set-Cookie header '" + header + "'");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -28,10 +28,10 @@ import javax.servlet.ServletResponse;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.mockito.Mockito.*;
/**
* Test fixture for {@link MockFilterChain}.

View File

@@ -29,14 +29,13 @@ import java.util.Locale;
import java.util.Map;
import javax.servlet.http.Cookie;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpHeaders;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.*;
/**
@@ -56,9 +55,6 @@ public class MockHttpServletRequestTests {
private final MockHttpServletRequest request = new MockHttpServletRequest();
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void protocolAndScheme() {
@@ -104,9 +100,9 @@ public class MockHttpServletRequestTests {
@Test
public void getContentAsStringWithoutSettingCharacterEncoding() throws IOException {
exception.expect(IllegalStateException.class);
exception.expectMessage("Cannot get content as a String for a null character encoding");
request.getContentAsString();
assertThatIllegalStateException().isThrownBy(
request::getContentAsString)
.withMessageContaining("Cannot get content as a String for a null character encoding");
}
@Test
@@ -142,20 +138,18 @@ public class MockHttpServletRequestTests {
@Test // SPR-16499
public void getReaderAfterGettingInputStream() throws IOException {
exception.expect(IllegalStateException.class);
exception.expectMessage(
"Cannot call getReader() after getInputStream() has already been called for the current request");
request.getInputStream();
request.getReader();
assertThatIllegalStateException().isThrownBy(
request::getReader)
.withMessageContaining("Cannot call getReader() after getInputStream() has already been called for the current request");
}
@Test // SPR-16499
public void getInputStreamAfterGettingReader() throws IOException {
exception.expect(IllegalStateException.class);
exception.expectMessage(
"Cannot call getInputStream() after getReader() has already been called for the current request");
request.getReader();
request.getInputStream();
assertThatIllegalStateException().isThrownBy(
request::getInputStream)
.withMessageContaining("Cannot call getInputStream() after getReader() has already been called for the current request");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -26,11 +26,7 @@ import org.junit.Test;
import org.springframework.http.MediaType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,18 +19,16 @@ package org.springframework.test.context;
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.test.context.support.DefaultTestContextBootstrapper;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.web.WebTestContextBootstrapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.context.BootstrapUtils.*;
/**
* Unit tests for {@link BootstrapUtils}.
@@ -43,30 +41,23 @@ public class BootstrapUtilsTests {
private final CacheAwareContextLoaderDelegate delegate = mock(CacheAwareContextLoaderDelegate.class);
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void resolveTestContextBootstrapperWithEmptyBootstrapWithAnnotation() {
BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(EmptyBootstrapWithAnnotationClass.class, delegate);
exception.expect(IllegalStateException.class);
exception.expectMessage("Specify @BootstrapWith's 'value' attribute");
resolveTestContextBootstrapper(bootstrapContext);
assertThatIllegalStateException().isThrownBy(() ->
resolveTestContextBootstrapper(bootstrapContext))
.withMessageContaining("Specify @BootstrapWith's 'value' attribute");
}
@Test
public void resolveTestContextBootstrapperWithDoubleMetaBootstrapWithAnnotations() {
BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(
DoubleMetaAnnotatedBootstrapWithAnnotationClass.class, delegate);
exception.expect(IllegalStateException.class);
exception.expectMessage("Configuration error: found multiple declarations of @BootstrapWith");
exception.expectMessage(FooBootstrapper.class.getName());
exception.expectMessage(BarBootstrapper.class.getName());
resolveTestContextBootstrapper(bootstrapContext);
assertThatIllegalStateException().isThrownBy(() ->
resolveTestContextBootstrapper(bootstrapContext))
.withMessageContaining("Configuration error: found multiple declarations of @BootstrapWith")
.withMessageContaining(FooBootstrapper.class.getName())
.withMessageContaining(BarBootstrapper.class.getName());
}
@Test
@@ -156,12 +147,12 @@ public class BootstrapUtilsTests {
@BootWithFoo
@BootWithFooAgain
static class DuplicateMetaAnnotatedBootstrapWithAnnotationClass {}
@BootWithFoo
@BootWithBar
@BootstrapWith(EnigmaBootstrapper.class)
static class LocalDeclarationAndMetaAnnotatedBootstrapWithAnnotationClass {}
@WebAppConfiguration
static class WebAppConfigurationAnnotatedClass {}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -24,11 +24,11 @@ import java.util.stream.IntStream;
import org.junit.Test;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toCollection;
import static org.hamcrest.CoreMatchers.equalTo;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Integration tests that verify proper concurrency support between a

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -23,6 +23,8 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.testng.ITestNGListener;
import org.testng.TestNG;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
@@ -36,9 +38,6 @@ import org.springframework.test.context.support.DirtiesContextTestExecutionListe
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.testng.TrackingTestNGTestListener;
import org.testng.ITestNGListener;
import org.testng.TestNG;
import static org.junit.Assert.*;
import static org.springframework.test.context.cache.ContextCacheTestUtils.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -23,8 +23,8 @@ import org.junit.Test;
import org.springframework.core.SpringProperties;
import static org.junit.Assert.*;
import static org.springframework.test.context.cache.ContextCacheUtils.*;
import static org.springframework.test.context.cache.ContextCache.*;
import static org.springframework.test.context.cache.ContextCacheUtils.*;
/**
* Unit tests for {@link ContextCacheUtils}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -25,7 +25,7 @@ import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
/**
* @author Sam Brannen

View File

@@ -33,8 +33,8 @@ import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.event.CustomTestEventTests.CustomEventPublishingTestExecutionListener;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.context.TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.test.context.TestExecutionListeners.MergeMode.*;
/**
* Integration tests for custom event publication via

View File

@@ -24,9 +24,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
@@ -53,6 +51,7 @@ import org.springframework.util.ReflectionUtils;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -85,9 +84,6 @@ public class EventPublishingTestExecutionListenerIntegrationTests {
private final Object testInstance = new ExampleTestCase();
private final Method traceableTestMethod = ReflectionUtils.findMethod(ExampleTestCase.class, "traceableTest");
@Rule
public final ExpectedException exception = ExpectedException.none();
@After
public void closeApplicationContext() {
@@ -132,16 +128,10 @@ public class EventPublishingTestExecutionListenerIntegrationTests {
@Test
public void beforeTestMethodAnnotationWithFailingEventListener() throws Exception {
Method method = ReflectionUtils.findMethod(ExampleTestCase.class, "testWithFailingEventListener");
exception.expect(RuntimeException.class);
exception.expectMessage("Boom!");
try {
testContextManager.beforeTestMethod(testInstance, method);
}
finally {
verify(listener, only()).beforeTestMethod(testContext);
}
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
testContextManager.beforeTestMethod(testInstance, method))
.withMessageContaining("Boom!");
verify(listener, only()).beforeTestMethod(testContext);
}
/**

View File

@@ -33,15 +33,14 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.test.context.TestContext;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link EventPublishingTestExecutionListener}.
*
*
* @author Frank Scheffler
* @author Sam Brannen
* @since 5.2

View File

@@ -16,10 +16,7 @@
package org.springframework.test.context.jdbc;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
@@ -29,8 +26,8 @@ import org.springframework.core.io.Resource;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.jdbc.SqlConfig.TransactionMode;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.*;
/**
@@ -45,9 +42,6 @@ public class SqlScriptsTestExecutionListenerTests {
private final TestContext testContext = mock(TestContext.class);
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void missingValueAndScriptsAndStatementsAtClassLevel() throws Exception {
@@ -73,15 +67,11 @@ public class SqlScriptsTestExecutionListenerTests {
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("foo"));
exception.expect(AnnotationConfigurationException.class);
exception.expectMessage(either(
containsString("attribute 'value' and its alias 'scripts'")).or(
containsString("attribute 'scripts' and its alias 'value'")));
exception.expectMessage(either(containsString("values of [{foo}] and [{bar}]")).or(
containsString("values of [{bar}] and [{foo}]")));
exception.expectMessage(either(containsString("but only one is permitted")).or(
containsString("Different @AliasFor mirror values")));
listener.beforeTestMethod(testContext);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
listener.beforeTestMethod(testContext))
.withMessageContaining("Different @AliasFor mirror values")
.withMessageContaining("attribute 'scripts' and its alias 'value'")
.withMessageContaining("values of [{bar}] and [{foo}]");
}
@Test
@@ -113,14 +103,9 @@ public class SqlScriptsTestExecutionListenerTests {
}
private void assertExceptionContains(String msg) throws Exception {
try {
listener.beforeTestMethod(testContext);
fail("Should have thrown an IllegalStateException.");
}
catch (IllegalStateException e) {
// System.err.println(e.getMessage());
assertTrue("Exception message should contain: " + msg, e.getMessage().contains(msg));
}
assertThatIllegalStateException().isThrownBy(() ->
listener.beforeTestMethod(testContext))
.withMessageContaining(msg);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,7 +20,6 @@ import java.lang.reflect.Method;
import java.util.Optional;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExtensionContext;
@@ -35,7 +34,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.MatcherAssert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -24,8 +24,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;
/**
* Integration tests which verify support for {@link DisabledIf @DisabledIf}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -24,9 +24,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;
/**
* Integration tests which verify support for {@link EnabledIf @EnabledIf}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,7 +19,6 @@ package org.springframework.test.context.junit.jupiter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.junit.jupiter.api.DynamicTest;
@@ -32,7 +31,6 @@ import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import org.opentest4j.AssertionFailedError;
import org.springframework.context.annotation.Bean;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -30,6 +30,7 @@ import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -27,7 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.junit.jupiter.nested.NestedTestsWithSpringAndJUnitJupiterTests.TopLevelConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
/**
* Integration tests that verify support for {@code @Nested} test classes

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -32,7 +32,7 @@ import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
/**
* Integration tests that verify support for {@link Nested @Nested} test classes in

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -28,8 +28,7 @@ import org.junit.runner.notification.RunNotifier;
import org.springframework.beans.BeanUtils;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
/**
* Collection of utilities for testing the execution of JUnit 4 based tests.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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,6 +21,7 @@ import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -23,11 +23,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* JUnit 4 based integration test which verifies that {@link @ContextConfiguration}
* is optional.
* JUnit 4 based integration test which verifies that
* {@link ContextConfiguration @ContextConfiguration} is optional.
*
* @author Phillip Webb
* @author Sam Brannen

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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,6 +19,7 @@ package org.springframework.test.context.junit4;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.test.context.cache.ClassLevelDirtiesContextTests;
import org.springframework.test.context.cache.SpringRunnerContextCacheTests;
import org.springframework.test.context.jdbc.IsolatedTransactionModeSqlScriptsTests;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -24,6 +24,7 @@ import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
@@ -34,7 +35,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit4.aci.annotation.InitializerConfiguredViaMetaAnnotationTests.ComposedContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Integration test that demonstrates how to register one or more {@code @Configuration}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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,6 +16,7 @@
package org.springframework.test.context.junit4.nested;
import de.bechte.junit.runners.context.HierarchicalContextRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -27,10 +28,7 @@ import org.springframework.test.context.junit4.nested.NestedTestsWithSpringRules
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import de.bechte.junit.runners.context.HierarchicalContextRunner;
import static org.junit.Assert.*;
/**
* JUnit 4 based integration tests for <em>nested</em> test classes that are

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2002-2019 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2002-2019 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2007 the original author or authors.
* Copyright 2002-2019 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2007 the original author or authors.
* Copyright 2002-2019 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2019 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.
@@ -24,7 +24,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.core.IsEqual.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,6 +21,7 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,6 +21,7 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,6 +21,7 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,6 +21,7 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,14 +19,11 @@ package org.springframework.test.context.junit4.statements;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runners.model.Statement;
import org.mockito.stubbing.Answer;
import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
/**
@@ -40,28 +37,25 @@ public class SpringFailOnTimeoutTests {
private Statement statement = mock(Statement.class);
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void nullNextStatement() throws Throwable {
exception.expect(IllegalArgumentException.class);
new SpringFailOnTimeout(null, 1);
assertThatIllegalArgumentException().isThrownBy(() ->
new SpringFailOnTimeout(null, 1));
}
@Test
public void negativeTimeout() throws Throwable {
exception.expect(IllegalArgumentException.class);
new SpringFailOnTimeout(statement, -1);
assertThatIllegalArgumentException().isThrownBy(() ->
new SpringFailOnTimeout(statement, -1));
}
@Test
public void userExceptionPropagates() throws Throwable {
doThrow(new Boom()).when(statement).evaluate();
exception.expect(Boom.class);
new SpringFailOnTimeout(statement, 1).evaluate();
assertThatExceptionOfType(Boom.class).isThrownBy(() ->
new SpringFailOnTimeout(statement, 1).evaluate());
}
@Test
@@ -71,16 +65,13 @@ public class SpringFailOnTimeoutTests {
return null;
}).when(statement).evaluate();
exception.expect(TimeoutException.class);
new SpringFailOnTimeout(statement, 1).evaluate();
assertThatExceptionOfType(TimeoutException.class).isThrownBy(() ->
new SpringFailOnTimeout(statement, 1).evaluate());
}
@Test
public void noExceptionThrownIfNoUserExceptionAndTimeoutDoesNotOccur() throws Throwable {
doAnswer((Answer<Void>) invocation -> {
return null;
}).when(statement).evaluate();
doAnswer((Answer<Void>) invocation -> null).when(statement).evaluate();
new SpringFailOnTimeout(statement, 100).evaluate();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 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,13 +16,11 @@
package org.springframework.test.context.support;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.context.MergedContextConfiguration;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
/**
@@ -38,21 +36,17 @@ public class AnnotationConfigContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
/**
* @since 4.0.4
*/
@Test
public void configMustNotContainLocations() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support resource locations"));
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, contextLoader);
contextLoader.loadContext(mergedConfig);
assertThatIllegalStateException().isThrownBy(() ->
contextLoader.loadContext(mergedConfig))
.withMessageContaining("does not support resource locations");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
@@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.Test;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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 java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.context.BootstrapTestUtils;
import org.springframework.test.context.ContextConfiguration;
@@ -32,9 +30,8 @@ import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.web.WebDelegatingSmartContextLoader;
import org.springframework.test.context.web.WebMergedContextConfiguration;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
/**
* Unit tests for {@link BootstrapTestUtils} involving {@link MergedContextConfiguration}.
@@ -44,10 +41,6 @@ import static org.junit.Assert.assertNotEquals;
*/
public class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigurationUtilsTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void buildImplicitMergedConfigWithoutAnnotation() {
Class<?> testClass = Enigma.class;
@@ -61,11 +54,10 @@ public class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigur
*/
@Test
public void buildMergedConfigWithContextConfigurationWithoutLocationsClassesOrInitializers() {
exception.expect(IllegalStateException.class);
exception.expectMessage(startsWith("DelegatingSmartContextLoader was unable to detect defaults, "
+ "and no ApplicationContextInitializers or ContextCustomizers were declared for context configuration attributes"));
buildMergedContextConfiguration(MissingContextAttributesTestCase.class);
assertThatIllegalStateException().isThrownBy(() ->
buildMergedContextConfiguration(MissingContextAttributesTestCase.class))
.withMessageStartingWith("DelegatingSmartContextLoader was unable to detect defaults, "
+ "and no ApplicationContextInitializers or ContextCustomizers were declared for context configuration attributes");
}
@Test

View File

@@ -18,16 +18,14 @@ package org.springframework.test.context.support;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.annotation.AnnotationConfigurationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextLoader;
import static org.hamcrest.Matchers.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.springframework.test.context.support.ContextLoaderUtils.*;
@@ -39,10 +37,6 @@ import static org.springframework.test.context.support.ContextLoaderUtils.*;
*/
public class ContextLoaderUtilsConfigurationAttributesTests extends AbstractContextConfigurationUtilsTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
private void assertLocationsFooAttributes(ContextConfigurationAttributes attributes) {
assertAttributes(attributes, LocationsFoo.class, new String[] { "/foo.xml" }, EMPTY_CLASS_ARRAY,
ContextLoader.class, false);
@@ -65,18 +59,12 @@ public class ContextLoaderUtilsConfigurationAttributesTests extends AbstractCont
@Test
public void resolveConfigAttributesWithConflictingLocations() {
exception.expect(AnnotationConfigurationException.class);
exception.expectMessage(containsString(ConflictingLocations.class.getName()));
exception.expectMessage(either(
containsString("attribute 'value' and its alias 'locations'")).or(
containsString("attribute 'locations' and its alias 'value'")));
exception.expectMessage(either(
containsString("values of [{x}] and [{y}]")).or(
containsString("values of [{y}] and [{x}]")));
exception.expectMessage(either(
containsString("Different @AliasFor mirror values")).or(
containsString("but only one is permitted")));
resolveContextConfigurationAttributes(ConflictingLocations.class);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
resolveContextConfigurationAttributes(ConflictingLocations.class))
.withMessageStartingWith("Different @AliasFor mirror values")
.withMessageContaining(ConflictingLocations.class.getName())
.withMessageContaining("attribute 'locations' and its alias 'value'")
.withMessageContaining("values of [{y}] and [{x}]");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.test.context.support;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
@@ -29,7 +27,7 @@ import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.util.ObjectUtils;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
/**
@@ -45,9 +43,6 @@ public class DelegatingSmartContextLoaderTests {
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader();
@Rule
public ExpectedException expectedException = ExpectedException.none();
private static void assertEmpty(Object[] array) {
assertTrue(ObjectUtils.isEmpty(array));
@@ -75,13 +70,12 @@ public class DelegatingSmartContextLoaderTests {
@Test
public void processContextConfigurationWithDefaultXmlConfigAndConfigurationClassGeneration() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("both default locations AND default configuration classes were detected"));
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(
ImproperDuplicateDefaultXmlAndConfigClassTestCase.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY,
true, null, true, ContextLoader.class);
loader.processContextConfiguration(configAttributes);
assertThatIllegalStateException().isThrownBy(() ->
loader.processContextConfiguration(configAttributes))
.withMessageContaining("both default locations AND default configuration classes were detected");
}
@Test
@@ -114,13 +108,12 @@ public class DelegatingSmartContextLoaderTests {
@Test
public void loadContextWithoutLocationsAndConfigurationClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Neither"));
expectedException.expectMessage(containsString("was able to load an ApplicationContext from"));
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(
getClass(), EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
assertThatIllegalStateException().isThrownBy(() ->
loader.loadContext(mergedConfig))
.withMessageStartingWith("Neither")
.withMessageContaining("was able to load an ApplicationContext from");
}
/**
@@ -128,13 +121,12 @@ public class DelegatingSmartContextLoaderTests {
*/
@Test
public void loadContextWithLocationsAndConfigurationClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Neither"));
expectedException.expectMessage(endsWith("declare either 'locations' or 'classes' but not both."));
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
new String[] {"test.xml"}, new Class<?>[] {getClass()}, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
assertThatIllegalStateException().isThrownBy(() ->
loader.loadContext(mergedConfig))
.withMessageStartingWith("Neither")
.withMessageContaining("declare either 'locations' or 'classes' but not both.");
}
private void assertApplicationContextLoadsAndContainsFooString(MergedContextConfiguration mergedConfig)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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,7 +20,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.Test;
import org.mockito.BDDMockito;
import org.springframework.test.annotation.DirtiesContext;
@@ -29,6 +28,7 @@ import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.*;
import static org.springframework.test.annotation.DirtiesContext.HierarchyMode.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 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,13 +16,11 @@
package org.springframework.test.context.support;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.context.MergedContextConfiguration;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
/**
* Unit tests for {@link GenericPropertiesContextLoader}.
@@ -34,19 +32,15 @@ public class GenericPropertiesContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void configMustNotContainAnnotatedClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support annotated classes"));
GenericPropertiesContextLoader loader = new GenericPropertiesContextLoader();
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
assertThatIllegalStateException().isThrownBy(() ->
loader.loadContext(mergedConfig))
.withMessageContaining("does not support annotated classes");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
@@ -25,6 +25,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 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,13 +16,11 @@
package org.springframework.test.context.support;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.context.MergedContextConfiguration;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
/**
* Unit tests for {@link GenericXmlContextLoader}.
@@ -35,19 +33,15 @@ public class GenericXmlContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void configMustNotContainAnnotatedClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support annotated classes"));
GenericXmlContextLoader loader = new GenericXmlContextLoader();
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
assertThatIllegalStateException().isThrownBy(() ->
loader.loadContext(mergedConfig))
.withMessageContaining("does not support annotated classes");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.test.context.support;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.AnnotationConfigurationException;
@@ -32,9 +30,9 @@ import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.test.context.TestPropertySource;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.context.support.TestPropertySourceUtils.*;
@@ -53,24 +51,21 @@ public class TestPropertySourceUtilsTests {
private static final String[] FOO_LOCATIONS = new String[] {"classpath:/foo.properties"};
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void emptyAnnotation() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Could not detect default properties file for test"));
expectedException.expectMessage(containsString("EmptyPropertySources.properties"));
buildMergedTestPropertySources(EmptyPropertySources.class);
assertThatIllegalStateException().isThrownBy(() ->
buildMergedTestPropertySources(EmptyPropertySources.class))
.withMessageStartingWith("Could not detect default properties file for test")
.withMessageContaining("EmptyPropertySources.properties");
}
@Test
public void extendedEmptyAnnotation() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Could not detect default properties file for test"));
expectedException.expectMessage(containsString("ExtendedEmptyPropertySources.properties"));
buildMergedTestPropertySources(ExtendedEmptyPropertySources.class);
assertThatIllegalStateException().isThrownBy(() ->
buildMergedTestPropertySources(ExtendedEmptyPropertySources.class))
.withMessageStartingWith("Could not detect default properties file for test")
.withMessageContaining("ExtendedEmptyPropertySources.properties");
}
@Test
@@ -81,8 +76,8 @@ public class TestPropertySourceUtilsTests {
@Test
public void locationsAndValueAttributes() {
expectedException.expect(AnnotationConfigurationException.class);
buildMergedTestPropertySources(LocationsAndValuePropertySources.class);
assertThatExceptionOfType(AnnotationConfigurationException.class).isThrownBy(() ->
buildMergedTestPropertySources(LocationsAndValuePropertySources.class));
}
@Test
@@ -125,30 +120,30 @@ public class TestPropertySourceUtilsTests {
@Test
public void addPropertiesFilesToEnvironmentWithNullContext() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment((ConfigurableApplicationContext) null, FOO_LOCATIONS);
assertThatIllegalArgumentException().isThrownBy(() ->
addPropertiesFilesToEnvironment((ConfigurableApplicationContext) null, FOO_LOCATIONS))
.withMessageContaining("must not be null");
}
@Test
public void addPropertiesFilesToEnvironmentWithContextAndNullLocations() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null);
assertThatIllegalArgumentException().isThrownBy(() ->
addPropertiesFilesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null))
.withMessageContaining("must not be null");
}
@Test
public void addPropertiesFilesToEnvironmentWithNullEnvironment() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment((ConfigurableEnvironment) null, mock(ResourceLoader.class), FOO_LOCATIONS);
assertThatIllegalArgumentException().isThrownBy(() ->
addPropertiesFilesToEnvironment((ConfigurableEnvironment) null, mock(ResourceLoader.class), FOO_LOCATIONS))
.withMessageContaining("must not be null");
}
@Test
public void addPropertiesFilesToEnvironmentWithEnvironmentAndNullLocations() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment(new MockEnvironment(), mock(ResourceLoader.class), (String[]) null);
assertThatIllegalArgumentException().isThrownBy(() ->
addPropertiesFilesToEnvironment(new MockEnvironment(), mock(ResourceLoader.class), (String[]) null))
.withMessageContaining("must not be null");
}
@Test
@@ -171,44 +166,44 @@ public class TestPropertySourceUtilsTests {
@Test
public void addInlinedPropertiesToEnvironmentWithNullContext() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("context");
addInlinedPropertiesToEnvironment((ConfigurableApplicationContext) null, KEY_VALUE_PAIR);
assertThatIllegalArgumentException().isThrownBy(() ->
addInlinedPropertiesToEnvironment((ConfigurableApplicationContext) null, KEY_VALUE_PAIR))
.withMessageContaining("context");
}
@Test
public void addInlinedPropertiesToEnvironmentWithContextAndNullInlinedProperties() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("inlined");
addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null);
assertThatIllegalArgumentException().isThrownBy(() ->
addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null))
.withMessageContaining("inlined");
}
@Test
public void addInlinedPropertiesToEnvironmentWithNullEnvironment() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("environment");
addInlinedPropertiesToEnvironment((ConfigurableEnvironment) null, KEY_VALUE_PAIR);
assertThatIllegalArgumentException().isThrownBy(() ->
addInlinedPropertiesToEnvironment((ConfigurableEnvironment) null, KEY_VALUE_PAIR))
.withMessageContaining("environment");
}
@Test
public void addInlinedPropertiesToEnvironmentWithEnvironmentAndNullInlinedProperties() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("inlined");
addInlinedPropertiesToEnvironment(new MockEnvironment(), (String[]) null);
assertThatIllegalArgumentException().isThrownBy(() ->
addInlinedPropertiesToEnvironment(new MockEnvironment(), (String[]) null))
.withMessageContaining("inlined");
}
@Test
public void addInlinedPropertiesToEnvironmentWithMalformedUnicodeInValue() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Failed to load test environment property");
addInlinedPropertiesToEnvironment(new MockEnvironment(), asArray("key = \\uZZZZ"));
assertThatIllegalStateException().isThrownBy(() ->
addInlinedPropertiesToEnvironment(new MockEnvironment(), asArray("key = \\uZZZZ")))
.withMessageContaining("Failed to load test environment property");
}
@Test
public void addInlinedPropertiesToEnvironmentWithMultipleKeyValuePairsInSingleInlinedProperty() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Failed to load exactly one test environment property");
addInlinedPropertiesToEnvironment(new MockEnvironment(), asArray("a=b\nx=y"));
assertThatIllegalStateException().isThrownBy(() ->
addInlinedPropertiesToEnvironment(new MockEnvironment(), asArray("a=b\nx=y")))
.withMessageContaining("Failed to load exactly one test environment property");
}
@Test
@@ -225,9 +220,9 @@ public class TestPropertySourceUtilsTests {
@Test
public void convertInlinedPropertiesToMapWithNullInlinedProperties() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("inlined");
convertInlinedPropertiesToMap((String[]) null);
assertThatIllegalArgumentException().isThrownBy(() ->
convertInlinedPropertiesToMap((String[]) null))
.withMessageContaining("inlined");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,6 +16,8 @@
package org.springframework.test.context.testng;
import org.testng.annotations.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -23,8 +25,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.tests.sample.beans.Employee;
import org.springframework.tests.sample.beans.Pet;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,13 +16,13 @@
package org.springframework.test.context.testng.transaction.ejb;
import org.testng.annotations.Test;
import org.springframework.test.annotation.Commit;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.context.transaction.ejb.dao.RequiredEjbTxTestEntityDao;
import org.testng.annotations.Test;
/**
* Concrete subclass of {@link AbstractEjbTxDaoTestNGTests} which uses the
* {@link RequiredEjbTxTestEntityDao} and sets the default rollback semantics

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,13 +16,13 @@
package org.springframework.test.context.testng.transaction.ejb;
import org.testng.annotations.Test;
import org.springframework.test.annotation.Commit;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.context.transaction.ejb.dao.RequiresNewEjbTxTestEntityDao;
import org.testng.annotations.Test;
/**
* Concrete subclass of {@link AbstractEjbTxDaoTestNGTests} which uses the
* {@link RequiresNewEjbTxTestEntityDao} and sets the default rollback semantics

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,11 +16,11 @@
package org.springframework.test.context.testng.transaction.ejb;
import org.testng.annotations.Test;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,11 +16,11 @@
package org.springframework.test.context.testng.transaction.ejb;
import org.testng.annotations.Test;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.testng.annotations.Test;
/**
* Extension of {@link CommitForRequiresNewEjbTxDaoTestNGTests} which sets the default
* rollback semantics for the {@link TransactionalTestExecutionListener} to

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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,12 @@ package org.springframework.test.context.testng.transaction.programmatic;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.sql.DataSource;
import org.testng.IHookCallBack;
import org.testng.ITestResult;
import org.testng.annotations.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@@ -37,10 +40,6 @@ import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.testng.IHookCallBack;
import org.testng.ITestResult;
import org.testng.annotations.Test;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.BDDMockito;
import org.springframework.beans.BeanUtils;
@@ -59,9 +57,6 @@ public class TransactionalTestExecutionListenerTests {
private final TestContext testContext = mock(TestContext.class);
@Rule
public ExpectedException exception = ExpectedException.none();
@After
public void cleanUpThreadLocalStateForSubsequentTestClassesInSuite() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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,7 +19,6 @@ package org.springframework.test.context.transaction.programmatic;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.sql.DataSource;
import org.junit.Rule;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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,11 +16,9 @@
package org.springframework.test.context.web;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
/**
* Unit tests for {@link AnnotationConfigWebContextLoader}.
@@ -33,20 +31,16 @@ public class AnnotationConfigWebContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void configMustNotContainLocations() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support resource locations"));
AnnotationConfigWebContextLoader loader = new AnnotationConfigWebContextLoader();
WebMergedContextConfiguration mergedConfig = new WebMergedContextConfiguration(getClass(),
new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY,
EMPTY_STRING_ARRAY, "resource/path", loader, null, null);
loader.loadContext(mergedConfig);
assertThatIllegalStateException().isThrownBy(() ->
loader.loadContext(mergedConfig))
.withMessageContaining("does not support resource locations");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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,11 +16,9 @@
package org.springframework.test.context.web;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
/**
* Unit tests for {@link GenericXmlWebContextLoader}.
@@ -32,20 +30,16 @@ public class GenericXmlWebContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void configMustNotContainAnnotatedClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("does not support annotated classes"));
GenericXmlWebContextLoader loader = new GenericXmlWebContextLoader();
WebMergedContextConfiguration mergedConfig = new WebMergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
new Class<?>[] { getClass() }, null, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY, EMPTY_STRING_ARRAY,
"resource/path", loader, null, null);
loader.loadContext(mergedConfig);
assertThatIllegalStateException().isThrownBy(() ->
loader.loadContext(mergedConfig))
.withMessageContaining("does not support annotated classes");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -29,8 +29,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfigurationBootstrapWithTests.CustomWebTestContextBootstrapper;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* JUnit-based integration tests that verify support for loading a

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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,6 +20,7 @@ import javax.websocket.server.ServerContainer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -23,8 +23,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.BDDMockito.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2016 the original author or authors.
* Copyright 2004-2019 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,11 +16,10 @@
package org.springframework.test.util;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.core.Is.is;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.core.Is.*;
/**
* Unit tests for {@link JsonPathExpectationsHelper}.
@@ -51,9 +50,6 @@ public class JsonPathExpectationsHelperTests {
"{'name': 'Maggie'} " + //
" ] }";
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void exists() throws Exception {
@@ -78,9 +74,9 @@ public class JsonPathExpectationsHelperTests {
@Test
public void existsForIndefinatePathWithEmptyResults() throws Exception {
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
exception.expect(AssertionError.class);
exception.expectMessage("No value at JSON path \"" + expression + "\"");
new JsonPathExpectationsHelper(expression).exists(SIMPSONS);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).exists(SIMPSONS))
.withMessageContaining("No value at JSON path \"" + expression + "\"");
}
@Test
@@ -91,26 +87,25 @@ public class JsonPathExpectationsHelperTests {
@Test
public void doesNotExistForAnEmptyArray() throws Exception {
String expression = "$.emptyArray";
exception.expect(AssertionError.class);
exception.expectMessage("Expected no value at JSON path \"" + expression + "\" but found: []");
new JsonPathExpectationsHelper(expression).doesNotExist(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).doesNotExist(CONTENT))
.withMessageContaining("Expected no value at JSON path \"" + expression + "\" but found: []");
}
@Test
public void doesNotExistForAnEmptyMap() throws Exception {
String expression = "$.emptyMap";
exception.expect(AssertionError.class);
exception.expectMessage("Expected no value at JSON path \"" + expression + "\" but found: {}");
new JsonPathExpectationsHelper(expression).doesNotExist(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).doesNotExist(CONTENT))
.withMessageContaining("Expected no value at JSON path \"" + expression + "\" but found: {}");
}
@Test
public void doesNotExistForIndefinatePathWithResults() throws Exception {
String expression = "$.familyMembers[?(@.name == 'Bart')]";
exception.expect(AssertionError.class);
exception.expectMessage("Expected no value at JSON path \"" + expression
+ "\" but found: [{\"name\":\"Bart\"}]");
new JsonPathExpectationsHelper(expression).doesNotExist(SIMPSONS);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).doesNotExist(SIMPSONS))
.withMessageContaining("Expected no value at JSON path \"" + expression + "\" but found: [{\"name\":\"Bart\"}]");
}
@Test
@@ -141,18 +136,17 @@ public class JsonPathExpectationsHelperTests {
@Test
public void assertValueIsEmptyForIndefinatePathWithResults() throws Exception {
String expression = "$.familyMembers[?(@.name == 'Bart')]";
exception.expect(AssertionError.class);
exception.expectMessage("Expected an empty value at JSON path \"" + expression
+ "\" but found: [{\"name\":\"Bart\"}]");
new JsonPathExpectationsHelper(expression).assertValueIsEmpty(SIMPSONS);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsEmpty(SIMPSONS))
.withMessageContaining("Expected an empty value at JSON path \"" + expression + "\" but found: [{\"name\":\"Bart\"}]");
}
@Test
public void assertValueIsEmptyForWhitespace() throws Exception {
String expression = "$.whitespace";
exception.expect(AssertionError.class);
exception.expectMessage("Expected an empty value at JSON path \"" + expression + "\" but found: ' '");
new JsonPathExpectationsHelper(expression).assertValueIsEmpty(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsEmpty(CONTENT))
.withMessageContaining("Expected an empty value at JSON path \"" + expression + "\" but found: ' '");
}
@Test
@@ -188,33 +182,33 @@ public class JsonPathExpectationsHelperTests {
@Test
public void assertValueIsNotEmptyForIndefinatePathWithEmptyResults() throws Exception {
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
exception.expect(AssertionError.class);
exception.expectMessage("Expected a non-empty value at JSON path \"" + expression + "\" but found: []");
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(SIMPSONS);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(SIMPSONS))
.withMessageContaining("Expected a non-empty value at JSON path \"" + expression + "\" but found: []");
}
@Test
public void assertValueIsNotEmptyForAnEmptyString() throws Exception {
String expression = "$.emptyString";
exception.expect(AssertionError.class);
exception.expectMessage("Expected a non-empty value at JSON path \"" + expression + "\" but found: ''");
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT))
.withMessageContaining("Expected a non-empty value at JSON path \"" + expression + "\" but found: ''");
}
@Test
public void assertValueIsNotEmptyForAnEmptyArray() throws Exception {
String expression = "$.emptyArray";
exception.expect(AssertionError.class);
exception.expectMessage("Expected a non-empty value at JSON path \"" + expression + "\" but found: []");
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT))
.withMessageContaining("Expected a non-empty value at JSON path \"" + expression + "\" but found: []");
}
@Test
public void assertValueIsNotEmptyForAnEmptyMap() throws Exception {
String expression = "$.emptyMap";
exception.expect(AssertionError.class);
exception.expectMessage("Expected a non-empty value at JSON path \"" + expression + "\" but found: {}");
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT))
.withMessageContaining("Expected a non-empty value at JSON path \"" + expression + "\" but found: {}");
}
@Test
@@ -235,9 +229,9 @@ public class JsonPathExpectationsHelperTests {
@Test
public void hasJsonPathForIndefinatePathWithEmptyResults() {
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
exception.expect(AssertionError.class);
exception.expectMessage("No values for JSON path \"" + expression + "\"");
new JsonPathExpectationsHelper(expression).hasJsonPath(SIMPSONS);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).hasJsonPath(SIMPSONS))
.withMessageContaining("No values for JSON path \"" + expression + "\"");
}
@Test // SPR-16339
@@ -247,8 +241,8 @@ public class JsonPathExpectationsHelperTests {
@Test // SPR-16339
public void doesNotHaveJsonPathWithNull() {
exception.expect(AssertionError.class);
new JsonPathExpectationsHelper("$.abc").doesNotHaveJsonPath("{\"abc\": null}");
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper("$.abc").doesNotHaveJsonPath("{\"abc\": null}"));
}
@Test
@@ -259,10 +253,9 @@ public class JsonPathExpectationsHelperTests {
@Test
public void doesNotHaveEmptyPathForIndefinatePathWithResults() {
String expression = "$.familyMembers[?(@.name == 'Bart')]";
exception.expect(AssertionError.class);
exception.expectMessage("Expected no values at JSON path \"" + expression + "\" " +
"but found: [{\"name\":\"Bart\"}]");
new JsonPathExpectationsHelper(expression).doesNotHaveJsonPath(SIMPSONS);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).doesNotHaveJsonPath(SIMPSONS))
.withMessageContaining("Expected no values at JSON path \"" + expression + "\" " + "but found: [{\"name\":\"Bart\"}]");
}
@Test
@@ -293,9 +286,9 @@ public class JsonPathExpectationsHelperTests {
@Test
public void assertValueIsStringForNonString() throws Exception {
String expression = "$.bool";
exception.expect(AssertionError.class);
exception.expectMessage("Expected a string at JSON path \"" + expression + "\" but found: true");
new JsonPathExpectationsHelper(expression).assertValueIsString(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsString(CONTENT))
.withMessageContaining("Expected a string at JSON path \"" + expression + "\" but found: true");
}
@Test
@@ -306,9 +299,9 @@ public class JsonPathExpectationsHelperTests {
@Test
public void assertValueIsNumberForNonNumber() throws Exception {
String expression = "$.bool";
exception.expect(AssertionError.class);
exception.expectMessage("Expected a number at JSON path \"" + expression + "\" but found: true");
new JsonPathExpectationsHelper(expression).assertValueIsNumber(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsNumber(CONTENT))
.withMessageContaining("Expected a number at JSON path \"" + expression + "\" but found: true");
}
@Test
@@ -319,9 +312,9 @@ public class JsonPathExpectationsHelperTests {
@Test
public void assertValueIsBooleanForNonBoolean() throws Exception {
String expression = "$.num";
exception.expect(AssertionError.class);
exception.expectMessage("Expected a boolean at JSON path \"" + expression + "\" but found: 5");
new JsonPathExpectationsHelper(expression).assertValueIsBoolean(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsBoolean(CONTENT))
.withMessageContaining("Expected a boolean at JSON path \"" + expression + "\" but found: 5");
}
@Test
@@ -337,9 +330,9 @@ public class JsonPathExpectationsHelperTests {
@Test
public void assertValueIsArrayForNonArray() throws Exception {
String expression = "$.str";
exception.expect(AssertionError.class);
exception.expectMessage("Expected an array at JSON path \"" + expression + "\" but found: 'foo'");
new JsonPathExpectationsHelper(expression).assertValueIsArray(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsArray(CONTENT))
.withMessageContaining("Expected an array at JSON path \"" + expression + "\" but found: 'foo'");
}
@Test
@@ -355,9 +348,9 @@ public class JsonPathExpectationsHelperTests {
@Test
public void assertValueIsMapForNonMap() throws Exception {
String expression = "$.str";
exception.expect(AssertionError.class);
exception.expectMessage("Expected a map at JSON path \"" + expression + "\" but found: 'foo'");
new JsonPathExpectationsHelper(expression).assertValueIsMap(CONTENT);
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathExpectationsHelper(expression).assertValueIsMap(CONTENT))
.withMessageContaining("Expected a map at JSON path \"" + expression + "\" but found: 'foo'");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -30,6 +30,8 @@ import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 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.
@@ -23,6 +23,7 @@ import org.junit.Test;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import static org.junit.Assert.*;
import static org.springframework.test.util.MetaAnnotationUtils.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.test.util;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.AopUtils;
@@ -30,7 +28,7 @@ import org.springframework.test.util.subpackage.Person;
import org.springframework.test.util.subpackage.PersonEntity;
import org.springframework.test.util.subpackage.StaticFields;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
@@ -50,9 +48,6 @@ public class ReflectionTestUtilsTests {
private final LegacyEntity entity = new LegacyEntity();
@Rule
public final ExpectedException exception = ExpectedException.none();
@Before
public void resetStaticFields() {
@@ -61,51 +56,51 @@ public class ReflectionTestUtilsTests {
@Test
public void setFieldWithNullTargetObject() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(startsWith("Either targetObject or targetClass"));
setField((Object) null, "id", Long.valueOf(99));
assertThatIllegalArgumentException().isThrownBy(() ->
setField((Object) null, "id", Long.valueOf(99)))
.withMessageStartingWith("Either targetObject or targetClass");
}
@Test
public void getFieldWithNullTargetObject() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(startsWith("Either targetObject or targetClass"));
getField((Object) null, "id");
assertThatIllegalArgumentException().isThrownBy(() ->
getField((Object) null, "id"))
.withMessageStartingWith("Either targetObject or targetClass");
}
@Test
public void setFieldWithNullTargetClass() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(startsWith("Either targetObject or targetClass"));
setField((Class<?>) null, "id", Long.valueOf(99));
assertThatIllegalArgumentException().isThrownBy(() ->
setField((Class<?>) null, "id", Long.valueOf(99)))
.withMessageStartingWith("Either targetObject or targetClass");
}
@Test
public void getFieldWithNullTargetClass() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(startsWith("Either targetObject or targetClass"));
getField((Class<?>) null, "id");
assertThatIllegalArgumentException().isThrownBy(() ->
getField((Class<?>) null, "id"))
.withMessageStartingWith("Either targetObject or targetClass");
}
@Test
public void setFieldWithNullNameAndNullType() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(startsWith("Either name or type"));
setField(person, null, Long.valueOf(99), null);
assertThatIllegalArgumentException().isThrownBy(() ->
setField(person, null, Long.valueOf(99), null))
.withMessageStartingWith("Either name or type");
}
@Test
public void setFieldWithBogusName() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(startsWith("Could not find field 'bogus'"));
setField(person, "bogus", Long.valueOf(99), long.class);
assertThatIllegalArgumentException().isThrownBy(() ->
setField(person, "bogus", Long.valueOf(99), long.class))
.withMessageStartingWith("Could not find field 'bogus'");
}
@Test
public void setFieldWithWrongType() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(startsWith("Could not find field"));
setField(person, "id", Long.valueOf(99), String.class);
assertThatIllegalArgumentException().isThrownBy(() ->
setField(person, "id", Long.valueOf(99), String.class))
.withMessageStartingWith("Could not find field");
}
@Test
@@ -363,30 +358,30 @@ public class ReflectionTestUtilsTests {
@Test
public void invokeInitMethodBeforeAutowiring() {
exception.expect(IllegalStateException.class);
exception.expectMessage(equalTo("number must not be null"));
invokeMethod(component, "init");
assertThatIllegalStateException().isThrownBy(() ->
invokeMethod(component, "init"))
.withMessageStartingWith("number must not be null");
}
@Test
public void invokeMethodWithIncompatibleArgumentTypes() {
exception.expect(IllegalStateException.class);
exception.expectMessage(startsWith("Method not found"));
invokeMethod(component, "subtract", "foo", 2.0);
assertThatIllegalStateException().isThrownBy(() ->
invokeMethod(component, "subtract", "foo", 2.0))
.withMessageStartingWith("Method not found");
}
@Test
public void invokeMethodWithTooFewArguments() {
exception.expect(IllegalStateException.class);
exception.expectMessage(startsWith("Method not found"));
invokeMethod(component, "configure", Integer.valueOf(42));
assertThatIllegalStateException().isThrownBy(() ->
invokeMethod(component, "configure", Integer.valueOf(42)))
.withMessageStartingWith("Method not found");
}
@Test
public void invokeMethodWithTooManyArguments() {
exception.expect(IllegalStateException.class);
exception.expectMessage(startsWith("Method not found"));
invokeMethod(component, "configure", Integer.valueOf(42), "enigma", "baz", "quux");
assertThatIllegalStateException().isThrownBy(() ->
invokeMethod(component, "configure", Integer.valueOf(42), "enigma", "baz", "quux"))
.withMessageStartingWith("Method not found");
}
@Test // SPR-14363

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,10 +16,9 @@
package org.springframework.test.util;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.*;
/**
* Unit tests for {@link XmlExpectationsHelper}.
@@ -28,62 +27,52 @@ import org.junit.rules.ExpectedException;
*/
public class XmlExpectationsHelperTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void assertXmlEqualForEqual() throws Exception {
final String control = "<root><field1>f1</field1><field2>f2</field2></root>";
final String test = "<root><field1>f1</field1><field2>f2</field2></root>";
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field1>f1</field1><field2>f2</field2></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
xmlHelper.assertXmlEqual(control, test);
}
@Test
public void assertXmlEqualExceptionForIncorrectValue() throws Exception {
final String control = "<root><field1>f1</field1><field2>f2</field2></root>";
final String test = "<root><field1>notf1</field1><field2>f2</field2></root>";
exception.expect(AssertionError.class);
exception.expectMessage(Matchers.startsWith("Body content Expected child 'field1'"));
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
xmlHelper.assertXmlEqual(control, test);
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field1>notf1</field1><field2>f2</field2></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
xmlHelper.assertXmlEqual(control, test))
.withMessageStartingWith("Body content Expected child 'field1'");
}
@Test
public void assertXmlEqualForOutOfOrder() throws Exception {
final String control = "<root><field1>f1</field1><field2>f2</field2></root>";
final String test = "<root><field2>f2</field2><field1>f1</field1></root>";
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field2>f2</field2><field1>f1</field1></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
xmlHelper.assertXmlEqual(control, test);
}
@Test
public void assertXmlEqualExceptionForMoreEntries() throws Exception {
final String control = "<root><field1>f1</field1><field2>f2</field2></root>";
final String test = "<root><field1>f1</field1><field2>f2</field2><field3>f3</field3></root>";
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field1>f1</field1><field2>f2</field2><field3>f3</field3></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
xmlHelper.assertXmlEqual(control, test))
.withMessageContaining("Expected child nodelist length '2' but was '3'");
exception.expect(AssertionError.class);
exception.expectMessage(Matchers.containsString("Expected child nodelist length '2' but was '3'"));
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
xmlHelper.assertXmlEqual(control, test);
}
@Test
public void assertXmlEqualExceptionForLessEntries() throws Exception {
final String control = "<root><field1>f1</field1><field2>f2</field2><field3>f3</field3></root>";
final String test = "<root><field1>f1</field1><field2>f2</field2></root>";
exception.expect(AssertionError.class);
exception.expectMessage(Matchers.containsString("Expected child nodelist length '3' but was '2'"));
final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
xmlHelper.assertXmlEqual(control, test);
String control = "<root><field1>f1</field1><field2>f2</field2><field3>f3</field3></root>";
String test = "<root><field1>f1</field1><field2>f2</field2></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
xmlHelper.assertXmlEqual(control, test))
.withMessageContaining("Expected child nodelist length '3' but was '2'");
}
}

View File

@@ -19,13 +19,12 @@ package org.springframework.test.web.client;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.test.web.client.ExpectedCount.*;
@@ -38,9 +37,6 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
*/
public class DefaultRequestExpectationTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void match() throws Exception {
@@ -52,9 +48,9 @@ public class DefaultRequestExpectationTests {
public void matchWithFailedExpectation() throws Exception {
RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo"));
expectation.andExpect(method(POST));
this.thrown.expectMessage("Unexpected HttpMethod expected:<POST> but was:<GET>");
expectation.match(createRequest(GET, "/foo"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
expectation.match(createRequest(GET, "/foo")))
.withMessageContaining("Unexpected HttpMethod expected:<POST> but was:<GET>");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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,14 +20,13 @@ import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.mock.http.client.MockClientHttpRequest;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.test.web.client.ExpectedCount.*;
@@ -43,9 +42,6 @@ public class SimpleRequestExpectationManagerTests {
private final SimpleRequestExpectationManager manager = new SimpleRequestExpectationManager();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void unexpectedRequest() throws Exception {
@@ -77,27 +73,25 @@ public class SimpleRequestExpectationManagerTests {
public void sequentialRequestsTooMany() throws Exception {
this.manager.expectRequest(max(1), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(max(1), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.thrown.expectMessage("No further requests expected: HTTP GET /baz\n" +
"2 request(s) executed:\n" +
"GET /foo\n" +
"GET /bar\n");
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/baz"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.manager.validateRequest(createRequest(GET, "/baz")))
.withMessage("No further requests expected: HTTP GET /baz\n" +
"2 request(s) executed:\n" +
"GET /foo\n" +
"GET /bar\n");
}
@Test
public void sequentialRequestsTooFew() throws Exception {
this.manager.expectRequest(min(1), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(min(1), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.thrown.expectMessage("Further request(s) expected leaving 1 unsatisfied expectation(s).\n" +
"1 request(s) executed:\nGET /foo\n");
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.verify();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.manager.verify())
.withMessage("Further request(s) expected leaving 1 unsatisfied expectation(s).\n" +
"1 request(s) executed:\nGET /foo\n");
}
@Test
@@ -118,35 +112,33 @@ public class SimpleRequestExpectationManagerTests {
public void repeatedRequestsTooMany() throws Exception {
this.manager.expectRequest(max(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(max(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.thrown.expectMessage("No further requests expected: HTTP GET /foo\n" +
"4 request(s) executed:\n" +
"GET /foo\n" +
"GET /bar\n" +
"GET /foo\n" +
"GET /bar\n");
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.manager.validateRequest(createRequest(GET, "/foo")))
.withMessage("No further requests expected: HTTP GET /foo\n" +
"4 request(s) executed:\n" +
"GET /foo\n" +
"GET /bar\n" +
"GET /foo\n" +
"GET /bar\n");
}
@Test
public void repeatedRequestsTooFew() throws Exception {
this.manager.expectRequest(min(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(min(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.thrown.expectMessage("3 request(s) executed:\n" +
"GET /foo\n" +
"GET /bar\n" +
"GET /foo\n");
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.verify();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.manager.verify())
.withMessageContaining("3 request(s) executed:\n" +
"GET /foo\n" +
"GET /bar\n" +
"GET /foo\n");
}
@Test
@@ -154,9 +146,9 @@ public class SimpleRequestExpectationManagerTests {
this.manager.expectRequest(twice(), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(twice(), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(twice(), requestTo("/baz")).andExpect(method(GET)).andRespond(withSuccess());
this.thrown.expectMessage("Unexpected HttpMethod expected:<GET> but was:<POST>");
this.manager.validateRequest(createRequest(POST, "/foo"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.manager.validateRequest(createRequest(POST, "/foo")))
.withMessage("Unexpected HttpMethod expected:<GET> but was:<POST>");
}
@Test // SPR-15672
@@ -186,14 +178,8 @@ public class SimpleRequestExpectationManagerTests {
andExpect(method(GET)).andRespond(request -> { throw new SocketException("pseudo network error"); });
this.manager.expectRequest(once(), requestTo("/handle-error")).
andExpect(method(POST)).andRespond(withSuccess());
try {
this.manager.validateRequest(createRequest(GET, "/foo"));
fail("Expected SocketException");
}
catch (SocketException ex) {
// expected
}
assertThatExceptionOfType(SocketException.class).isThrownBy(() ->
this.manager.validateRequest(createRequest(GET, "/foo")));
this.manager.validateRequest(createRequest(POST, "/handle-error"));
this.manager.verify();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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,13 +19,12 @@ package org.springframework.test.web.client;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.springframework.http.HttpMethod.*;
import static org.springframework.test.web.client.ExpectedCount.*;
@@ -41,9 +40,6 @@ public class UnorderedRequestExpectationManagerTests {
private UnorderedRequestExpectationManager manager = new UnorderedRequestExpectationManager();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void unexpectedRequest() throws Exception {
@@ -87,35 +83,33 @@ public class UnorderedRequestExpectationManagerTests {
public void repeatedRequestsTooMany() throws Exception {
this.manager.expectRequest(max(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(max(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.thrown.expectMessage("No further requests expected: HTTP GET /foo\n" +
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.manager.validateRequest(createRequest(GET, "/foo")))
.withMessage("No further requests expected: HTTP GET /foo\n" +
"4 request(s) executed:\n" +
"GET /bar\n" +
"GET /foo\n" +
"GET /bar\n" +
"GET /foo\n");
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/foo"));
}
@Test
public void repeatedRequestsTooFew() throws Exception {
this.manager.expectRequest(min(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(min(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.thrown.expectMessage("3 request(s) executed:\n" +
"GET /bar\n" +
"GET /foo\n" +
"GET /foo\n");
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.verify();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.manager.verify())
.withMessageContaining("3 request(s) executed:\n" +
"GET /bar\n" +
"GET /foo\n" +
"GET /foo\n");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -25,9 +25,9 @@ import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.mock.http.client.MockClientHttpRequest;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
/**
* Unit tests for {@link MockRestRequestMatchers}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -40,8 +40,8 @@ import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Tests that use a {@link RestTemplate} configured with a

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -28,7 +28,7 @@ import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.util.concurrent.ListenableFuture;
import static org.junit.Assert.*;
import static org.springframework.test.web.client.ExpectedCount.manyTimes;
import static org.springframework.test.web.client.ExpectedCount.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -35,12 +35,9 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.*;
import static org.springframework.test.web.client.ExpectedCount.manyTimes;
import static org.springframework.test.web.client.ExpectedCount.never;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import static org.springframework.test.web.client.ExpectedCount.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
/**
* Examples to demonstrate writing client-side REST tests with Spring MVC Test.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -40,8 +40,8 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static java.nio.charset.StandardCharsets.*;
import static org.junit.Assert.*;
/**
* Unit tests for {@link HttpHandlerConnector}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -33,7 +33,7 @@ import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import org.springframework.web.reactive.function.client.ExchangeFunctions;
import static java.time.Duration.ofMillis;
import static java.time.Duration.*;
import static org.junit.Assert.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -107,9 +107,9 @@ public class ResponseEntityTests {
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBodyList(Person.class).value(people -> {
MatcherAssert.assertThat(people, hasItem(new Person("Jason")));
});
.expectBodyList(Person.class).value(people ->
MatcherAssert.assertThat(people, hasItem(new Person("Jason")))
);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -167,4 +167,4 @@ public class XmlContentTests {
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -23,8 +23,8 @@ import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
/**
* Sample tests demonstrating "mock" server tests binding to a RouterFunction.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
@@ -34,6 +34,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.hamcrest.CoreMatchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -41,7 +41,7 @@ import org.springframework.tests.TestGroup;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.IsNot.not;
import static org.mockito.Mockito.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -45,16 +45,10 @@ import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static java.util.Arrays.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
/**
* Unit tests for {@link HtmlUnitRequestBuilder}.

View File

@@ -24,7 +24,6 @@ import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,10 +42,9 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
/**
* Integration tests for {@link MockMvcWebConnectionBuilderSupport}.

View File

@@ -49,7 +49,7 @@ import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
/**
* Integration tests for {@link MockMvcWebClientBuilder}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -30,7 +30,7 @@ import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
/**
* Tests for {@link MockWebResponseBuilder}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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,17 +21,18 @@ import java.io.IOException;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openqa.selenium.WebDriverException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
/**
@@ -49,9 +50,6 @@ public class WebConnectionHtmlUnitDriverTests {
@Mock
private WebConnection connection;
@Rule
public ExpectedException exception = ExpectedException.none();
@Before
public void setup() throws Exception {
when(this.connection.getResponse(any(WebRequest.class))).thenThrow(new IOException(""));
@@ -65,17 +63,16 @@ public class WebConnectionHtmlUnitDriverTests {
@Test
public void setWebConnectionToNull() {
this.exception.expect(IllegalArgumentException.class);
this.driver.setWebConnection(null);
assertThatIllegalArgumentException().isThrownBy(() ->
this.driver.setWebConnection(null));
}
@Test
public void setWebConnection() {
this.driver.setWebConnection(this.connection);
assertThat(this.driver.getWebConnection(), equalTo(this.connection));
this.exception.expect(WebDriverException.class);
this.driver.get("https://example.com");
assertThatExceptionOfType(WebDriverException.class).isThrownBy(() ->
this.driver.get("https://example.com"));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,7 +17,6 @@
package org.springframework.test.web.servlet.result;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -44,12 +44,10 @@ import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.mockito.ArgumentMatchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Tests with Java configuration.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
@@ -46,15 +46,11 @@ import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Tests with Java configuration.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -30,12 +30,9 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
import static org.hamcrest.Matchers.containsString;
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.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.handler;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Tests dependent on access to resources under the web application root directory.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 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.
@@ -41,13 +41,11 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**
* Integration tests for SPR-13211 which verify that a custom mock request

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 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.
@@ -47,11 +47,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.util.UriComponentsBuilder;
import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import static org.hamcrest.core.Is.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**
* Tests for SPR-11441 (MockMvc accepts an already encoded URI).

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 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.
@@ -37,10 +37,10 @@ import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**
* Tests for SPR-10093 (support for OPTIONS requests).

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 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.
@@ -30,7 +30,7 @@ import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
/**
* Test for SPR-10277 (multiple method chaining when building MockMvc).

View File

@@ -44,6 +44,7 @@ import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -49,17 +49,10 @@ import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**
* Tests with {@link Filter}'s.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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,7 +17,6 @@
package org.springframework.test.web.servlet.samples.standalone.resulthandlers;
import java.io.StringWriter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.test.web.servlet.samples.standalone.resultmatchers;
import java.lang.reflect.Method;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;
@@ -28,14 +26,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.handler;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;
/**
* Examples of expectations on the controller type and controller method.
@@ -47,9 +43,6 @@ public class HandlerAssertionTests {
private final MockMvc mockMvc = standaloneSetup(new SimpleController()).alwaysExpect(status().isOk()).build();
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void handlerType() throws Exception {
@@ -58,12 +51,11 @@ public class HandlerAssertionTests {
@Test
public void methodCallOnNonMock() throws Exception {
exception.expect(AssertionError.class);
exception.expectMessage("The supplied object [bogus] is not an instance of");
exception.expectMessage(MvcUriComponentsBuilder.MethodInvocationInfo.class.getName());
exception.expectMessage("Ensure that you invoke the handler method via MvcUriComponentsBuilder.on()");
this.mockMvc.perform(get("/")).andExpect(handler().methodCall("bogus"));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.mockMvc.perform(get("/")).andExpect(handler().methodCall("bogus")))
.withMessageContaining("The supplied object [bogus] is not an instance of")
.withMessageContaining(MvcUriComponentsBuilder.MethodInvocationInfo.class.getName())
.withMessageContaining("Ensure that you invoke the handler method via MvcUriComponentsBuilder.on()");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2019 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.
@@ -30,8 +30,8 @@ import org.springframework.web.bind.annotation.SessionAttributes;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.test.web.servlet.setup;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.support.StaticApplicationContext;
@@ -29,7 +27,7 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
@@ -45,22 +43,18 @@ public class DefaultMockMvcBuilderTests {
private final MockServletContext servletContext = new MockServletContext();
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void webAppContextSetupWithNullWac() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(equalTo("WebApplicationContext is required"));
webAppContextSetup(null);
assertThatIllegalArgumentException().isThrownBy(() ->
webAppContextSetup(null))
.withMessage("WebApplicationContext is required");
}
@Test
public void webAppContextSetupWithNullServletContext() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(equalTo("WebApplicationContext must have a ServletContext"));
webAppContextSetup(new StubWebApplicationContext(null));
assertThatIllegalArgumentException().isThrownBy(() ->
webAppContextSetup(new StubWebApplicationContext(null)))
.withMessage("WebApplicationContext must have a ServletContext");
}
/**