From 458dde63a80939d06502c1271591f477642b43fb Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 1 Feb 2018 14:20:28 +0000 Subject: [PATCH] Explicitly order AbstractTestExecutionListener subclasses By default, AbstractTestExecutionListeners have an order of lowest precedence. This means that it is impossible to write a listener with lower precedence that any listener that's using the default order. This commit updates Boot's 6 AbstractTestExecutionListeners to order them explicitly. MockitoTestExecutionListener performs injection of Mockito mocks and spies into the test instance. It now has an order of 2050 giving it slightly lower precedence than the dependency injection test execution listener (2000). The remaining 5 listeners have all been ordered with lowest precedence - 100. This leaves them near their current lowest precedence position while creating some room for any listeners that require lower precedence. Closes gh-11796 --- .../restdocs/RestDocsTestExecutionListener.java | 8 +++++++- .../MockRestServiceServerResetTestExecutionListener.java | 8 +++++++- .../MockMvcPrintOnlyOnFailureTestExecutionListener.java | 8 +++++++- .../web/servlet/WebDriverTestExecutionListener.java | 8 +++++++- .../test/mock/mockito/MockitoTestExecutionListener.java | 7 ++++++- .../mock/mockito/ResetMocksTestExecutionListener.java | 6 ++++++ 6 files changed, 40 insertions(+), 5 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 908fa4de06..2a1e3a5abd 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-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -17,6 +17,7 @@ package org.springframework.boot.test.autoconfigure.restdocs; import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.core.Ordered; import org.springframework.restdocs.ManualRestDocumentation; import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; @@ -35,6 +36,11 @@ public class RestDocsTestExecutionListener extends AbstractTestExecutionListener private static final String REST_DOCS_CLASS = "org.springframework.restdocs.ManualRestDocumentation"; + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE - 100; + } + @Override public void beforeTestMethod(TestContext testContext) throws Exception { if (restDocsIsPresent()) { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/MockRestServiceServerResetTestExecutionListener.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/MockRestServiceServerResetTestExecutionListener.java index 98d37744ec..9a4604b830 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/MockRestServiceServerResetTestExecutionListener.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/MockRestServiceServerResetTestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -17,6 +17,7 @@ package org.springframework.boot.test.autoconfigure.web.client; import org.springframework.context.ApplicationContext; +import org.springframework.core.Ordered; import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener; @@ -30,6 +31,11 @@ import org.springframework.test.web.client.MockRestServiceServer; class MockRestServiceServerResetTestExecutionListener extends AbstractTestExecutionListener { + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE - 100; + } + @Override public void afterTestMethod(TestContext testContext) throws Exception { ApplicationContext applicationContext = testContext.getApplicationContext(); diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcPrintOnlyOnFailureTestExecutionListener.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcPrintOnlyOnFailureTestExecutionListener.java index e242235888..05b2176148 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcPrintOnlyOnFailureTestExecutionListener.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcPrintOnlyOnFailureTestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -17,6 +17,7 @@ package org.springframework.boot.test.autoconfigure.web.servlet; import org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter; +import org.springframework.core.Ordered; import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener; @@ -29,6 +30,11 @@ import org.springframework.test.context.support.AbstractTestExecutionListener; class MockMvcPrintOnlyOnFailureTestExecutionListener extends AbstractTestExecutionListener { + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE - 100; + } + @Override public void afterTestMethod(TestContext testContext) throws Exception { if (testContext.getTestException() != null) { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebDriverTestExecutionListener.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebDriverTestExecutionListener.java index 0260dcfad2..a8e18929a0 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebDriverTestExecutionListener.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebDriverTestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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,6 +16,7 @@ package org.springframework.boot.test.autoconfigure.web.servlet; +import org.springframework.core.Ordered; import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener; @@ -30,6 +31,11 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution */ class WebDriverTestExecutionListener extends AbstractTestExecutionListener { + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE - 100; + } + @Override public void afterTestMethod(TestContext testContext) throws Exception { WebDriverScope scope = WebDriverScope diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoTestExecutionListener.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoTestExecutionListener.java index 2a7374022d..f58d48db08 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoTestExecutionListener.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoTestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -41,6 +41,11 @@ import org.springframework.util.ReflectionUtils.FieldCallback; */ public class MockitoTestExecutionListener extends AbstractTestExecutionListener { + @Override + public final int getOrder() { + return 2050; + } + @Override public void prepareTestInstance(TestContext testContext) throws Exception { if (hasMockitoAnnotations(testContext)) { diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java index 0eccd5523b..b28724e91d 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java @@ -27,6 +27,7 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.Ordered; import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener; @@ -45,6 +46,11 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen "org.mockito.MockSettings", ResetMocksTestExecutionListener.class.getClassLoader()); + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE - 100; + } + @Override public void beforeTestMethod(TestContext testContext) throws Exception { if (MOCKITO_IS_PRESENT) {