【问题标题】:An annotation argument must be a compile-time constant @RunWith(AndroidJUnit4::class)注释参数必须是编译时常量 @RunWith(AndroidJUnit4::class)
【发布时间】:2021-12-07 17:08:41
【问题描述】:

我的测试套件无法运行,它似乎是 @RunWith(AndroidJUnit4::class)。我正在学习一个教程,所以我不能 100% 确定那部分是做什么的,或者这意味着什么。

@RunWith(AndroidJUnit4::class) 错误是 An annotation argument must be a compile-time constant

@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    @get:Rule
    val activityRule = ActivityTestRule(MainActivity::class.java, true, false)

    private val mockWebServer = MockWebServer()

    @Before
    fun setup() {
        mockWebServer.start(8080)
        IdlingRegistry.getInstance().register(
            ServiceBuilder.getClient()?.let {
                OkHttp3IdlingResource.create(
                    "okhttp",
                    it
                )
            }
        )
    }

    @Test
    fun testSuccessfulResponse() {
        mockWebServer.dispatcher = object : Dispatcher() {
            override fun dispatch(request: RecordedRequest): MockResponse {
                return MockResponse()
                    .setResponseCode(200)
                    .setBody(readStringFromFile("success_response.json"))
            }

        }
        activityRule.launchActivity(null)

        onView(withId(R.id.progress_bar))
            .check(matches(withEffectiveVisibility(Visibility.GONE)))
        onView(withId(R.id.recyclerView))
            .check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
    }

    @Test
    fun testFailedResponse() {
        mockWebServer.dispatcher = object : Dispatcher() {
            override fun dispatch(request: RecordedRequest): MockResponse {
                return MockResponse().throttleBody(1024, 5, TimeUnit.SECONDS)
            }
        }

        activityRule.launchActivity(null)

        onView(withId(R.id.progress_bar))
            .check(matches(withEffectiveVisibility(Visibility.GONE)))
        onView(withId(R.id.recyclerView))
            .check(matches(withEffectiveVisibility(Visibility.GONE)))
    }

    @After
    fun teardown() {
        mockWebServer.shutdown()
    }
}

我正在尝试编写将从模拟服务器接收 json 的测试。

【问题讨论】:

    标签: kotlin junit4 android-espresso okhttp mockwebserver


    【解决方案1】:

    原来我需要将测试放入

    androidTest/java/com/example/appname 文件夹而不是 test/java/com/example/appname文件夹

    一旦我移动了所有东西,它就很好用!

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多