darrachequesne / spring-data-jpa-datatables

Spring Data JPA extension to work with the great jQuery plugin DataTables (https://datatables.net/)
Apache License 2.0
441 stars 174 forks source link

When testing I need to disable @EnableJpaRepositories #84

Closed sebasira closed 4 years ago

sebasira commented 5 years ago

Hello!

I'm learning how to test my code. In my first test, I was testing a simple dumb controller just to check it and I get this error

At least one JPA metamodel must be present!

I could solve it by disabling the @EnableJpaRepositories annotations (found over stack overflow)

The testing I'm doing is not of the full app, just a slice using @WebMvcTest. When testing with @SpringBootTest this problem is gone.

Do you know how can I successfully run my test with out disabling that annotation? Thank you!

darrachequesne commented 5 years ago

@sebasira sorry for the delay! Did you find a solution to your issue?

I think the error At least one JPA metamodel must be present! is due to the fact that Spring does not find any @Entity class (obviously!).

@EnableJpaRepositories looks for classes in its package (like com.test), unless its basePackageClasses attribute is overriden.

darrachequesne commented 4 years ago

Closed due to inactivity, please reopen if needed.

sebasira commented 4 years ago

For anyone in the same situation, I solve it by separating the configuration from the main class.

At first I had:

File App.java:

@SpringBootApplication(scanBasePackages = "ar.com.sebasira.*")
@EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class)
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

And then I create another file which I named AppConfig.java and move there the @EnableJpaRepositories annotation. So it became:

File App.java:

@SpringBootApplication(scanBasePackages = "ar.com.sebasira.*")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

File AppConfig.java


@Configuration
@EntityScan("ar.com.sebasira.*")
@ComponentScan(basePackages = { "ar.com.sebasira.*" })
@EnableJpaRepositories(basePackages = {"ar.com.sebasira.*"}, repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class)
public class AppConfig {
}