1
我有三個測試的setUp()
方法幾乎相同的代碼。當單獨運行時,測試都可以正常工作,但是當我將它們作爲完整測試集的一部分運行時,最後一個測試失敗。爲什麼第三次執行此代碼時運行JUnit測試時出現錯誤?
「攻擊」 的代碼是:
@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();
}
堆棧跟蹤爲:
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.<init>(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
我使用Maven的依賴關係,並有依賴性com.h2database.h2,1.3版本.168在我的classpath中。正如前面提到的,它在前兩次測試中工作正常,只是在這裏失敗。
任何想法?
乾杯提前
編輯:
這是我tearDown
這個測試:
@After
public void tearDown() throws Exception {
try {
sessionFactory.getCurrentSession().getTransaction().rollback();
} catch (Exception e) {
//
}
sessionFactory.close();
}
我註釋掉其他兩個測試,這其中仍然失敗,但是隻有當它作爲衆多其中之一運行時,它才能運行在自己身上。
編輯2:
我設法讓過去的一些驅動程序問題,當我註釋掉行:
configuration.setProperty("hibernate.current_session_context_class", "org.hibernate.context.ThreadLocalSessionContext");
不知道它這是一個紅色的鯡魚,但是出於測試,有沒有更好的類用於上下文類?
你是否正確拆除? –
不確定,但我在事務上有一個簡單的回滾:sessionFactory.getCurrentSession()。getTransaction()。rollback();我試過關閉會議,但沒有。第二種情況的作用也很奇怪,而且只發生在第三次測試中。推薦的銷燬方法是什麼?我認爲這是一個簡單的session.close()? – theZenPebble
是的,就像馬特說的那樣,我也曾經有過這些奇怪的錯誤之一,是測試拆解時的問題。仔細檢查它,因爲它的錯誤是一種痛苦。 – Scorpio