Unit testing 迭代Kotlin TestCase中的对象列表

Unit testing 迭代Kotlin TestCase中的对象列表,unit-testing,kotlin,Unit Testing,Kotlin,我正在使用Kotlin进行单元测试,无法迭代测试用例中的对象列表,请检查我下面的编码 @Test @WithMockOAuth(siteId = "5698965", subPermissions = [SubPermission.GETD]) fun `get fee zero`() { val body = """ { "newMessage": { "call": true, "callMessatgeCount":

我正在使用Kotlin进行单元测试,无法迭代测试用例中的对象列表,请检查我下面的编码

@Test
  @WithMockOAuth(siteId = "5698965", subPermissions = [SubPermission.GETD])
  fun `get fee zero`() {

    val body = """
      {
      "newMessage": {
        "call": true,
        "callMessatgeCount": 3,
        "discounted": 2,
        "NewFees": 4.99,
        "Id" : "extra SIM Business"
      }
    }
      """.trimIndent()

    this.server.expect(requestTo("${integrationClientProperties.url}/main/0767777777/register/"))
      .andRespond(withSuccess(body, MediaType.APPLICATION_JSON_UTF8))

    assertThat(service.getValues("0767777777"))
      .hasSize(3)
      .first()
      .hasFieldOrPropertyWithValue("callMessatgeCount", 3)
      .hasFieldOrPropertyWithValue("NewFees", BigDecimal.ZERO)

    this.server.verify()
  }
上面我可以检查
first()
元素的
hasfield或propertyWithValue
,因为
hasSize(3)
我需要在相同的
TestCase
方法中检查对象列表中的所有3个值

对象列表如下所示

ListValue:[
{
      "newMessage": {
        "call": true,
        "callMessatgeCount": 3,
        "discounted": 2,
        "NewFees": 4.99,
        "Id" : "extra SIM Business"
      },
{
      "newMessage": {
        "call": true,
        "callMessatgeCount": 3,
        "discounted": 2,
        "NewFees": 0,
        "Id" : "extra SIM Business"
      },
{
      "newMessage": {
        "call": true,
        "callMessatgeCount": 3,
        "discounted": 2,
        "NewFees": 4.99,
        "Id" : "extra SIM Business"
      }
]
注意:我尝试了
element(index)
来检查使用多个测试用例的对象列表

更新

库“
org.assertj.core.api.Assertions
并支持
java8
假设您使用的是方法名称中的assertj,并且您有一个支持Java-8的版本(即),您可以找到:

验证所有元素是否满足以使用者表示的给定需求

这对于对元素执行一组断言非常有用

从文档中,类似于以下内容的内容应该可以工作

assertThat(service.getValues("0767777777"))
  .hasSize(3)
  .allMatch { assertThat(it)
      .hasFieldOrPropertyWithValue("callMessatgeCount", 3)
      .hasFieldOrPropertyWithValue("NewFees", BigDecimal.ZERO)
  }

您还可以查看特定于Kotlin的库(特别是如果您需要编译到JVM 6)。

假设您使用的是方法名称中的AssertJ,并且您有支持Java-8的版本(即),您可以找到:

验证所有元素是否满足以使用者表示的给定需求

这对于对元素执行一组断言非常有用

从文档中,类似于以下内容的内容应该可以工作

assertThat(service.getValues("0767777777"))
  .hasSize(3)
  .allMatch { assertThat(it)
      .hasFieldOrPropertyWithValue("callMessatgeCount", 3)
      .hasFieldOrPropertyWithValue("NewFees", BigDecimal.ZERO)
  }

您还可以查看特定于Kotlin的库(特别是当您需要编译到JVM 6时).

未来:提及您使用的库!正如您所说,我正在使用asserJ支持java8,并且我通过使用tuple解决了这个问题。感谢未来:提及您使用的库!正如您所说,我正在使用asserJ支持java8,并且我通过使用tuple解决了这个问题。谢谢