Skip to content

@MockBean and @SpyBean don't work when @DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) is used #11903

@xak2000

Description

@xak2000

Spring Boot 1.5.10.RELEASE

@SpyBean doesn't work when @DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) is placed on the test class.

Example:

@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)
public class MyServiceTests {

	@Autowired
	private MyService myService;

	@SpyBean
	private SpyMe spyMe;

	@Test
	public void spyBean() {
		myService.serviceMethod();
		verify(spyMe).someMethod(); // doesn't pass
	}

}

@Service
public class MyService {

	@Autowired
	private SpyMe spyMe;

	public void serviceMethod() {
		spyMe.someMethod();
	}

}

@Component
public class SpyMe {

	public void someMethod() {
		System.out.println("SpyMe.someMethod() was called");
	}

}

If I remove @DirtiesContext or use other classMode (AFTER_EACH_TEST_METHOD for example) - test passes.

MCVE: https://github.com/xak2000/spybean-dirtiescontext-bug

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions