Issue
I have created a small application (RestApi) which basically contains an embedded grizzly server. Now I want to test the functionality and for this I am using Junit.
In the test class I am using @BeforeClass for running the embedded server and the @Test for testing the functionality. On running the test class I can see the server getting started but the flow is getting stuck and not reaching method annotated with the @Test.
Test.java
public class MyTest {
@BeforeClass
public static void init() {
try {
MyApplication.grizzlyServerSetup();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testCreateNewBankAccount() {
// test some functionality.
}
And when I am stopping the server the flow reaches the test method and errors out with Connection Refused exception.
Note: Application works perfectly fine when tested with PostMan.
Solution
You may need to start grizzly server in a separate thread so that it will not block your main thread with tests.
You can do something like this
@BeforeClass
public static void setUp() throws Exception {
new Thread(() -> {
try {
MyApplication.grizzlyServerSetup();
} catch (IOException e) {
e.printStackTrace();
}).run();
}
You may also need a tear down method to stop grizzly server
@AfterClass
public static void tearDown() throws Exception {
//whatever stuff you need to do to stop it
}
Answered By - Ivan