4

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:

@ExtendWith(SpringExtension.class)
public class MyRepositoryTest {
    ...
    @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?

TheStranger
  • 1,387
  • 1
  • 13
  • 35

4 Answers4

3

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 {
    }
}
MevlütÖzdemir
  • 3,180
  • 1
  • 23
  • 28
  • The error I had is gone, however when I run the test now it gives that says `java.lang.IllegalStateException: Failed to load ApplicationContext` – TheStranger Feb 15 '22 at 09:56
  • Is it working right now ? @Ben – MevlütÖzdemir Feb 15 '22 at 10:50
  • No it's not working. The @ContectConfiguration just moved the problem somewhere else, now I just have another issue: https://stackoverflow.com/questions/71124665/why-do-i-keep-getting-the-failed-to-load-applicationcontext-exception?noredirect=1#comment125727713_71124665 – TheStranger Feb 15 '22 at 10:59
1

you can try to specify the base packages that should be included in the component scan. For example:

@SpringBootApplication(scanBasePackages = {"com.example.demo", "com.example.model"})

the annotation is indicating that the component scan should include the com.example.demo and com.example.model packages. This means that Spring will search for and register any Spring-managed components (such as @Controller, @Service, and @Repository beans) in those packages, and make them available for dependency injection.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0

Do you have @EnableAutoConfiguration under your @SpringBootApplication at the main class? Try to set it.

@SpringBootApplication
@EnableAutoConfiguration
....
public class MyProject{

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

}
Luis Manrique
  • 308
  • 3
  • 15
0

Looks like framework is not scanning package which have test class in your application

From hierarchy shared above, package for MyRepository interface is "rest.repository" and package for MyRepositoryTest class is "rest".

Try changing package of MyRepositoryTest class as well to "rest.repository". This should solve the problem.

Selvakumar K
  • 151
  • 1
  • 5