Avoid repeated class lookups in tests

See gh-34613
pull/34975/head
Christoph Dreis 2 years ago committed by Stephane Nicoll
parent 6c4809f54b
commit 9107c5cae3

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { 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 @Override
public int getOrder() { public int getOrder() {
@ -42,22 +44,18 @@ public class RestDocsTestExecutionListener extends AbstractTestExecutionListener
@Override @Override
public void beforeTestMethod(TestContext testContext) throws Exception { public void beforeTestMethod(TestContext testContext) throws Exception {
if (restDocsIsPresent()) { if (REST_DOCS_PRESENT) {
new DocumentationHandler().beforeTestMethod(testContext); new DocumentationHandler().beforeTestMethod(testContext);
} }
} }
@Override @Override
public void afterTestMethod(TestContext testContext) throws Exception { public void afterTestMethod(TestContext testContext) throws Exception {
if (restDocsIsPresent()) { if (REST_DOCS_PRESENT) {
new DocumentationHandler().afterTestMethod(testContext); new DocumentationHandler().afterTestMethod(testContext);
} }
} }
private boolean restDocsIsPresent() {
return ClassUtils.isPresent(REST_DOCS_CLASS, getClass().getClassLoader());
}
private static class DocumentationHandler { private static class DocumentationHandler {
private void beforeTestMethod(TestContext testContext) { private void beforeTestMethod(TestContext testContext) {

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { 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 @Override
public int getOrder() { public int getOrder() {
@ -42,7 +44,7 @@ public class MockWebServiceServerTestExecutionListener extends AbstractTestExecu
@Override @Override
public void afterTestMethod(TestContext testContext) { public void afterTestMethod(TestContext testContext) {
if (isMockWebServiceServerPresent()) { if (MOCK_SERVER_PRESENT) {
ApplicationContext applicationContext = testContext.getApplicationContext(); ApplicationContext applicationContext = testContext.getApplicationContext();
String[] names = applicationContext.getBeanNamesForType(MockWebServiceServer.class, false, false); String[] names = applicationContext.getBeanNamesForType(MockWebServiceServer.class, false, false);
for (String name : names) { for (String name : names) {
@ -53,8 +55,4 @@ public class MockWebServiceServerTestExecutionListener extends AbstractTestExecu
} }
} }
private boolean isMockWebServiceServerPresent() {
return ClassUtils.isPresent(MOCK_SERVER_CLASS, getClass().getClassLoader());
}
} }

Loading…
Cancel
Save