0

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.

superkytoz
  • 1,267
  • 4
  • 23
  • 43
  • 1
    This question is nearly impossible to answer without the actual exception that is causing the tests to fail. All you're presenting right now is console out put stating that "stuff failed" and the code. Have you done any investigation at all into the root of the problem, say looking in those files the output refers to for the details, or simply running one test from an IDE to see what happens? – Gimby Jan 04 '17 at 21:39
  • @Gimby it works in an IDE but how!? Before this I was using Tomcat embedded and JUnit through the console. – superkytoz Jan 04 '17 at 21:59
  • @Gimby ah ofcourse I think I already know why.. before using Tomcat embedded 7. I was using Tomcat 8.5 on my IDE. So that could be the reason. – superkytoz Jan 04 '17 at 22:02
  • The fact that it seems to work from the IDE makes it difficult, but its not necessarily anything strange. For example Eclipse sets things up differently from a true Maven build so you get discrepancies. You should still be able to find an exception of some sort in the generated surefire reports that the console output refers too. – Gimby Jan 04 '17 at 22:19

0 Answers0