2017-09-15 64 views
1

我的應用程序使用Spring Boot,Hibernate,JUnit構建。我使用H2數據庫進行內存測試,並創建了一個能夠使用SQL工具連接到H2數據庫的bean。Spring Junit org.h2.jdbc.JdbcSQLException:異常打開端口「9092」(端口可能正在使用)

@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) 

這看起來像春天試圖創建應用程序上下文和錯誤每次下一個測試類將運行時啓動bean,儘管這與本身打開的端口以某種方式衝突。 我如何運行所有測試並讓這個bean工作?

更新

由於很多人提到的測試類註釋,我將在這裏展示他們:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(IndeedScraper.class) 
@TestPropertySource(locations="classpath:test.properties") 

我有幾個@Configuration類:兩臺數據庫連接/交易/實體管理類和在這裏描述h2Server bean。 h2Server豆生成器類被存儲在SRC /測試/ JAVA,而在SRC /主/ JAVA其他@Configuration類

結束更新

這裏完整的堆棧爲基準軌跡:

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 
+0

您可以使用@Autowire代替爲每個測試用例創建bean。 – Nidhi257

+0

爲什麼你這樣做'Server.createTcpServer'有特定的原因嗎? Springboot自動連接到h2數據庫 – pvpkiran

+0

@ Nidhi257你能解釋一下你的意思嗎? Autowire如何防止創建bean? – Pavlo

回答

1

其中一個選項就可以按照本integration test tutorial描述也要注意上下文緩存描述Spring documentation

你的測試類之間共享Spring上下文

也就是說,共享相同的運行時環境,以單元測試類,嘗試註釋要求所有的H2連接你的單元測試如下:

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

注意,這種方法不依賴於SpringBoot整合測試支持在另一個答案中提到在這個線程中。

更新

,因爲它似乎不是很明顯,你可能需要列出在那裏,像@ContextConfiguration(classes={H2Config.class, MyConfigFromSrcTest.class, MyConfigFromSrcMain.class, ...}, ...所有其他必要的@配置註解類,以反映由您的集成測試類所需的所有背景件超集。然後通過使用這些配置類的精確集合緩存結果上下文作爲緩存密鑰

+0

這是否意味着H2DataBaseTestConfig類bean將在每個測試類開始之前加載?如果是這樣的話,我已經有了這個,bean在每個測試類的運行中實例化。或者這將在所有測試類之間共享H2DataBaseTestConfig?我應該在H2DataBaseTestConfig類名附近刪除配置註釋嗎? – Pavlo

+0

保留你的'@ Configuration'註解。只要添加'@ ContextConfiguration'如上所述,這將迫使Spring緩存並重新使用配置了相同'H2DataBaseTestConfig'的上下文,而不是每次執行新的單元測試類時創建上下文 – Kostiantyn

+0

當我添加@ContextConfiguration我的其他自動佈線bean測試類不再工作 - org.springframework.beans.factory.BeanCreationException。查看我的問題更新。 – Pavlo

1
@Before 
public void setUp() { 
    createTCPConnection(); 
} 

@After 
public void endTest() { 
    closeTCPConnection(); 
} 

@Test 
public void firstTest() { 
    doTest(1); 
} 

@Test 
public void secondTest() { 
    doTest(2); 
} 

然後,所有正在運行的測試都會在之前和之後調用。因此,連接它的創建,一旦該類和它所有的測試方法

+0

我沒有檢查過,因爲發現了帶有測試上下文緩存的問題解決方案。儘管看起來你的想法也可以工作。謝謝。 – Pavlo