【发布时间】:2018-02-21 05:29:10
【问题描述】:
我的项目有一个 JUnit 测试用例,它是使用 SpringBoot (1.3.5) & JDK 8. 当我在 STS 中将我的项目作为 JUnit 测试运行时,所有 测试用例通过,但在每个测试用例结束时给出错误,这 是:我正在尝试运行 JUnit Coverage:
ERROR [ main] o.a.coyote.http11.Http11NioProtocol o.a.j.l.DirectJDKLog.log(DirectJDKLog.java:182) - |||||||||Failed to start end point associated with ProtocolHandler ["http-nio-8080"]
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340)
at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:773)
at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473)
at org.apache.catalina.connector.Connector.startInternal(Connector.java:986)
........
........
........
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:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
ERROR [ main] o.apache.catalina.core.StandardService o.a.j.l.DirectJDKLog.log(DirectJDKLog.java:182) - |||||||||Failed to start connector [Connector[HTTP/1.1-8080]]
org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8080]]
此错误显示为端口 8080 已在使用中。如何避免这种情况 错误并让我的所有 20 个测试用例在没有这个的情况下在一个流程中运行 错误?
我的代码是:--->
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(KYCNotificationApplication.class)
@ActiveProfiles("local")
@WebIntegrationTest
@IntegrationTest({"server.port=0"})
public class DetermineKYCNotificationServiceImplTest {
@InjectMocks
DetermineKYCNotificationServiceImpl kycService;
@Mock
KYCOperationsDao kycOperationsDao;
@Mock
HttpServletRequest httpServletRequest;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
...
}
@Test
public void testMeth1() {
try {
.....
assertTrue(......);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testMeth2() {
try {
.....
assertTrue(......);
} catch (Exception e) {
e.printStackTrace();
}
}
...2o 多个测试用例....
【问题讨论】:
标签: spring tomcat spring-boot junit