Skip to content

Commit 118f736

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-34044
2 parents 0184548 + 9940fcf commit 118f736

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,7 +79,7 @@ private void resetMocks(ConfigurableApplicationContext applicationContext, MockR
7979
for (String name : names) {
8080
BeanDefinition definition = beanFactory.getBeanDefinition(name);
8181
if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
82-
Object bean = beanFactory.getSingleton(name);
82+
Object bean = getBean(beanFactory, name);
8383
if (reset.equals(MockReset.get(bean))) {
8484
Mockito.reset(bean);
8585
}
@@ -101,4 +101,13 @@ private void resetMocks(ConfigurableApplicationContext applicationContext, MockR
101101
}
102102
}
103103

104+
private Object getBean(ConfigurableListableBeanFactory beanFactory, String name) {
105+
try {
106+
return beanFactory.getBean(name);
107+
}
108+
catch (Exception ex) {
109+
return beanFactory.getSingleton(name);
110+
}
111+
}
112+
104113
}

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListenerTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,13 +52,15 @@ void test001() {
5252
given(getMock("none").greeting()).willReturn("none");
5353
given(getMock("before").greeting()).willReturn("before");
5454
given(getMock("after").greeting()).willReturn("after");
55+
given(getMock("fromFactoryBean").greeting()).willReturn("fromFactoryBean");
5556
}
5657

5758
@Test
5859
void test002() {
5960
assertThat(getMock("none").greeting()).isEqualTo("none");
6061
assertThat(getMock("before").greeting()).isNull();
6162
assertThat(getMock("after").greeting()).isNull();
63+
assertThat(getMock("fromFactoryBean").greeting()).isNull();
6264
}
6365

6466
ExampleService getMock(String name) {
@@ -102,6 +104,11 @@ BrokenFactoryBean brokenFactoryBean() {
102104
return new BrokenFactoryBean();
103105
}
104106

107+
@Bean
108+
WorkingFactoryBean fromFactoryBean() {
109+
return new WorkingFactoryBean();
110+
}
111+
105112
}
106113

107114
static class BrokenFactoryBean implements FactoryBean<String> {
@@ -123,4 +130,23 @@ public boolean isSingleton() {
123130

124131
}
125132

133+
static class WorkingFactoryBean implements FactoryBean<ExampleService> {
134+
135+
@Override
136+
public ExampleService getObject() {
137+
return mock(ExampleService.class, MockReset.before());
138+
}
139+
140+
@Override
141+
public Class<?> getObjectType() {
142+
return ExampleService.class;
143+
}
144+
145+
@Override
146+
public boolean isSingleton() {
147+
return true;
148+
}
149+
150+
}
151+
126152
}

0 commit comments

Comments
 (0)