No bean named 'entityManagerFactory' available

Asked

Viewed 432 times

0

I am testing my Spring application with Junit, however the error occurs after placing the annotation @EnableJpaRepositories:

Caused by: org.springframework.Beans.factory.Nosuchbeandefinitionexception: No bean named 'entityManagerFactory' available

Configuration class:

@SpringBootApplication
@ComponentScan
@EnableJpaRepositories
public class ExampleApplication {

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

Removing this annotation returns the problem that does not find a dependency for the repository interface.

Obs: The application starts normally, this problem occurs when running the tests, with both Gradle and Junit.

1 answer

1

Answer:

The problem was in the test, needs the note @MockBean on top of an object that is a class being a service or repository, that is, it is necessary to mock.

Example:

@MockBean
private UserService userService;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.