Home > Back-end >  NoSuchBeanException: expected at least 1 bean which qualifies as autowire candidate. But only on one
NoSuchBeanException: expected at least 1 bean which qualifies as autowire candidate. But only on one

Time:01-11

There's this weird problem.

I'm currently testing two repository classes that I plan to use as MockBean.

The snippets of the code are as follows:

For ClassARepoHibernate

@Repository
public class ClassARepoHibernate implements ClassARepo {

    @Autowired
    JdbcTemplate jdbcTemplate;
....

For ClassBRepoHibernate

@Repository
public class ClassBRepoHibernate implements ClassBRepo {

    @Autowired
    JdbcTemplate jdbcTemplate;
....

Now then, on the testing class, I put this up:

@SpringBootTest
public class unitTest{
    @MockBean
    ClassARepo repoA;

    @MockBean
    ClassBRepo repoB;

    @Test
    public void testing(){
        System.out.println("Start");//I put breakpoint here
        when (repoA.function(any())).thenReturn(null);
        when (repoB.function(any())).thenReturn(null);
        System.out.println("End");
    }

}

When I run this test, it gives out this error before even going to the breakpoint if I debug:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'ClassBRepoHibernate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

This confuses me because generally, both classes are similar in structure. But somehow, my project recognizes ClassARepo while not recognizing ClassBRepo. Like, if I just comment out everything about ClassBRepo, then the test works out.

Additionally, if I use an actual run on the application instead of testing, ClassBRepo works fine.

What goes wrong here? Here's my pom.xml for good measure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version><!-- lookup parent from repository -->
    </parent>
    <groupId>id.co.test</groupId>
    <artifactId>test/artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>Test Project</description>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.json</groupId>
                    <artifactId>json</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>19.8.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.13.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.13.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.13.3</version>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <finalName>test</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
                        <plugin>
                            <artifactId>maven-war-plugin</artifactId>
                            <configuration>
                                <failOnMissingWebXml>false</failOnMissingWebXml>
                                <packagingExcludes>**/*.properties</packagingExcludes>
                            </configuration>
                        </plugin>
                        
        </plugins>
    </build>

</project>

I've tried adding @Service and @Component on ClassBRepo. I studied the structure of both classes and found out that they are the same (same package, especially), hoping that it'll get past the NoSuchBeanDefinitionException part.

I also tried putting this line in application-test.properties:

spring.jpa.properties.hibernate.globally_quoted_identifiers=true

I still end up with the same result.

Thanks in advance for the help.

CodePudding user response:

You have a @MockBean for ClassBRepo but the error message is a NoSuchBeanDefinitionException for ClassBRepoHibernate.

I'm guessing somewhere in your code you are referencing the concrete class. Eg:

public class SomeOtherClass {
   @Autowired
   ClassBRepoHibernate repoB;
   ...
}

This will need to change to the interface. Eg:

public class SomeOtherClass {
   @Autowired
   ClassBRepo repoB;
   ...
}
  • Related