Skip to content

Commit

Permalink
Merge branch '2.4.x'
Browse files Browse the repository at this point in the history
Closes gh-26646
  • Loading branch information
snicoll committed May 24, 2021
2 parents 363b508 + 9426917 commit cb7c377
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ dependencies {
testImplementation("org.hibernate.validator:hibernate-validator")
testImplementation("org.hsqldb:hsqldb")
testImplementation("org.jooq:jooq")
testImplementation("org.junit.platform:junit-platform-engine")
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.junit.platform:junit-platform-engine")
testImplementation("org.junit.platform:junit-platform-launcher")
testImplementation("org.mockito:mockito-core")
testImplementation("org.mockito:mockito-junit-jupiter")
testImplementation("org.skyscreamer:jsonassert")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -21,16 +21,17 @@

import org.junit.jupiter.api.Test;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.ExampleEntity;
import org.springframework.boot.testsupport.junit.platform.Launcher;
import org.springframework.boot.testsupport.junit.platform.LauncherDiscoveryRequest;
import org.springframework.boot.testsupport.junit.platform.LauncherDiscoveryRequestBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -49,7 +50,7 @@ class ImportsContextCustomizerFactoryWithAutoConfigurationTests {
static ApplicationContext contextFromTest;

@Test
void testClassesThatHaveSameAnnotationsShareAContext() throws Throwable {
void testClassesThatHaveSameAnnotationsShareAContext() {
executeTests(DataJpaTest1.class);
ApplicationContext test1Context = contextFromTest;
executeTests(DataJpaTest3.class);
Expand All @@ -58,7 +59,7 @@ void testClassesThatHaveSameAnnotationsShareAContext() throws Throwable {
}

@Test
void testClassesThatOnlyHaveDifferingUnrelatedAnnotationsShareAContext() throws Throwable {
void testClassesThatOnlyHaveDifferingUnrelatedAnnotationsShareAContext() {
executeTests(DataJpaTest1.class);
ApplicationContext test1Context = contextFromTest;
executeTests(DataJpaTest2.class);
Expand All @@ -67,19 +68,18 @@ void testClassesThatOnlyHaveDifferingUnrelatedAnnotationsShareAContext() throws
}

@Test
void testClassesThatOnlyHaveDifferingPropertyMappedAnnotationAttributesDoNotShareAContext() throws Throwable {
void testClassesThatOnlyHaveDifferingPropertyMappedAnnotationAttributesDoNotShareAContext() {
executeTests(DataJpaTest1.class);
ApplicationContext test1Context = contextFromTest;
executeTests(DataJpaTest4.class);
ApplicationContext test2Context = contextFromTest;
assertThat(test1Context).isNotSameAs(test2Context);
}

private void executeTests(Class<?> testClass) throws Throwable {
ClassLoader classLoader = testClass.getClassLoader();
LauncherDiscoveryRequest request = new LauncherDiscoveryRequestBuilder(classLoader)
private void executeTests(Class<?> testClass) {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(DiscoverySelectors.selectClass(testClass)).build();
Launcher launcher = new Launcher(testClass.getClassLoader());
Launcher launcher = LauncherFactory.create();
launcher.execute(request);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -21,15 +21,16 @@
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.testsupport.junit.platform.Launcher;
import org.springframework.boot.testsupport.junit.platform.LauncherDiscoveryRequest;
import org.springframework.boot.testsupport.junit.platform.LauncherDiscoveryRequestBuilder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;

Expand All @@ -49,22 +50,21 @@
class WebMvcTestPrintDefaultIntegrationTests {

@Test
void shouldNotPrint(CapturedOutput output) throws Throwable {
void shouldNotPrint(CapturedOutput output) {
executeTests(ShouldNotPrint.class);
assertThat(output).doesNotContain("HTTP Method");
}

@Test
void shouldPrint(CapturedOutput output) throws Throwable {
void shouldPrint(CapturedOutput output) {
executeTests(ShouldPrint.class);
assertThat(output).containsOnlyOnce("HTTP Method");
}

private void executeTests(Class<?> testClass) throws Throwable {
ClassLoader classLoader = testClass.getClassLoader();
LauncherDiscoveryRequest request = new LauncherDiscoveryRequestBuilder(classLoader)
private void executeTests(Class<?> testClass) {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(DiscoverySelectors.selectClass(testClass)).build();
Launcher launcher = new Launcher(testClass.getClassLoader());
Launcher launcher = LauncherFactory.create();
launcher.execute(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
compileOnly("org.elasticsearch:elasticsearch")
compileOnly("org.junit.jupiter:junit-jupiter")
compileOnly("org.junit.platform:junit-platform-engine")
compileOnly("org.junit.platform:junit-platform-launcher")
compileOnly("org.mockito:mockito-core")
compileOnly("org.springframework:spring-context")
compileOnly("org.springframework.data:spring-data-redis")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -24,12 +24,16 @@
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;

import org.springframework.boot.testsupport.junit.platform.Launcher;
import org.springframework.boot.testsupport.junit.platform.LauncherDiscoveryRequest;
import org.springframework.boot.testsupport.junit.platform.LauncherDiscoveryRequestBuilder;
import org.springframework.boot.testsupport.junit.platform.SummaryGeneratingListener;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;

/**
Expand Down Expand Up @@ -95,17 +99,18 @@ private void runTestWithModifiedClassPath(ReflectiveInvocationContext<Method> in
}

private void runTest(ClassLoader classLoader, String testClassName, String testMethodName) throws Throwable {
Class<?> testClass = Class.forName(testClassName, false, classLoader);
Class<?> testClass = classLoader.loadClass(testClassName);
Method testMethod = findMethod(testClass, testMethodName);
LauncherDiscoveryRequest request = new LauncherDiscoveryRequestBuilder(classLoader)
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(DiscoverySelectors.selectMethod(testClass, testMethod)).build();
Launcher launcher = new Launcher(classLoader);
SummaryGeneratingListener listener = new SummaryGeneratingListener(classLoader);
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
Throwable failure = listener.getSummary().getFailure();
if (failure != null) {
throw failure;
launcher.execute(testPlan);
TestExecutionSummary summary = listener.getSummary();
if (!CollectionUtils.isEmpty(summary.getFailures())) {
throw summary.getFailures().get(0).getException();
}
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit cb7c377

Please sign in to comment.