Spring Boot DataJpaTest fail with java.lang.IllegalStateException:Caused by: Given type must be an interface

后端 未结 1 1139
野的像风
野的像风 2020-12-22 05:22

To be precise depends on what error I have.

If I go with Intellij Maven Install, I get this exception (which is strange because I have this dependency and it should

相关标签:
1条回答
  • 2020-12-22 05:39

    You don't need @EnableAutoConfiguration for your @DataJpaTest, as the annotation is enabling every required part for testing this slice of the application:

    // ... and some more
    @BootstrapWith(DataJpaTestContextBootstrapper.class)
    @ExtendWith(SpringExtension.class)
    @OverrideAutoConfiguration(enabled = false)
    @TypeExcludeFilters(DataJpaTypeExcludeFilter.class)
    @Transactional
    @AutoConfigureCache
    @AutoConfigureDataJpa
    @AutoConfigureTestDatabase
    @AutoConfigureTestEntityManager
    @ImportAutoConfiguration
    public @interface DataJpaTest {
    
    }
    

    It's important to mention that with @DataJpaTest Spring will use an embedded in-memory database by default:

     * By default, tests annotated with {@code @DataJpaTest} are transactional and roll back
     * at the end of each test. They also use an embedded in-memory database (replacing any
     * explicit or usually auto-configured DataSource). The
     * {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} annotation can be used to
     * override these settings.
    

    That's why you see the H2 database output and not Oracle. By default, Spring should use spring.jpa.hibernate.ddl-auto=create-drop to take care that your tables are present.

    As you hardcode the schema inside your JPA entity with @Table(name = "MY_TABLE", schema = "MYSCHEMA", catalog = "") you have to ensure that the embedded H2 also uses this schema.

    First try to remove schema from the @Table annotation and see if it works. Then you can globally configure your schema inside your application.properties and use @TestPropertySource(properties = "spring.jpa.properties.hibernate.default_schema=PETRA") for your test.

    The following StackOverflow question might also help.

    0 讨论(0)
提交回复
热议问题