0

I have a SpringBoot 2.0 application, and I've decided to mix annotations and XML configuration using orm.xml. So, I've put orm.xml (JPA file with XML configuration) in resources/META-INF, and, when starting the application, it's taken into account and everything is good, it works like a charm.

However, when running integration tests (which use an in-memory database), orm.xml seems to be completely ignored, as the test fails with an exception related to a missing mapping, mapping which I've written in orm.xml (an embeddable entity).

Here's how a test looks like:

package hello;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@DataJpaTest
public class ApplicationTests {

    @Test
    public void contextLoads() {
    }

}

I've also tried @AutoConfigureTestDatabase instead of @DataJpaTest, but with no success.

What should I change, in order for the integration test to load orm.xml?

Vlad Dinulescu
  • 1,173
  • 1
  • 14
  • 24
  • is it in `persistence.xml`? or whatever excuse for a "standard" Spring use? –  Feb 21 '18 at 07:21
  • Check this .... https://stackoverflow.com/questions/32536200/spring-boot-add-hibernate-mapping-file-to-entity-manager – osama yaccoub Feb 21 '18 at 13:08

2 Answers2

0

I had to migrate some integration tests that used an in-memory database to SpringBoot2 and in order to have access to the "orm.xml" file in the tests I just copied it from "src/main/resources/META-INF/orm.xml" to "src/test/resources/META-INF/orm.xml".

Ungureanu Adrian
  • 145
  • 5
  • 18
-1

I was using spring with junit (not spring boot) and I used to annotate the test class like this :

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "appContextTest.xml" })

and inside the appContextTest.xml , I have the import :

<import resource="classpath:config-test.xml" />

EDIT : the file contents are like follows (it has the Datasource definition and hibernate properties :

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
    <property name="URL" value="jdbc:oracle:thin:@127.0.0.1:1521:serv"/>
    <property name="user" value="us"/>
    <property name="password" value="pass"/>
</bean>
.

.

.
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> 
        </props>
    </property>

` ... I don't know if that helps or no

osama yaccoub
  • 1,884
  • 2
  • 17
  • 47
  • I've tried this, but it doesn't work since the tag imports XML bean definitions, while orm.xml is a JPA file, and therefore unrelated to Spring. – Vlad Dinulescu Feb 21 '18 at 12:40