Skip to content

Commit

Permalink
Fix resetting of spied FactoryBean output
Browse files Browse the repository at this point in the history
Fixes gh-31204
  • Loading branch information
wilkinsona committed Nov 6, 2024
1 parent e089b90 commit 4900ca1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,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
@@ -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.
Expand Down Expand Up @@ -47,13 +47,17 @@ 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(getMock("fromFactoryBean").greeting()).willReturn("fromFactoryBean");
assertThat(this.context.getBean(NonSingletonFactoryBean.class).getObjectInvocations).isEqualTo(0);
given(this.spied.action()).willReturn("spied");
}

@Test
Expand All @@ -63,6 +67,7 @@ void test002() {
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) {
Expand Down Expand Up @@ -116,6 +121,11 @@ NonSingletonFactoryBean nonSingletonFactoryBean() {
return new NonSingletonFactoryBean();
}

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

}

static class BrokenFactoryBean implements FactoryBean<String> {
Expand Down Expand Up @@ -158,6 +168,14 @@ public boolean isSingleton() {

}

static class ToSpy {

String action() {
return null;
}

}

static class NonSingletonFactoryBean implements FactoryBean<ExampleService> {

private int getObjectInvocations = 0;
Expand All @@ -180,4 +198,18 @@ public boolean isSingleton() {

}

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 4900ca1

Please sign in to comment.