Commit 268b97bf authored by Andy Wilkinson's avatar Andy Wilkinson

No-op ResetMocksTestExecutionListener when Mockito is not present

Closes gh-11508
parent 64101378
/* /*
* 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"); * 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.
...@@ -30,6 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext; ...@@ -30,6 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.util.ClassUtils;
/** /**
* {@link TestExecutionListener} to reset any mock beans that have been marked with a * {@link TestExecutionListener} to reset any mock beans that have been marked with a
...@@ -40,14 +41,22 @@ import org.springframework.test.context.support.AbstractTestExecutionListener; ...@@ -40,14 +41,22 @@ import org.springframework.test.context.support.AbstractTestExecutionListener;
*/ */
public class ResetMocksTestExecutionListener extends AbstractTestExecutionListener { public class ResetMocksTestExecutionListener extends AbstractTestExecutionListener {
private static final boolean MOCKITO_IS_PRESENT = ClassUtils.isPresent(
"org.mockito.MockSettings",
ResetMocksTestExecutionListener.class.getClassLoader());
@Override @Override
public void beforeTestMethod(TestContext testContext) throws Exception { public void beforeTestMethod(TestContext testContext) throws Exception {
resetMocks(testContext.getApplicationContext(), MockReset.BEFORE); if (MOCKITO_IS_PRESENT) {
resetMocks(testContext.getApplicationContext(), MockReset.BEFORE);
}
} }
@Override @Override
public void afterTestMethod(TestContext testContext) throws Exception { public void afterTestMethod(TestContext testContext) throws Exception {
resetMocks(testContext.getApplicationContext(), MockReset.AFTER); if (MOCKITO_IS_PRESENT) {
resetMocks(testContext.getApplicationContext(), MockReset.AFTER);
}
} }
private void resetMocks(ApplicationContext applicationContext, MockReset reset) { private void resetMocks(ApplicationContext applicationContext, MockReset reset) {
......
...@@ -17,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -17,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Madhura Bhave * @author Madhura Bhave
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class SampleTestNoMockitoApplicationTest { public class SampleTestNoMockitoApplicationTests {
// gh-7065 // gh-7065
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment