JUnit test case error: "Address already in use: bind"

Viewed 8861

I have a JUnit test case for my Project which is developed using SpringBoot (1.3.5) & JDK 8. When I run my project as JUnit test in STS, all test cases pass, but gives error at the end of each test cases, which is: I am trying to run 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]]

This error shows as the port 8080 is already in use. How to avoid this error and let my all 20 test cases to run in one flow without this error?

My code is:--->

@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 more test cases....

1 Answers
Related