From 9107c5cae32fb981c140ec728bf499bd15775211 Mon Sep 17 00:00:00 2001 From: Christoph Dreis Date: Wed, 15 Mar 2023 17:06:25 +0100 Subject: [PATCH] Avoid repeated class lookups in tests See gh-34613 --- .../restdocs/RestDocsTestExecutionListener.java | 14 ++++++-------- .../MockWebServiceServerTestExecutionListener.java | 12 +++++------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestExecutionListener.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestExecutionListener.java index c9760a5b96dd..9d0d7c6f1a52 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestExecutionListener.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestExecutionListener.java @@ -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 @@ */ 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 int getOrder() { @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) { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerTestExecutionListener.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerTestExecutionListener.java index 6e65ea6551d6..d9ecd2961d8a 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerTestExecutionListener.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerTestExecutionListener.java @@ -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 @@ */ 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 int getOrder() { @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 void afterTestMethod(TestContext testContext) { } } - private boolean isMockWebServiceServerPresent() { - return ClassUtils.isPresent(MOCK_SERVER_CLASS, getClass().getClassLoader()); - } - }