Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I get an error running JUnit tests on the 3rd time this code is executed?
    primarykey
    data
    text
    <p>I have almost identical code in the <code>setUp()</code> method of three tests. When run individually, the tests all work, but when I run them as part of the complete set of tests, the last one fails.</p> <p>The "offending" code is:</p> <pre><code>@Before public void setUp() throws Exception { AnnotationConfiguration configuration = new AnnotationConfiguration(); configuration.addAnnotatedClass(User.class); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem"); configuration.setProperty("hibernate.hbm2ddl.auto", "create"); configuration.setProperty("hibernate.current_session_context_class", "org.hibernate.context.ThreadLocalSessionContext"); sessionFactory = configuration.buildSessionFactory(); sessionFactory.openSession(); // This is where it dies: sessionFactory.getCurrentSession().beginTransaction(); User user = new User(); user.setUsername("emanymton"); user.setPassword(passwordEncoder.encodePassword("password", null)); user.setAccess(1); sessionFactory.getCurrentSession().save(user); sessionFactory.getCurrentSession().getTransaction().commit(); } </code></pre> <p>The stack trace is:</p> <pre><code>1 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - schema export unsuccessful ava.sql.SQLException: No suitable driver found for jdbc:h2:mem at java.sql.DriverManager.getConnection(DriverManager.java:604) at java.sql.DriverManager.getConnection(DriverManager.java:190) at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133) at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51) at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:252) at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:211) at org.hibernate.impl.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:383) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954) at [MYPROJECT].security.authentication.manager.CustomAuthenticationManagerTest.setUp(CustomAuthenticationManagerTest.java:73) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 7 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No suitable driver found for jdbc:h2:mem </code></pre> <p>I'm using Maven for dependencies, and have the dependency com.h2database.h2, version 1.3.168 in my classpath. As mentioned before, it works fine in the previous two tests, just fails here.</p> <p>Any ideas?</p> <p>Cheers in advance</p> <p><strong>EDIT:</strong></p> <p>This is my <code>tearDown</code> for this Test:</p> <pre><code>@After public void tearDown() throws Exception { try { sessionFactory.getCurrentSession().getTransaction().rollback(); } catch (Exception e) { // } sessionFactory.close(); } </code></pre> <p>I've commented out the other two tests, and this one still fails, but ONLY if it's run as one of many, it runs Fine wen run on it's own.</p> <p><strong>EDIT 2:</strong></p> <p>I managed to get past some of the Driver issue when I commented out the line:</p> <pre><code> configuration.setProperty("hibernate.current_session_context_class", "org.hibernate.context.ThreadLocalSessionContext"); </code></pre> <p>Not sure it this is a red herring, but for testing, is there a better class to use for the context class?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload