[Java] [Spring] Spring Boot Dependency injection mysterious hamarineta

It may be a matter of course for those who know it, but I will show it here because it was a great pain.

environment

Premise

There were such repository interfaces.

For unit testing, I created a Configuration class that returns a mock of each repository.

MyConfig.java


@Configuration
public class MyConfig {
    @Bean 
    public MenuRepository menuRepository() {
        return Mockito.mock(MenuRepository.class);
    }

    @Bean 
    public CategoryRepository categoryRepository() {
        return Mockito.mock(CategoryRepository.class);
    }

    @Bean 
    public UserRepository userRepository() {
        return Mockito.mock(UserRepository.class);
    }
}

The test of the injection destination is like this.

MyTest.java


//Abbreviation
public class MyTest {
    @Autowired
    private MenuRepository menuRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private UserRepository userRepository;
}

Mysterious movement

The categoryRepository @Bean method is called, but no other @Bean method is called.

As a result, only Category Repository is mocked at the injection destination. .. ..

Looking at the contents of the container, there are only two CategoryRepository (mock and original) and only one others.

solution

By changing the method name, all @Bean methods are now called safely.

MyConfig.java


@Configuration
public class MyConfig {
    @Bean 
    @Primary
    public MenuRepository menuRepositoryBean() {
        return Mockito.mock(MenuRepository.class);
    }

    @Bean 
    @Primary
    public CategoryRepository categoryRepositoryBean() {
        return Mockito.mock(CategoryRepository.class);
    }

    @Bean 
    @Primary
    public UserRepository userRepositoryBean() {
        return Mockito.mock(UserRepository.class);
    }
}

Also, I got an error saying that the type is on, so I added @Primary.

Why is this happening ...? ??

Recommended Posts

[Java] [Spring] Spring Boot Dependency injection mysterious hamarineta
About Spring Dependency Injection using Java, Kotlin
[Java] Thymeleaf Basic (Spring Boot)
CICS-Run Java application-(4) Spring Boot application
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Spring Boot + Java + GitHub authentication login
Elastic Beanstalk (Java) + Spring Boot + https
Java --Jersey Framework vs Spring Boot
[Java] LINE integration with Spring Boot
Spring Java
Create Java Spring Boot project in IntelliJ
Spring Boot + Docker Java development environment construction
Spring injection various
Challenge Spring Boot
[Java / Spring Boot] Spring security ④ --Implementation of login process
Java tips-Create a Spring Boot project in Gradle
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Spring Boot Form
[Java] Article to add validation with Spring Boot 2.3.1.
Spring Boot Memorandum
gae + spring boot
LINE Bot x Java (Spring Boot) construction procedure
[Java] Spring DI ③
Create a portfolio app using Java and Spring Boot
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
[Java] How to omit spring constructor injection with Lombok
Try Dependency Inversion Principle with Multiple Spring Boot Projects
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Dagger2 --Android Dependency Injection
Java table expression injection
Download with Spring Boot
Change the injection target for each environment with Spring Boot 2
Handle Java 8 date and time API with Thymeleaf with Spring Boot
[Java] Deploy the Spring Boot application to Azure App Service
How to call and use API in Java (Spring Boot)
Introducing Spring Boot2, a Java framework for web development (for beginners)
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
[Java] Sample project for developing web applications with Spring Boot