No-op ResetMocksTestExecutionListener when Mockito is not present
Closes gh-11508
This commit is contained in:
parent
641013787d
commit
268b97bf98
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
Loading…
Reference in New Issue