Browse Source

Merge branch '3.3.x'

Closes gh-43053
pull/43065/head
Andy Wilkinson 1 year ago
parent
commit
362593019d
  1. 1
      spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java
  2. 32
      spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListenerTests.java

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

@ -336,6 +336,7 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor @@ -336,6 +336,7 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
SpyDefinition definition = this.spies.get(beanName);
if (definition != null) {
bean = definition.createSpy(beanName, bean);
this.mockitoBeans.add(bean);
}
return bean;
}

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

@ -50,6 +50,9 @@ class ResetMocksTestExecutionListenerTests { @@ -50,6 +50,9 @@ class ResetMocksTestExecutionListenerTests {
@Autowired
private ApplicationContext context;
@SpyBean
ToSpy spied;
@Test
void test001() {
given(getMock("none").greeting()).willReturn("none");
@ -57,6 +60,7 @@ class ResetMocksTestExecutionListenerTests { @@ -57,6 +60,7 @@ class ResetMocksTestExecutionListenerTests {
given(getMock("after").greeting()).willReturn("after");
given(getMock("fromFactoryBean").greeting()).willReturn("fromFactoryBean");
assertThat(this.context.getBean(NonSingletonFactoryBean.class).getObjectInvocations).isEqualTo(0);
given(this.spied.action()).willReturn("spied");
}
@Test
@ -66,6 +70,7 @@ class ResetMocksTestExecutionListenerTests { @@ -66,6 +70,7 @@ class ResetMocksTestExecutionListenerTests {
assertThat(getMock("after").greeting()).isNull();
assertThat(getMock("fromFactoryBean").greeting()).isNull();
assertThat(this.context.getBean(NonSingletonFactoryBean.class).getObjectInvocations).isEqualTo(0);
assertThat(this.spied.action()).isNull();
}
ExampleService getMock(String name) {
@ -119,6 +124,11 @@ class ResetMocksTestExecutionListenerTests { @@ -119,6 +124,11 @@ class ResetMocksTestExecutionListenerTests {
return new NonSingletonFactoryBean();
}
@Bean
ToSpyFactoryBean toSpyFactoryBean() {
return new ToSpyFactoryBean();
}
}
static class BrokenFactoryBean implements FactoryBean<String> {
@ -161,6 +171,14 @@ class ResetMocksTestExecutionListenerTests { @@ -161,6 +171,14 @@ class ResetMocksTestExecutionListenerTests {
}
static class ToSpy {
String action() {
return null;
}
}
static class NonSingletonFactoryBean implements FactoryBean<ExampleService> {
private int getObjectInvocations = 0;
@ -183,4 +201,18 @@ class ResetMocksTestExecutionListenerTests { @@ -183,4 +201,18 @@ class ResetMocksTestExecutionListenerTests {
}
static class ToSpyFactoryBean implements FactoryBean<ToSpy> {
@Override
public ToSpy getObject() throws Exception {
return new ToSpy();
}
@Override
public Class<?> getObjectType() {
return ToSpy.class;
}
}
}

Loading…
Cancel
Save