【问题标题】:JUnit test can't create an AccountJUnit 测试无法创建帐户
【发布时间】:2019-03-11 16:46:10
【问题描述】:

我正在尝试在我的应用程序中运行一些测试,但是当我尝试在 JUnit 中创建 Account(name, type) 时,结果始终是 null,但在我运行应用程序时工作正常

示例:

class ExampleTest {

@Rule
@JvmField
val instantExecutorRule = InstantTaskExecutorRule()
@Rule
@JvmField
val trampolineSchedulerRule = TrampolineSchedulerRule()

val ACCOUNT_NAME = "ACCOUNT_NAME"
val ACCOUNT_TYPE = "ACCOUNT_TYPE"

@Test
fun createAccountTest() {
    val account = Account(ACCOUNT_NAME, ACCOUNT_TYPE)

    assertThat(account.name, `is`(ACCOUNT_NAME))
}
}

在我运行测试时有什么特殊的方法可以创建帐户吗?

【问题讨论】:

    标签: android unit-testing junit4 account


    【解决方案1】:

    我认为您的问题是您试图在 test 目录中执行此类测试,这是用于单元测试的目录。

    在这种情况下,您正在访问一个框架类,因此您需要将测试放在您的 androidTest 目录中,其中驻留有检测的测试。

    在我的环境中,这个测试是绿色的:

    import org.junit.Assert.assertEquals
    import org.junit.Assert.assertNotEquals
    
    @RunWith(AndroidJUnit4::class)
    class AccountTest {
    
        @Test
        fun accountCreationTest() {
            val account = Account("NAME", "TYPE")
            assertEquals(account.name, "NAME")
            assertNotEquals(account.name, "WRONGNAME")
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2020-10-14
      • 1970-01-01
      • 2020-10-15
      • 2015-08-02
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多