gpt4 book ai didi

java - Spring Junit org.h2.jdbc.JdbcSQLException : Exception opening port "9092" (port may be in use)

转载 作者:行者123 更新时间:2023-12-01 14:28:46 25 4
gpt4 key购买 nike

我的应用程序使用 Spring Boot、Hibernate、JUnit 构建。我使用 H2 数据库进行内存测试并创建了一个 bean 以便能够使用 SQL 工具连接到 H2 数据库。

@Configuration
public class H2DataBaseTestConfig {
@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}
}

如果我运行一个测试它工作正常,但是如果我运行所有测试,我会在第二个和所有下一个测试类中收到错误
Exception opening port "9092" (port may be in use)

这看起来像 Spring 尝试创建应用程序上下文并在每次下一个测试类运行时启动 bean,尽管这与它自己打开的端口有某种冲突。 我怎样才能运行所有测试并使这个 bean 工作?

更新

由于很多人提到测试类注释,我将在这里展示它们:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(IndeedScraper.class)
@TestPropertySource(locations="classpath:test.properties")

我有几个@Configuration 类:两个数据库连接/事务/实体管理类,这里描述了 h2Server bean。 h2Server bean builder类存放在src/test/java中,其他@Configuration类存放在src/main/java中

结束更新

这里完整的堆栈跟踪供引用:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'h2Server' defined in class path resource [healthjobs/scraper/H2DataBaseTestConfig.class]: Invocation of init method failed; nested exception is org.h2.jdbc.JdbcSQLException: Exception opening port "9092" (port may be in use), cause: "java.net.BindException: Address already in use (Bind failed)" [90061-192]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:760)
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 25 more
Caused by: org.h2.jdbc.JdbcSQLException: Exception opening port "9092" (port may be in use), cause: "java.net.BindException: Address already in use (Bind failed)" [90061-192]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.util.NetUtils.createServerSocketTry(NetUtils.java:199)
at org.h2.util.NetUtils.createServerSocket(NetUtils.java:165)
at org.h2.server.TcpServer.start(TcpServer.java:232)
at org.h2.tools.Server.start(Server.java:484)
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.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1706)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1645)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 40 more
Caused by: java.net.BindException: Address already in use (Bind failed)
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at org.h2.util.NetUtils.createServerSocketTry(NetUtils.java:195)
... 50 more

最佳答案

选项之一可以是在您的测试类之间共享 Spring 上下文,如本 integration test tutorial 中所述。并且还要注意 Spring documentation 中的上下文缓存描述

也就是说,要与单元测试类共享相同的运行时上下文,请尝试注释所有需要 H2 连接的单元测试,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
...
@ContextConfiguration(classes=H2DataBaseTestConfig.class, loader=AnnotationConfigContextLoader.class)
public class SomeServiceTest {
...

请注意,此方法不依赖 SpringBoot 集成测试支持,该支持在此线程的另一个答案中引用。

更新

由于看起来并不明显,您可能需要在其中列出所有其他必要的 @Configuration 注释类,例如 @ContextConfiguration(classes={H2Config.class, MyConfigFromSrcTest.class, MyConfigFromSrcMain.class, ...}, ...反射(reflect)集成测试类所需的所有上下文片段的超集。然后将通过使用这些配置类的确切集合作为缓存键来缓存结果上下文

关于java - Spring Junit org.h2.jdbc.JdbcSQLException : Exception opening port "9092" (port may be in use),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46236808/

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