Java Junit流在运行嵌入式服务器时卡住

Java Junit流在运行嵌入式服务器时卡住,java,rest,junit,Java,Rest,Junit,我创建了一个小应用程序RestApi,它基本上包含一个嵌入式grizzly服务器。现在我想测试功能,为此我使用Junit 在测试类中,我使用@BeforeClass运行嵌入式服务器,使用@test测试功能。在运行test类时,我可以看到服务器正在启动,但是流被卡住了,无法到达带有@test注释的方法 Test.java public class MyTest { @BeforeClass public static void init() { try { MyAppli

我创建了一个小应用程序RestApi,它基本上包含一个嵌入式grizzly服务器。现在我想测试功能,为此我使用Junit

在测试类中,我使用@BeforeClass运行嵌入式服务器,使用@test测试功能。在运行test类时,我可以看到服务器正在启动,但是流被卡住了,无法到达带有@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.
}
当我停止服务器时,流到达测试方法并出错,连接被拒绝异常


注意:当使用PostMan进行测试时,应用程序运行得非常好。

您可能需要在单独的线程中启动grizzly服务器,这样它就不会用测试阻塞主线程

你可以这样做

@BeforeClass
public static void setUp() throws Exception {
    new Thread(() -> {
        try {
            MyApplication.grizzlyServerSetup();
        } catch (IOException e) {
            e.printStackTrace();
        }).run();
}
您可能还需要一个拆卸方法来停止grizzly服务器

@AfterClass
public static void tearDown() throws Exception {
    //whatever stuff you need to do to stop it
}

这是一个基本的错误,不知道为什么它没有点击我。无论如何,谢谢!