java - Junit 流卡在正在运行的嵌入式服务器上
问题描述
我创建了一个小应用程序(RestApi),它基本上包含一个嵌入式灰熊服务器。现在我想测试功能,为此我正在使用 Junit。
在测试类中,我使用@BeforeClass 运行嵌入式服务器,使用@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
}
推荐阅读
- mysql - 仅使用 1 个语句从 2 个具有条件的表创建列表
- html - 如何防止 Angular 中的选项卡事件?
- reactjs - 阻止反应路线
- python - 使用脚本将随机图像放入一张合成图像中
- javascript - 有没有办法在 Sum_Total 为零时禁用此链接并在它大于零时启用?
- android - 加载 HLS 播放列表时 Android WebView 崩溃
- python - 如何为管道难题实施 AI python 解决方案
- tableau-api - 在 Tableau 中计算两个日期的两个百分比之间的百分比差异
- assembly - 我需要对一个字符串(AZ)进行桶排序并在汇编中打印它
- java - 如何在服务上获取 websocket 会话?