No-op ResetMocksTestExecutionListener when Mockito is not present

Closes gh-11508
This commit is contained in:
Andy Wilkinson 2018-01-05 11:04:36 +00:00
parent 641013787d
commit 268b97bf98
2 changed files with 13 additions and 4 deletions

View File

@ -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.
@ -30,6 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
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
@ -40,14 +41,22 @@ import org.springframework.test.context.support.AbstractTestExecutionListener;
*/
public class ResetMocksTestExecutionListener extends AbstractTestExecutionListener {
private static final boolean MOCKITO_IS_PRESENT = ClassUtils.isPresent(
"org.mockito.MockSettings",
ResetMocksTestExecutionListener.class.getClassLoader());
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
resetMocks(testContext.getApplicationContext(), MockReset.BEFORE);
if (MOCKITO_IS_PRESENT) {
resetMocks(testContext.getApplicationContext(), MockReset.BEFORE);
}
}
@Override
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) {

View File

@ -17,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Madhura Bhave
*/
@RunWith(SpringRunner.class)
public class SampleTestNoMockitoApplicationTest {
public class SampleTestNoMockitoApplicationTests {
// gh-7065