I have a JUnit test class written in spring boot which contains 3 unit tests:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@ContextConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class AppclaitionTest {
@BeforeClass
public static void initialize(){
System.out.println(">>>>>>>Intialization Part!");
}
@Test
public void test1(){
System.out.println(">>>>>>>Appcliation Test Started! - Test 1");
Application.main(new String[]{});
System.out.println(">>>>>>>Appcliation Test Ended! - Test 1");
}
@Test
public void test2(){
System.out.println(">>>>>>>Appcliation Test Started! - Test 2");
Application.main(new String[]{});
System.out.println(">>>>>>>Appcliation Test Ended! - Test 2");
}
@Test
public void test3(){
System.out.println(">>>>>>>Appcliation Test Started! - Test 3");
Application.main(new String[]{});
System.out.println(">>>>>>>Appcliation Test Ended! - Test 3");
}
@AfterClass
public static void destory(){
System.out.println(">>>>>>>Destroy Part!");
}
}
If run after the first test, i am getting below the exception:
java.net.BindException: Address already in use: bind
though the application context released, it is not releasing the port number, hence i'm getting the above exception.
Is there any way that I can close the port number before each unit test?