gpt4 book ai didi

java - 扩展 AndroidTestCase 类时 JUnit 4 注释不起作用

转载 作者:行者123 更新时间:2023-11-30 01:23:40 24 4
gpt4 key购买 nike

你好,我正在为我的 sqlite 数据库做测试部分。我以前使用过 JUnit,但我了解到我必须扩展 AndroidTestCase 才能获得模拟上下文。但是,当我 try catch 异常时(如在 JUnit 中),编译器会报错。这是为什么,因为 Android 现在附带 JUnit 4?

引用类示例:

public class MyTest extends AndroidTestCase {

@Test(expected=NullPointerException.class) //<-- this does not work
public void testRandomMethod() {
callRandomMethod() //Throws NullPointerException
}
}

最佳答案

  1. 删除AndroidTestCase
  2. 使用@Runwith(AndroidJunit4.class)

对于上下文,使用 InstrumentationRegistry.getTargetContext()

关于java - 扩展 AndroidTestCase 类时 JUnit 4 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36737489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com