From 5c723b0fce02c90cd917a381203bc6bf6a66ffa3 Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Sat, 11 Jul 2020 13:58:07 +0900 Subject: [PATCH] Set attribute that helps MockitoPostProcessor replace a MyBatis mapper bean with a mock https://github.com/mybatis/spring-boot-starter/issues/475 https://github.com/spring-projects/spring-boot/commit/f4985abf3cd299f0842d9f474942c418f177eb13 --- .../org/mybatis/spring/mapper/ClassPathMapperScanner.java | 7 +++++++ .../spring/mapper/MapperScannerConfigurerTest.java | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java b/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java index 983b36ae20..208b579eb3 100644 --- a/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java +++ b/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java @@ -57,6 +57,9 @@ public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner { private static final Logger LOGGER = LoggerFactory.getLogger(ClassPathMapperScanner.class); + // Copy of FactoryBean#OBJECT_TYPE_ATTRIBUTE which was added in Spring 5.2 + static final String FACTORY_BEAN_OBJECT_TYPE = "factoryBeanObjectType"; + private boolean addToConfig = true; private boolean lazyInitialization; @@ -234,6 +237,10 @@ private void processBeanDefinitions(Set beanDefinitions) { definition.getPropertyValues().add("addToConfig", this.addToConfig); + // Attribute for MockitoPostProcessor + // https://github.com/mybatis/spring-boot-starter/issues/475 + definition.setAttribute(FACTORY_BEAN_OBJECT_TYPE, beanClassName); + boolean explicitFactoryUsed = false; if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) { definition.getPropertyValues().add("sqlSessionFactory", diff --git a/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java b/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java index 05e453aa2e..d62d1af27a 100644 --- a/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java +++ b/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java @@ -385,6 +385,14 @@ void testScanWithMapperFactoryBeanClass() { assertTrue(DummyMapperFactoryBean.getMapperCount() > 0); } + @Test + void testMapperBeanAttribute() { + startContext(); + + assertThat(applicationContext.getBeanDefinition("annotatedMapper").getAttribute(ClassPathMapperScanner.FACTORY_BEAN_OBJECT_TYPE)) + .isEqualTo(AnnotatedMapper.class.getName()); + } + private void setupSqlSessionFactory(String name) { GenericBeanDefinition definition = new GenericBeanDefinition(); definition.setBeanClass(SqlSessionFactoryBean.class);