如何在单元测试期间忽略某些 JSON 和对象属性?

huangapple go评论78阅读模式
英文:

How can I ignore some JSON and object properties during unit testing?

问题

以下是要翻译的内容:

我有一个将旧的JSON结构迁移到新结构的方法,我想要测试,但有一个随机生成的属性(UUID),我想要忽略。

对象的有趣部分如下所示:

val expected = FooterContainer(id = UUID.fromString("11914145-9775-4675-9c65-54bbd369fb2c"), title = null, flowType=ContainerType.ROWS, elements=listOf() //the rest is not important.

要转换的JSON与此对象看起来完全不同,但这是该方法的目的 - 将其转换为新模型。

val json = """[{"type": "list", "items": [{"link": "www.something.com", "type": "link", "label": "Some label"},
{"type": "text", "label": "Another label"}, {"type": "text", "label": "Other label"},
{"type": "text", "label": "yet another label"}, {"type": "text", "label": "info@something.com"}]
等等。

下面的代码按预期工作:

val gson: JsonElement = Gson().fromJson(json, JsonElement::class.java)

// when
val converted = with(ClientDataRepository()) {
gson.readFooter()
}

直到我在FooterContainer对象中引入了一个新字段val id: UUID为止。

方法readFooter()会随机生成UUID,这是期望的行为。

但现在,我不能只将随机生成的(或像上面示例代码中的硬编码的)UUID分配给期望的类,这是合理的 - 两个随机生成的UUID永远不会相同。

当我运行测试时,我显然会得到:

Expected :FooterContainer(id=11914145-9775-4675-9c65-54bbd369fb2c,
Actual :FooterContainer(id=7ba39ad0-1412-4fda-a827-0241916f8558,

现在,是否有可能在这个测试中忽略id字段?从测试的角度来看,这不重要,但其余部分很重要。

英文:

I have a method that is migrating old JSON structure to a new one that I would like to test, but there is one randomly generated property (UUID) that I would like to ignore.

The interesting part of the object looks like this:

val expected = FooterContainer(id = UUID.fromString("11914145-9775-4675-9c65-54bbd369fb2c"), title = null, flowType=ContainerType.ROWS, elements=listOf() //the rest is not important.

The JSON to be converted looks totally different from this object, but this is the purpose of the method - to convert it to a new model.

val json = """[{"type": "list", "items": [{"link": "www.something.com", "type": "link", "label": "Some label"},
			{"type": "text", "label": "Another label"}, {"type": "text", "label": "Other label"},
			{"type": "text", "label": "yet another label"}, {"type": "text", "label": "info@something.com"}]

et cetera.

The below code worked as expected:

val gson: JsonElement = Gson().fromJson(json, JsonElement::class.java)

	// when
	val converted = with(ClientDataRepository()) {
		gson.readFooter()
	}

until I had to introduce a new field in FooterContainer object, which was val id: UUID

The method readFooter() is generating the UUID randomly, which is an expected behaviour.

But now, I cannot just assign a random (or hardcoded, as in the example code above) UUID to the expected class, which is logical - two randomly generated UUIDs will never be the same.

when I run the test, I obviously get:

Expected :FooterContainer(id=11914145-9775-4675-9c65-54bbd369fb2c,
Actual   :FooterContainer(id=7ba39ad0-1412-4fda-a827-0241916f8558,

Now, is there a possibility to ignore the id field for this test? It is not important from the test perspective, but the rest is.

答案1

得分: 1

You can simply copy the id field from the actual object to the expected:

val converted = with(ClientDataRepository()) {
    gson.readFooter()
}

val expected = FooterContainer(id = converted.id, title = null, flowType=ContainerType.ROWS, elements=listOf()) //the rest is not important.
Assertions.assertThat(converted).isEqualTo(expected);

If you are using assertJ, there is a straightforward solution:

Assertions.assertThat(converted).isEqualToIgnoringGivenFields(expected, "id")
英文:

You can simply copy the id field from the actual object to the expected:

val converted = with(ClientDataRepository()) {
    gson.readFooter()
}

val expected = FooterContainer(id = converted.id, title = null, flowType=ContainerType.ROWS, elements=listOf()) //the rest is not important.
Assertions.assertThat(converted).isEqualTo(expected);

If you are using assertJ, there is a stright forward solution:

Assertions.assertThat(converted).isEqualToIgnoringGivenFields(expected, "id")

答案2

得分: 1

尝试使用JsonUnit。在这种情况下,您可以按照以下方式修改您的代码:

assertThatJson("{\"id\":${json-unit.ignore}}").isEqualTo("{\"id\": 11914145-9775-4675-9c65-54bbd369fb2c}");
英文:

Try JsonUnit. In this case you can modify your code in next way:

assertThatJson("{\"id\":${json-unit.ignore}}").isEqualTo("{\"id\": 11914145-9775-4675-9c65-54bbd369fb2c}");

答案3

得分: 0

你可以使用类似于json-assert(https://github.com/skyscreamer/JSONassert)的工具,它支持忽略键 - https://stackoverflow.com/a/44328139/4473822

英文:

You can use something like json-assert (https://github.com/skyscreamer/JSONassert) that support ignoring keys - https://stackoverflow.com/a/44328139/4473822

huangapple
  • 本文由 发表于 2020年8月12日 16:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63372964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定