在JUnit 6中实现sniffy的几种方式
在文档中描述的使用JUnit进行连接测试的设置时,我遇到了“@Rule无法解析为类型”的错误,而且据我在网上所了解,@Rule已不再是JUnit 5或 6的特性。我想知道是否有办法为不再包含 @Rule的新版JUnit实现sniffy的 JUnit测试?
package edu.group5.app.control;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.sniffy.socket.DisableSockets;
import io.sniffy.test.junit.SniffyRule;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class OrgAPIWrapperTest {
String testURL;
String wrongURL;
@BeforeEach
void init() {
testURL = "https://app.innsamlingskontrollen.no/api/public/v1/all";
wrongURL = "This is not a URL";
}
@Rule public SniffyRule sniffyRule = new SniffyRule();
@Test
@DisableSockets
public void noConnectionReturnsFalseImport() {
// Insert code to be tested with no internet here
}
}
解决方案
不难收集一些基本事实。
https://mvnrepository.com/artifact/io.sniffy/sniffy-junit/3.1.14/dependencies —— 这是Maven上自2022年以来的最新版,链接指向JUnit 4。
https://github.com/sniffy/sniffy/commits/develop/ GitHub上最近一次提交是在6 个月前。看起来正在准备一个新版本,但进展就此停滞。通常在所有提交都来自一位活跃维护者……以及Copilot时,这种情况并不少见。
这个项目长期停滞,现在在我看来几乎死气沉沉。
你可以尝试把JUnit Vintage引擎添加到你的项目中,这样即使你正在使用JUnit 5或 6,也能继续支持使用JUnit 4 API的测试;然后你可以保持Sniffy的测试不变。但是,这应该只是一个临时措施,在你寻找Sniffy的替代方案期间使用。
https://www.baeldung.com/java-junit-vintage-engine-vs-junit-jupiter-engine
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。