Browse Source

Fix resetting of spied FactoryBean output

Fixes gh-31204
pull/43333/head
Andy Wilkinson 1 year ago
parent
commit
4900ca1ffc
  1. 1
      spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java
  2. 34
      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

@ -330,6 +330,7 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor @@ -330,6 +330,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;
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -47,6 +47,9 @@ class ResetMocksTestExecutionListenerTests { @@ -47,6 +47,9 @@ class ResetMocksTestExecutionListenerTests {
@Autowired
private ApplicationContext context;
@SpyBean
ToSpy spied;
@Test
void test001() {
given(getMock("none").greeting()).willReturn("none");
@ -54,6 +57,7 @@ class ResetMocksTestExecutionListenerTests { @@ -54,6 +57,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
@ -63,6 +67,7 @@ class ResetMocksTestExecutionListenerTests { @@ -63,6 +67,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) {
@ -116,6 +121,11 @@ class ResetMocksTestExecutionListenerTests { @@ -116,6 +121,11 @@ class ResetMocksTestExecutionListenerTests {
return new NonSingletonFactoryBean();
}
@Bean
ToSpyFactoryBean toSpyFactoryBean() {
return new ToSpyFactoryBean();
}
}
static class BrokenFactoryBean implements FactoryBean<String> {
@ -158,6 +168,14 @@ class ResetMocksTestExecutionListenerTests { @@ -158,6 +168,14 @@ class ResetMocksTestExecutionListenerTests {
}
static class ToSpy {
String action() {
return null;
}
}
static class NonSingletonFactoryBean implements FactoryBean<ExampleService> {
private int getObjectInvocations = 0;
@ -180,4 +198,18 @@ class ResetMocksTestExecutionListenerTests { @@ -180,4 +198,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