出于测试目的,我想用category
表填充测试数据。哪个工作正常。
但是当我想填充另一个名为products
的表时,会发生错误。
这是因为以下行(我通过取消注释以下行发现它):
EntityManagerFactory emf = Persistence.createEntityManagerFactory("snel-transport-test");
以下是日志:
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
日志没有说太多,所以我会告诉你代码。 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..
}
}
我不想让这篇文章太长。所以下面是其他文件的链接,以防你想看到它:
persistence.xml链接:http://pastebin.com/7hPFtYqE
Category.java链接:http://pastebin.com/b74QfV0e
Product.java链接:http://pastebin.com/5Fuz9JrZ
此外,我已经尝试通过persistence.xml
文件中的.sql
文件填充测试数据库。为了那个原因
我已按照本教程链接:https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm
但不幸的是,这没有成功。
我还尝试通过setUpBeforeClass()
文件中的OrdersControllerTest.java
方法填充测试数据库。但这也没有成功。