From d6d4251550a82fc6642bd11c43664e8c0dba5294 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 8 Jul 2016 17:22:42 +0200 Subject: [PATCH] Utilize default methods in TestExecutionListener Issue: SPR-14432 --- .../test/context/TestExecutionListener.java | 35 ++++++++++-- .../AbstractTestExecutionListener.java | 55 ++----------------- 2 files changed, 34 insertions(+), 56 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java index fbca021a94..74fe7fc401 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java @@ -21,6 +21,10 @@ package org.springframework.test.context; * test execution events published by the {@link TestContextManager} with which * the listener is registered. * + *

This interface provides empty {@code default} implementations for all methods. + * Concrete implementations can therefore choose to override only those methods + * suitable for the task at hand. + * *

Concrete implementations must provide a {@code public} no-args constructor, * so that listeners can be instantiated transparently by tools and configuration * mechanisms. @@ -51,6 +55,7 @@ package org.springframework.test.context; * @author Sam Brannen * @author Juergen Hoeller * @since 2.5 + * @see org.springframework.test.context.support.AbstractTestExecutionListener */ public interface TestExecutionListener { @@ -61,20 +66,28 @@ public interface TestExecutionListener { * before class lifecycle callbacks. *

If a given testing framework does not support before class * lifecycle callbacks, this method will not be called for that framework. + *

The default implementation is empty. Can be overridden by + * concrete classes as necessary. * @param testContext the test context for the test; never {@code null} * @throws Exception allows any exception to propagate */ - void beforeTestClass(TestContext testContext) throws Exception; + default void beforeTestClass(TestContext testContext) throws Exception { + /* no-op */ + } /** * Prepares the {@link Object test instance} of the supplied * {@link TestContext test context}, for example by injecting dependencies. *

This method should be called immediately after instantiation of the test * instance but prior to any framework-specific lifecycle callbacks. + *

The default implementation is empty. Can be overridden by + * concrete classes as necessary. * @param testContext the test context for the test; never {@code null} * @throws Exception allows any exception to propagate */ - void prepareTestInstance(TestContext testContext) throws Exception; + default void prepareTestInstance(TestContext testContext) throws Exception { + /* no-op */ + } /** * Pre-processes a test before execution of the @@ -83,11 +96,15 @@ public interface TestExecutionListener { * fixtures. *

This method should be called immediately prior to framework-specific * before lifecycle callbacks. + *

The default implementation is empty. Can be overridden by + * concrete classes as necessary. * @param testContext the test context in which the test method will be * executed; never {@code null} * @throws Exception allows any exception to propagate */ - void beforeTestMethod(TestContext testContext) throws Exception; + default void beforeTestMethod(TestContext testContext) throws Exception { + /* no-op */ + } /** * Post-processes a test after execution of the @@ -96,11 +113,15 @@ public interface TestExecutionListener { * fixtures. *

This method should be called immediately after framework-specific * after lifecycle callbacks. + *

The default implementation is empty. Can be overridden by + * concrete classes as necessary. * @param testContext the test context in which the test method was * executed; never {@code null} * @throws Exception allows any exception to propagate */ - void afterTestMethod(TestContext testContext) throws Exception; + default void afterTestMethod(TestContext testContext) throws Exception { + /* no-op */ + } /** * Post-processes a test class after execution of all tests within @@ -109,9 +130,13 @@ public interface TestExecutionListener { * after class lifecycle callbacks. *

If a given testing framework does not support after class * lifecycle callbacks, this method will not be called for that framework. + *

The default implementation is empty. Can be overridden by + * concrete classes as necessary. * @param testContext the test context for the test; never {@code null} * @throws Exception allows any exception to propagate */ - void afterTestClass(TestContext testContext) throws Exception; + default void afterTestClass(TestContext testContext) throws Exception { + /* no-op */ + } } diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestExecutionListener.java index f7b99f1e7e..ac395780fa 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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,65 +17,18 @@ package org.springframework.test.context.support; import org.springframework.core.Ordered; -import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; /** - * Abstract implementation of the {@link TestExecutionListener} interface which - * provides empty method stubs. Subclasses can extend this class and override - * only those methods suitable for the task at hand. + * Abstract {@linkplain Ordered ordered} implementation of the + * {@link TestExecutionListener} API. * * @author Sam Brannen - * @author Juergen Hoeller * @since 2.5 + * @see #getOrder() */ public abstract class AbstractTestExecutionListener implements TestExecutionListener, Ordered { - /** - * The default implementation is empty. Can be overridden by - * subclasses as necessary. - */ - @Override - public void beforeTestClass(TestContext testContext) throws Exception { - /* no-op */ - } - - /** - * The default implementation is empty. Can be overridden by - * subclasses as necessary. - */ - @Override - public void prepareTestInstance(TestContext testContext) throws Exception { - /* no-op */ - } - - /** - * The default implementation is empty. Can be overridden by - * subclasses as necessary. - */ - @Override - public void beforeTestMethod(TestContext testContext) throws Exception { - /* no-op */ - } - - /** - * The default implementation is empty. Can be overridden by - * subclasses as necessary. - */ - @Override - public void afterTestMethod(TestContext testContext) throws Exception { - /* no-op */ - } - - /** - * The default implementation is empty. Can be overridden by - * subclasses as necessary. - */ - @Override - public void afterTestClass(TestContext testContext) throws Exception { - /* no-op */ - } - /** * The default implementation returns {@link Ordered#LOWEST_PRECEDENCE}, * thereby ensuring that custom listeners are ordered after default -- GitLab