commit
118f736260
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2022 the original author or authors.
|
* Copyright 2012-2023 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.
|
||||||
|
|
@ -79,7 +79,7 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen
|
||||||
for (String name : names) {
|
for (String name : names) {
|
||||||
BeanDefinition definition = beanFactory.getBeanDefinition(name);
|
BeanDefinition definition = beanFactory.getBeanDefinition(name);
|
||||||
if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
|
if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
|
||||||
Object bean = beanFactory.getSingleton(name);
|
Object bean = getBean(beanFactory, name);
|
||||||
if (reset.equals(MockReset.get(bean))) {
|
if (reset.equals(MockReset.get(bean))) {
|
||||||
Mockito.reset(bean);
|
Mockito.reset(bean);
|
||||||
}
|
}
|
||||||
|
|
@ -101,4 +101,13 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object getBean(ConfigurableListableBeanFactory beanFactory, String name) {
|
||||||
|
try {
|
||||||
|
return beanFactory.getBean(name);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
return beanFactory.getSingleton(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2023 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.
|
||||||
|
|
@ -52,6 +52,7 @@ class ResetMocksTestExecutionListenerTests {
|
||||||
given(getMock("none").greeting()).willReturn("none");
|
given(getMock("none").greeting()).willReturn("none");
|
||||||
given(getMock("before").greeting()).willReturn("before");
|
given(getMock("before").greeting()).willReturn("before");
|
||||||
given(getMock("after").greeting()).willReturn("after");
|
given(getMock("after").greeting()).willReturn("after");
|
||||||
|
given(getMock("fromFactoryBean").greeting()).willReturn("fromFactoryBean");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -59,6 +60,7 @@ class ResetMocksTestExecutionListenerTests {
|
||||||
assertThat(getMock("none").greeting()).isEqualTo("none");
|
assertThat(getMock("none").greeting()).isEqualTo("none");
|
||||||
assertThat(getMock("before").greeting()).isNull();
|
assertThat(getMock("before").greeting()).isNull();
|
||||||
assertThat(getMock("after").greeting()).isNull();
|
assertThat(getMock("after").greeting()).isNull();
|
||||||
|
assertThat(getMock("fromFactoryBean").greeting()).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
ExampleService getMock(String name) {
|
ExampleService getMock(String name) {
|
||||||
|
|
@ -102,6 +104,11 @@ class ResetMocksTestExecutionListenerTests {
|
||||||
return new BrokenFactoryBean();
|
return new BrokenFactoryBean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
WorkingFactoryBean fromFactoryBean() {
|
||||||
|
return new WorkingFactoryBean();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static class BrokenFactoryBean implements FactoryBean<String> {
|
static class BrokenFactoryBean implements FactoryBean<String> {
|
||||||
|
|
@ -123,4 +130,23 @@ class ResetMocksTestExecutionListenerTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class WorkingFactoryBean implements FactoryBean<ExampleService> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExampleService getObject() {
|
||||||
|
return mock(ExampleService.class, MockReset.before());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getObjectType() {
|
||||||
|
return ExampleService.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSingleton() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue