Skip to content

@MockitoBean can no longer replace a manually registered singleton #33678

@wilkinsona

Description

@wilkinsona

Affects: 6.2.0-SNAPSHOT

This is a regression since 6.2.0-RC1. The problem can be reproduced with the following:

package com.example;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.example.ManuallyRegisteredSingletonMockitoBeanTests.SingletonRegistrar;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(initializers = SingletonRegistrar.class)
class ManuallyRegisteredSingletonMockitoBeanTests {
	
	@MockitoBean
	SomethingToMock mock;
	
	@Test
	void test() {
		
	}
	
	static class SingletonRegistrar implements ApplicationContextInitializer<ConfigurableApplicationContext> {
		
		@Override
		public void initialize(ConfigurableApplicationContext applicationContext) {
			applicationContext.getBeanFactory().registerSingleton("somethingToMock", new SomethingToMock());
		}
		
	}
	
	static class SomethingToMock {
		
	}	

}

It fails with 6.2.0-20241009.135715-934 (the latest 6.2.0 snapshot at the time of writing). It passes with 6.2.0-RC1. Unfortunately, this is preventing us from moving Boot's build to Framework 6.2.0 snapshots as I've been unable to find a workaround and the affected test cannot be easily disabled as it's used to produce Actuator API documentation.

Metadata

Metadata

Assignees

Labels

in: testIssues in the test moduletype: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions