Avoid repeated class lookups in tests

See gh-34613
This commit is contained in:
Christoph Dreis 2023-03-15 17:06:25 +01:00 committed by Stephane Nicoll
parent 6c4809f54b
commit 9107c5cae3
2 changed files with 11 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 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,9 @@ import org.springframework.util.ClassUtils;
*/
public class RestDocsTestExecutionListener extends AbstractTestExecutionListener {
private static final String REST_DOCS_CLASS = "org.springframework.restdocs.ManualRestDocumentation";
private static final boolean REST_DOCS_PRESENT = ClassUtils.isPresent(
"org.springframework.restdocs.ManualRestDocumentation",
RestDocsTestExecutionListener.class.getClassLoader());
@Override
public int getOrder() {
@ -42,22 +44,18 @@ public class RestDocsTestExecutionListener extends AbstractTestExecutionListener
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
if (restDocsIsPresent()) {
if (REST_DOCS_PRESENT) {
new DocumentationHandler().beforeTestMethod(testContext);
}
}
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
if (restDocsIsPresent()) {
if (REST_DOCS_PRESENT) {
new DocumentationHandler().afterTestMethod(testContext);
}
}
private boolean restDocsIsPresent() {
return ClassUtils.isPresent(REST_DOCS_CLASS, getClass().getClassLoader());
}
private static class DocumentationHandler {
private void beforeTestMethod(TestContext testContext) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2023 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,9 @@ import org.springframework.ws.test.client.MockWebServiceServer;
*/
public class MockWebServiceServerTestExecutionListener extends AbstractTestExecutionListener {
private static final String MOCK_SERVER_CLASS = "org.springframework.ws.test.client.MockWebServiceServer";
private static final boolean MOCK_SERVER_PRESENT = ClassUtils.isPresent(
"org.springframework.ws.test.client.MockWebServiceServer",
MockWebServiceServerTestExecutionListener.class.getClassLoader());
@Override
public int getOrder() {
@ -42,7 +44,7 @@ public class MockWebServiceServerTestExecutionListener extends AbstractTestExecu
@Override
public void afterTestMethod(TestContext testContext) {
if (isMockWebServiceServerPresent()) {
if (MOCK_SERVER_PRESENT) {
ApplicationContext applicationContext = testContext.getApplicationContext();
String[] names = applicationContext.getBeanNamesForType(MockWebServiceServer.class, false, false);
for (String name : names) {
@ -53,8 +55,4 @@ public class MockWebServiceServerTestExecutionListener extends AbstractTestExecu
}
}
private boolean isMockWebServiceServerPresent() {
return ClassUtils.isPresent(MOCK_SERVER_CLASS, getClass().getClassLoader());
}
}