Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I fix "Could not autowire. No beans of 'MyRepository' type found"?

I have this hierarchy in my project:

▼ server
  ▼ myproject
    ▼ src
      ▼ main
        ▼ java
          ▼ rest
            ▼ repository
              Ⓘ MyRepository
        ▶ resources
      ▼ test
        ▼ java
          ▼ rest
            Ⓒ MyRepositoryTest

This is the MyRepository interface:

public interface MyRepository extends MongoRepository<String, Integer> {
}

This is the MyRepositoryTest test class:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@ExtendWith(SpringExtension.class)
public class AdminEvaluatorTest {
    ...
    @Autowired MyRepository myRepository;
    ...
}

The error occurs on the autowired myRepository instance in the test class. It says Could not autowire. No beans of 'MyRepository' type found. I’ve searched a bit and tried to add @Component, @Repository and so on, but nothing really helps. How do I fix this issue?

>Solution :

You could try this, set your package in @ComponentScan of the class AppConfig:

@ContextConfiguration(classes = AppConfig.class)
@ExtendWith(SpringExtension.class)
public class AdminEvaluatorTest {
    @Autowired MyRepository myRepository;

    @Configuration
    @ComponentScan("com.<your-package>")
    public static class AppConfig {
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading