Can't use EntityManagerFactory twice for a database in test environment

84 views Asked by At

For test purposes I want to populate the category table with test data. Which works fine. But when I want to populate another table named products then an error occurs.

And it's because of the following line (I discovered it by uncommenting the following line):

EntityManagerFactory emf = Persistence.createEntityManagerFactory("snel-transport-test");

Here are the logs:

Tests in error:
  testAddOrder(controller.OrdersControllerTest):

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.851s
[INFO] Finished at: Wed Jan 04 21:05:53 CET 2017
[INFO] Final Memory: 8M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project snel-transport: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Users\yomac_000\Documents\documenten z\werk\cimsolutions\java_ee\snel-transport\back_end\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project snel-transport: There are test failures.

Please refer to C:\Users\yomac_000\Documents\documenten z\werk\cimsolutions\java_ee\snel-transport\back_end\target\surefire-reports for the individual test results.
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures.

Please refer to C:\Users\yomac_000\Documents\documenten z\werk\cimsolutions\java_ee\snel-transport\back_end\target\surefire-reports for the individual test results.
        at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution(SurefireHelper.java:87)
        at org.apache.maven.plugin.surefire.SurefirePlugin.writeSummary(SurefirePlugin.java:641)
        at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary(SurefirePlugin.java:615)
        at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:137)
        at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:98)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        ... 19 more
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

The logs don't say much so I will show you the code. OrdersControllerTest.java:

public class OrdersControllerTest {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("snel-transport");
    StatusFacade statusFacade = new StatusFacade();
    CustomerFacade customerFacade = new CustomerFacade();
    ProductFacade productFacade = new ProductFacade();
    OrdersFacade orderFacade = new OrdersFacade();
    CategoryFacade categoryFacade = new CategoryFacade();
    String orderURL = "http://localhost:9090/snel-transport/api/orders";
    Orders foundOrder = new Orders();

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {}

    @AfterClass
    public static void tearDownAfterClass() throws Exception {}

    @Before
    public void setUp() throws Exception {}

    @After
    public void tearDown() throws Exception {}

    public void insertCategories(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("snel-transport-test");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        Query q = em.createNativeQuery("INSERT INTO Category (Name, categoryId) " +
                " VALUES(?,?)");
        q.setParameter(1, "Games");
        q.setParameter(2, 1L);
        q.executeUpdate();

        tx.commit();
    }

    public void insertProducts(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("snel-transport-test");
        System.out.println("can't execute the above line of code");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        Query q = em.createNativeQuery("INSERT INTO Product(id, code) " +
                " VALUES(?,?)");
        q.setParameter(1, 1);
        q.setParameter(2, "002");
        q.executeUpdate();

        tx.commit();
    }

    @Test
    public void testAddOrder() {
        insertCategories();
        insertProducts();

      //More code..
    }
}

I don't want to make this post too long. So below are the links to the other files in case you want to see it:

persistence.xml link: http://pastebin.com/7hPFtYqE

Category.java link: http://pastebin.com/b74QfV0e

Product.java link: http://pastebin.com/5Fuz9JrZ

Further I have already tried to populate the test database through the persistence.xml file through a SQL script in a .sql file. For that I have followed this tutorial link: https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm

But unfortunately that didn't worked out.

I have also tried to populate the test database through the setUpBeforeClass() method in the OrdersControllerTest.java file. But that also didn't worked out.

0

There are 0 answers