gpt4 book ai didi

java - 为什么在第三次执行此代码时运行 JUnit 测试时会出现错误?

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:05 24 4
gpt4 key购买 nike

我在三个测试的 setUp() 方法中拥有几乎相同的代码。当单独运行时,测试全部工作,但是当我将它们作为完整测试集的一部分运行时,最后一个测试失败。

“有问题的”代码是:

@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)。正如之前提到的,在前两次测试中它运行良好,只是在这里失败了。

有什么想法吗?

提前干杯

编辑:

这是我针对此测试的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");

不确定这是一个转移注意力的问题,但是为了测试,是否有更好的类可用于上下文类?

最佳答案

不确定您在其他两个测试中做了什么,我只能推测以下原因

第二个测试中可能有某种方法使 session 不可用,这可能是由于您在第一个测试中插入的一些数据引起的。由于它单独运行良好,所以第二个测试中的某些持久方法失败(因为主键之类的东西已经存在),这会弄乱 session 状态,并且无法正确捕获异常以在控制台中显示。

关于java - 为什么在第三次执行此代码时运行 JUnit 测试时会出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13115729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com