Move tests to JUnit 5 wherever possible
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport;
|
||||
|
||||
import org.junit.AssumptionViolatedException;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Provides utility methods that allow JUnit tests to {@link org.junit.Assume} certain
|
||||
* conditions hold {@code true}. If the assumption fails, it means the test should be
|
||||
* skipped.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public abstract class Assume {
|
||||
|
||||
public static void javaEight() {
|
||||
if (ClassUtils.isPresent("java.security.cert.URICertStoreParameters", null)) {
|
||||
throw new AssumptionViolatedException("Assumed Java 8 but got Java 9");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,8 +29,6 @@ import javax.tools.StandardJavaFileManager;
|
||||
import javax.tools.StandardLocation;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
/**
|
||||
* Wrapper to make the {@link JavaCompiler} easier to use in tests.
|
||||
*
|
||||
@@ -51,14 +49,15 @@ public class TestCompiler {
|
||||
|
||||
private final File outputLocation;
|
||||
|
||||
public TestCompiler(TemporaryFolder temporaryFolder) throws IOException {
|
||||
this(ToolProvider.getSystemJavaCompiler(), temporaryFolder);
|
||||
public TestCompiler(File outputLocation) throws IOException {
|
||||
this(ToolProvider.getSystemJavaCompiler(), outputLocation);
|
||||
}
|
||||
|
||||
public TestCompiler(JavaCompiler compiler, TemporaryFolder temporaryFolder) throws IOException {
|
||||
public TestCompiler(JavaCompiler compiler, File outputLocation) throws IOException {
|
||||
this.compiler = compiler;
|
||||
this.fileManager = compiler.getStandardFileManager(null, null, null);
|
||||
this.outputLocation = temporaryFolder.newFolder();
|
||||
this.outputLocation = outputLocation;
|
||||
this.outputLocation.mkdirs();
|
||||
Iterable<? extends File> temp = Arrays.asList(this.outputLocation);
|
||||
this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, temp);
|
||||
this.fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, temp);
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.asm.Opcodes;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.rule;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Assert;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
|
||||
/**
|
||||
* Internal JUnit {@code @Rule} to capture output from System.out and System.err.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class OutputCapture implements TestRule {
|
||||
|
||||
private CaptureOutputStream captureOut;
|
||||
|
||||
private CaptureOutputStream captureErr;
|
||||
|
||||
private ByteArrayOutputStream copy;
|
||||
|
||||
private List<Matcher<? super String>> matchers = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
captureOutput();
|
||||
try {
|
||||
base.evaluate();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (!OutputCapture.this.matchers.isEmpty()) {
|
||||
String output = OutputCapture.this.toString();
|
||||
Assert.assertThat(output, allOf(OutputCapture.this.matchers));
|
||||
}
|
||||
}
|
||||
finally {
|
||||
releaseOutput();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected void captureOutput() {
|
||||
// FIXME AnsiOutput.setEnabled(Enabled.NEVER);
|
||||
this.copy = new ByteArrayOutputStream();
|
||||
this.captureOut = new CaptureOutputStream(System.out, this.copy);
|
||||
this.captureErr = new CaptureOutputStream(System.err, this.copy);
|
||||
System.setOut(new PrintStream(this.captureOut));
|
||||
System.setErr(new PrintStream(this.captureErr));
|
||||
}
|
||||
|
||||
protected void releaseOutput() {
|
||||
// FIXME AnsiOutput.setEnabled(Enabled.DETECT);
|
||||
System.setOut(this.captureOut.getOriginal());
|
||||
System.setErr(this.captureErr.getOriginal());
|
||||
this.copy = null;
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
try {
|
||||
this.captureOut.flush();
|
||||
this.captureErr.flush();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
flush();
|
||||
return this.copy.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the output is matched by the supplied {@code matcher}. Verification is
|
||||
* performed after the test method has executed.
|
||||
* @param matcher the matcher
|
||||
*/
|
||||
public void expect(Matcher<? super String> matcher) {
|
||||
this.matchers.add(matcher);
|
||||
}
|
||||
|
||||
private static class CaptureOutputStream extends OutputStream {
|
||||
|
||||
private final PrintStream original;
|
||||
|
||||
private final OutputStream copy;
|
||||
|
||||
CaptureOutputStream(PrintStream original, OutputStream copy) {
|
||||
this.original = original;
|
||||
this.copy = copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
this.copy.write(b);
|
||||
this.original.write(b);
|
||||
this.original.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
write(b, 0, b.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
this.copy.write(b, off, len);
|
||||
this.original.write(b, off, len);
|
||||
}
|
||||
|
||||
public PrintStream getOriginal() {
|
||||
return this.original;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
this.copy.flush();
|
||||
this.original.flush();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Internal JUnit rules used in Spring Boot tests.
|
||||
*/
|
||||
package org.springframework.boot.testsupport.rule;
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.system;
|
||||
|
||||
/**
|
||||
* Provides access to {@link System#out System.out} and {@link System#err System.err}
|
||||
* output that has been capture by the {@link OutputCaptureExtension}. Can be used to
|
||||
* apply assertions either using AssertJ or standard JUnit assertions. For example:
|
||||
* <pre class="code">
|
||||
* assertThat(output).contains("started"); // Checks all output
|
||||
* assertThat(output.getErr()).contains("failed"); // Only checks System.err
|
||||
* assertThat(output.getOut()).contains("ok"); // Only checks System.put
|
||||
* </pre>
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 2.2.0
|
||||
* @see OutputCaptureExtension
|
||||
*/
|
||||
public interface CapturedOutput extends CharSequence {
|
||||
|
||||
@Override
|
||||
default int length() {
|
||||
return toString().length();
|
||||
}
|
||||
|
||||
@Override
|
||||
default char charAt(int index) {
|
||||
return toString().charAt(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
default CharSequence subSequence(int start, int end) {
|
||||
return toString().subSequence(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all content (both {@link System#out System.out} and {@link System#err
|
||||
* System.err}) in the order that it was was captured.
|
||||
* @return all captured output
|
||||
*/
|
||||
String getAll();
|
||||
|
||||
/**
|
||||
* Return {@link System#out System.out} content in the order that it was was captured.
|
||||
* @return {@link System#out System.out} captured output
|
||||
*/
|
||||
String getOut();
|
||||
|
||||
/**
|
||||
* Return {@link System#err System.err} content in the order that it was was captured.
|
||||
* @return {@link System#err System.err} captured output
|
||||
*/
|
||||
String getErr();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.system;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Provides support for capturing {@link System#out System.out} and {@link System#err
|
||||
* System.err}.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @see OutputCaptureExtension
|
||||
* @see OutputCaptureRule
|
||||
*/
|
||||
class OutputCapture implements CapturedOutput {
|
||||
|
||||
private final Deque<SystemCapture> systemCaptures = new ArrayDeque<>();
|
||||
|
||||
/**
|
||||
* Push a new system capture session onto the stack.
|
||||
*/
|
||||
final void push() {
|
||||
this.systemCaptures.addLast(new SystemCapture());
|
||||
}
|
||||
|
||||
/**
|
||||
* Pop the last system capture session from the stack.
|
||||
*/
|
||||
final void pop() {
|
||||
this.systemCaptures.removeLast().release();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof CapturedOutput || obj instanceof CharSequence) {
|
||||
return getAll().equals(obj.toString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return toString().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all content (both {@link System#out System.out} and {@link System#err
|
||||
* System.err}) in the order that it was was captured.
|
||||
* @return all captured output
|
||||
*/
|
||||
@Override
|
||||
public String getAll() {
|
||||
return get((type) -> true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@link System#out System.out} content in the order that it was was captured.
|
||||
* @return {@link System#out System.out} captured output
|
||||
*/
|
||||
@Override
|
||||
public String getOut() {
|
||||
return get(Type.OUT::equals);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@link System#err System.err} content in the order that it was was captured.
|
||||
* @return {@link System#err System.err} captured output
|
||||
*/
|
||||
@Override
|
||||
public String getErr() {
|
||||
return get(Type.ERR::equals);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the current capture session, clearing its captured output.
|
||||
*/
|
||||
void reset() {
|
||||
this.systemCaptures.peek().reset();
|
||||
}
|
||||
|
||||
private String get(Predicate<Type> filter) {
|
||||
Assert.state(!this.systemCaptures.isEmpty(),
|
||||
"No system captures found. Check that you have used @RegisterExtension "
|
||||
+ "or @ExtendWith and the fields are not private");
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (SystemCapture systemCapture : this.systemCaptures) {
|
||||
systemCapture.append(builder, filter);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A capture session that captures {@link System#out System.out} and {@link System#out
|
||||
* System.err}.
|
||||
*/
|
||||
private static class SystemCapture {
|
||||
|
||||
private final PrintStreamCapture out;
|
||||
|
||||
private final PrintStreamCapture err;
|
||||
|
||||
private final Object monitor = new Object();
|
||||
|
||||
private final List<CapturedString> capturedStrings = new ArrayList<>();
|
||||
|
||||
SystemCapture() {
|
||||
this.out = new PrintStreamCapture(System.out, this::captureOut);
|
||||
this.err = new PrintStreamCapture(System.err, this::captureErr);
|
||||
System.setOut(this.out);
|
||||
System.setErr(this.err);
|
||||
}
|
||||
|
||||
public void release() {
|
||||
System.setOut(this.out.getParent());
|
||||
System.setErr(this.err.getParent());
|
||||
}
|
||||
|
||||
private void captureOut(String string) {
|
||||
synchronized (this.monitor) {
|
||||
this.capturedStrings.add(new CapturedString(Type.OUT, string));
|
||||
}
|
||||
}
|
||||
|
||||
private void captureErr(String string) {
|
||||
synchronized (this.monitor) {
|
||||
this.capturedStrings.add(new CapturedString(Type.ERR, string));
|
||||
}
|
||||
}
|
||||
|
||||
public void append(StringBuilder builder, Predicate<Type> filter) {
|
||||
synchronized (this.monitor) {
|
||||
for (CapturedString stringCapture : this.capturedStrings) {
|
||||
if (filter.test(stringCapture.getType())) {
|
||||
builder.append(stringCapture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
synchronized (this.monitor) {
|
||||
this.capturedStrings.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link PrintStream} implementation that captures written strings.
|
||||
*/
|
||||
private static class PrintStreamCapture extends PrintStream {
|
||||
|
||||
private final PrintStream parent;
|
||||
|
||||
PrintStreamCapture(PrintStream parent, Consumer<String> copy) {
|
||||
super(new OutputStreamCapture(getSystemStream(parent), copy));
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public PrintStream getParent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
private static PrintStream getSystemStream(PrintStream printStream) {
|
||||
while (printStream instanceof PrintStreamCapture) {
|
||||
return ((PrintStreamCapture) printStream).getParent();
|
||||
}
|
||||
return printStream;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* An {@link OutputStream} implementation that captures written strings.
|
||||
*/
|
||||
private static class OutputStreamCapture extends OutputStream {
|
||||
|
||||
private final PrintStream systemStream;
|
||||
|
||||
private final Consumer<String> copy;
|
||||
|
||||
OutputStreamCapture(PrintStream systemStream, Consumer<String> copy) {
|
||||
this.systemStream = systemStream;
|
||||
this.copy = copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
write(new byte[] { (byte) (b & 0xFF) });
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
this.copy.accept(new String(b, off, len));
|
||||
this.systemStream.write(b, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
this.systemStream.flush();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A captured string that forms part of the full output.
|
||||
*/
|
||||
private static class CapturedString {
|
||||
|
||||
private final Type type;
|
||||
|
||||
private final String string;
|
||||
|
||||
CapturedString(Type type, String string) {
|
||||
this.type = type;
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.string;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Types of content that can be captured.
|
||||
*/
|
||||
private enum Type {
|
||||
|
||||
OUT, ERR
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.system;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ParameterContext;
|
||||
import org.junit.jupiter.api.extension.ParameterResolutionException;
|
||||
import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
|
||||
/**
|
||||
* Internal JUnit 5 {@code @Extension} to capture {@link System#out System.out} and
|
||||
* {@link System#err System.err}. Can be used on a test class via
|
||||
* {@link ExtendWith @ExtendWith}. This extension provides {@link ParameterResolver
|
||||
* parameter resolution} for a {@link CapturedOutput} instance which can be used to assert
|
||||
* that the correct output was written.
|
||||
* <p>
|
||||
* To use with {@link ExtendWith @ExtendWith}, inject the {@link CapturedOutput} as an
|
||||
* argument to your test class constructor or test method:
|
||||
*
|
||||
* <pre class="code">
|
||||
* @ExtendWith(OutputExtension.class)
|
||||
* class MyTest {
|
||||
*
|
||||
* @Test
|
||||
* void test(CapturedOutput output) {
|
||||
* assertThat(output).contains("ok");
|
||||
* }
|
||||
*
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 2.2.0
|
||||
* @see CapturedOutput
|
||||
*/
|
||||
public class OutputCaptureExtension
|
||||
implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver {
|
||||
|
||||
private final OutputCapture outputCapture = new OutputCapture();
|
||||
|
||||
OutputCaptureExtension() {
|
||||
// Package private to prevent users from directly creating an instance.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeAll(ExtensionContext context) throws Exception {
|
||||
this.outputCapture.push();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterAll(ExtensionContext context) throws Exception {
|
||||
this.outputCapture.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext context) throws Exception {
|
||||
this.outputCapture.push();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterEach(ExtensionContext context) throws Exception {
|
||||
this.outputCapture.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
|
||||
throws ParameterResolutionException {
|
||||
return CapturedOutput.class.equals(parameterContext.getParameter().getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
|
||||
throws ParameterResolutionException {
|
||||
return this.outputCapture;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.system;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Assert;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
|
||||
/**
|
||||
* Internal JUnit {@code @Rule} to capture output from System.out and System.err.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class OutputCaptureRule implements TestRule {
|
||||
|
||||
private final OutputCapture delegate = new OutputCapture();
|
||||
|
||||
private List<Matcher<? super String>> matchers = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
OutputCaptureRule.this.delegate.push();
|
||||
try {
|
||||
base.evaluate();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (!OutputCaptureRule.this.matchers.isEmpty()) {
|
||||
String output = OutputCaptureRule.this.delegate.toString();
|
||||
Assert.assertThat(output, allOf(OutputCaptureRule.this.matchers));
|
||||
}
|
||||
}
|
||||
finally {
|
||||
OutputCaptureRule.this.delegate.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.delegate.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the output is matched by the supplied {@code matcher}. Verification is
|
||||
* performed after the test method has executed.
|
||||
* @param matcher the matcher
|
||||
*/
|
||||
public void expect(Matcher<? super String> matcher) {
|
||||
this.matchers.add(matcher);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -15,6 +15,6 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Support for integration testing with Testcontainers.
|
||||
* Classes for {@link java.lang.System System}-related testing.
|
||||
*/
|
||||
package org.springframework.boot.testsupport.testcontainers;
|
||||
package org.springframework.boot.testsupport.system;
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.testcontainers;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.testcontainers.DockerClientFactory;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.lifecycle.Startable;
|
||||
|
||||
/**
|
||||
* {@link TestRule} for working with an optional Docker environment. Spins up a
|
||||
* {@link GenericContainer} if a valid docker environment is found.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class Container implements Startable {
|
||||
|
||||
private final int port;
|
||||
|
||||
private final Supplier<GenericContainer<?>> containerFactory;
|
||||
|
||||
private GenericContainer<?> container;
|
||||
|
||||
<T extends GenericContainer<T>> Container(String dockerImageName, int port) {
|
||||
this(dockerImageName, port, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "resource" })
|
||||
<T extends GenericContainer<T>> Container(String dockerImageName, int port, Consumer<T> customizer) {
|
||||
this.port = port;
|
||||
this.containerFactory = () -> {
|
||||
T container = (T) new GenericContainer<>(dockerImageName).withExposedPorts(port);
|
||||
if (customizer != null) {
|
||||
customizer.accept(container);
|
||||
}
|
||||
return container;
|
||||
};
|
||||
}
|
||||
|
||||
public int getMappedPort() {
|
||||
return this.container.getMappedPort(this.port);
|
||||
}
|
||||
|
||||
protected GenericContainer<?> getContainer() {
|
||||
return this.container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
Assumptions.assumeTrue(isDockerRunning(), "Could not find valid docker environment.");
|
||||
this.container = this.containerFactory.get();
|
||||
this.container.start();
|
||||
}
|
||||
|
||||
private boolean isDockerRunning() {
|
||||
try {
|
||||
DockerClientFactory.instance().client();
|
||||
return true;
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (this.container != null) {
|
||||
this.container.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.testcontainers;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
/**
|
||||
* Customization of {@link Testcontainers @Testcontainers} that disables the tests when
|
||||
* Docker is not available.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@ExtendWith(DockerIsAvailableCondition.class)
|
||||
@Testcontainers
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface DisabledWithoutDockerTestcontainers {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.testcontainers;
|
||||
|
||||
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
|
||||
import org.junit.jupiter.api.extension.ExecutionCondition;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.testcontainers.DockerClientFactory;
|
||||
|
||||
/**
|
||||
* {@link ExecutionCondition} for
|
||||
* {@link DisabledWithoutDockerTestcontainers @DisabledWithoutDockerTestcontainers}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
final class DockerIsAvailableCondition implements ExecutionCondition {
|
||||
|
||||
@Override
|
||||
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
|
||||
try {
|
||||
DockerClientFactory.instance().client();
|
||||
return ConditionEvaluationResult.enabled("Docker is available");
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
return ConditionEvaluationResult.disabled("Docker is not available: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.testcontainers;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* A {@link Container} for Elasticsearch.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ElasticsearchContainer extends Container {
|
||||
|
||||
public ElasticsearchContainer() {
|
||||
super("elasticsearch:6.7.2", 9200, (container) -> container.withStartupTimeout(Duration.ofSeconds(120))
|
||||
.withStartupAttempts(5).withEnv("discovery.type", "single-node").addExposedPorts(9200, 9300));
|
||||
}
|
||||
|
||||
public int getMappedTransportPort() {
|
||||
return getContainer().getMappedPort(9300);
|
||||
}
|
||||
|
||||
public int getMappedHttpPort() {
|
||||
return getContainer().getMappedPort(9200);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
System.setProperty("es.set.netty.runtime.available.processors", "false");
|
||||
super.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
System.clearProperty("es.set.netty.runtime.available.processors");
|
||||
super.stop();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,10 +24,11 @@ import org.testcontainers.containers.GenericContainer;
|
||||
* @author Andy Wilkinson
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class RedisContainer extends Container {
|
||||
public class RedisContainer extends GenericContainer<RedisContainer> {
|
||||
|
||||
public RedisContainer() {
|
||||
super("redis:4.0.6", 6379);
|
||||
super("redis:4.0.6");
|
||||
addExposedPorts(6379);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.testcontainers;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.testcontainers.DockerClientFactory;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.lifecycle.Startable;
|
||||
|
||||
/**
|
||||
* A {@link GenericContainer} decorator that skips test execution when Docker is not
|
||||
* available.
|
||||
*
|
||||
* @param <T> type of the underlying container
|
||||
* @author Andy Wilkinson
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class SkippableContainer<T extends GenericContainer<?>> implements Startable {
|
||||
|
||||
private final Supplier<T> containerFactory;
|
||||
|
||||
private T container;
|
||||
|
||||
public SkippableContainer(Supplier<T> containerFactory) {
|
||||
this.containerFactory = containerFactory;
|
||||
}
|
||||
|
||||
public T getContainer() {
|
||||
if (this.container == null) {
|
||||
throw new IllegalStateException("Container cannot be accessed prior to test invocation");
|
||||
}
|
||||
return this.container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
Assumptions.assumeTrue(isDockerRunning(), "Could not find valid docker environment.");
|
||||
this.container = this.containerFactory.get();
|
||||
this.container.start();
|
||||
}
|
||||
|
||||
private boolean isDockerRunning() {
|
||||
try {
|
||||
DockerClientFactory.instance().client();
|
||||
return true;
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (this.container != null) {
|
||||
this.container.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-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,7 +16,7 @@
|
||||
|
||||
package org.springframework.boot.testsupport.assertj;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
@@ -27,27 +27,27 @@ import static org.hamcrest.Matchers.startsWith;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class MatchedTests {
|
||||
class MatchedTests {
|
||||
|
||||
@Test
|
||||
public void byMatcherMatches() {
|
||||
void byMatcherMatches() {
|
||||
assertThat("1234").is(Matched.by(startsWith("12")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byMatcherDoesNotMatch() {
|
||||
void byMatcherDoesNotMatch() {
|
||||
assertThatExceptionOfType(AssertionError.class)
|
||||
.isThrownBy(() -> assertThat("1234").is(Matched.by(startsWith("23"))))
|
||||
.withMessageContaining("a string starting with \"23\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatcherMatches() {
|
||||
void whenMatcherMatches() {
|
||||
assertThat("1234").is(Matched.when(startsWith("12")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatcherDoesNotMatch() {
|
||||
void whenMatcherDoesNotMatch() {
|
||||
assertThatExceptionOfType(AssertionError.class)
|
||||
.isThrownBy(() -> assertThat("1234").is(Matched.when(startsWith("23"))))
|
||||
.withMessageContaining("a string starting with \"23\"");
|
||||
|
||||
Reference in New Issue
Block a user