Skip to content

Commit

Permalink
Fix reseting of a spy that is spying on a factory bean's output
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed May 30, 2022
1 parent 419ac26 commit 0c7b98b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ protected final Object createSpyIfNecessary(Object bean, String beanName) throws
SpyDefinition definition = this.spies.get(beanName);
if (definition != null) {
bean = definition.createSpy(beanName, bean);
this.mockitoBeans.add(bean);
}
return bean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,23 @@ class ResetMocksTestExecutionListenerTests {
@Autowired
private ApplicationContext context;

@SpyBean
ToSpy spied;

@Test
void test001() {
given(getMock("none").greeting()).willReturn("none");
given(getMock("before").greeting()).willReturn("before");
given(getMock("after").greeting()).willReturn("after");
given(this.spied.action()).willReturn("spied");
}

@Test
void test002() {
assertThat(getMock("none").greeting()).isEqualTo("none");
assertThat(getMock("before").greeting()).isNull();
assertThat(getMock("after").greeting()).isNull();
assertThat(this.spied.action()).isNull();
}

ExampleService getMock(String name) {
Expand Down Expand Up @@ -102,6 +107,11 @@ BrokenFactoryBean brokenFactoryBean() {
return new BrokenFactoryBean();
}

@Bean
ToSpyFactoryBean toSpyFactoryBean() {
return new ToSpyFactoryBean();
}

}

static class BrokenFactoryBean implements FactoryBean<String> {
Expand All @@ -123,4 +133,32 @@ public boolean isSingleton() {

}

interface ToMock {

String action();

}

static class ToSpy {

String action() {
return null;
}

}

static class ToSpyFactoryBean implements FactoryBean<ToSpy> {

@Override
public ToSpy getObject() throws Exception {
return new ToSpy();
}

@Override
public Class<?> getObjectType() {
return ToSpy.class;
}

}

}

0 comments on commit 0c7b98b

Please sign in to comment.