admin管理员组

文章数量:1356304

I've updated the version of Spring Boot in my project to 3.4.4. And now one of the JUnit tests starts failing with the exception .springframework.beans.BeanInstantiationException: Failed to instantiate [.springframework.boot.actuate.health.HealthContributor]: Factory method 'dbHealthContributor' threw exception with message: Error creating bean with name 'jpaMappingContext': Metamodel must not be null Below is a sample code to reproduce this problem.

Main class

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Test code

import .apache.camel.Message;

public class Base {
    
    protected Exchange getExchange(final String body) {
        final Message message = new Message() {
            //Default implementation of the methods
        };
        
        message.setBody(body);
        DefaultExchange exchange = new DefaultExchange(MyProcessorTest.EarlyConfiguration.camelContext);
        exchange.setMessage(message);
        
        return exchange;
    }
}
import jakarta.persistence.EntityManagerFactory;

@ExtendWith(SpringExtension.class)
@SpringBootTest(properties = { "test.configuration.class=MyProcessorTest" })
@EnableAutoConfiguration(exclude = { CamelAutoConfiguration.class })
class MyProcessorTest extends Base {
    
    @Test
    void test() {
        assertEquals(1, 1);
    }
    
    @TestConfiguration
    @ConditionalOnProperty(name = "test.configuration.class", havingValue = "MyProcessorTest")
    public static class EarlyConfiguration {

        @MockBean
        public static CamelContext camelContext;

        @MockBean
        EntityManagerFactory entityManagerFactory;
    }
}

pom.xml

    <parent>
    <!-- spring-boot-starter-parent 3.4.4 -->
    </parent>
    
    <properties>
        <java.version>17</java.version>
        <camel.version>4.10.2</camel.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>   
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.yaml

server:
  port: 9102

shared_root: "tmp/shared/tfd"

spring:
  datasource:
    url: jdbc:h2:mem:mydb;Mode=Oracle;NON_KEYWORDS=KEY,VALUE
    username: sa
    password: password
    driverClassName: .h2.Driver
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: none
  test:
    database:
      replace: none

The code above works fine for Spring Boot v.3.4.3. While playing around with this example I have noticed that the test runs without error for Spring Boot 3.4.4 if I delete the following lines from the test class:

@MockBean
EntityManagerFactory entityManagerFactory;

But for now I don't have any technical explanation why this helps.

本文标签: