单元测试JSON输出模块,最佳实践

时间:2021-01-03 19:38:59

I am currently working on a module that takes one of our business objects and returns a json representation of that object to the caller. Due to limitations in our environment I am unable to use any existing json writer, so I have written my own, which is then used by the business object writer to serialize my objects. The json writer is tested in a way similar to this

我目前正在研究一个模块,它接受我们的业务对象之一并将该对象的json表示返回给调用者。由于我们环境的限制,我无法使用任何现有的json编写器,因此我编写了自己的json编写器,然后业务对象编写器使用它来序列化我的对象。 json编写器以类似于此的方式进行测试

@Test
public void writeEmptyArrayTest() 
{
   String expected = "[  ]";
   writer.array().endArray();
   assertEquals(expected, writer.toString());
}

which is only manageable because of the small output each instruction produces, even though I keep feeling there must be a better way.

这是唯一可管理的,因为每条指令产生的输出很小,即使我一直觉得必须有更好的方法。

The problem I am now facing is writing tests for the object writer module, where the output is much larger and much less manageable. The risk of spelling mistakes in the expected strings mucking up my tests seem too great, and writing code in this fashion seems both silly and unmanageable in a long term perspective. I keep feeling like I want to write tests to ensure that my tests are behaving correctly, and this feeling worries me.

我现在面临的问题是为对象编写器模块编写测试,其中输出更大,更易于管理。在预期的字符串中拼写错误的风险使我的测试变得非常糟糕,从长远来看,以这种方式编写代码似乎既愚蠢又无法管理。我一直觉得我想写测试以确保我的测试表现正常,这种感觉让我担心。

What is a better way of doing this?

这样做的更好方法是什么?

2 个解决方案

#1


5  

Technically the environment in which you are deploying your code is not the same as the environment in which you are developing it so I would use an existing JSON reader/writer to test the one you have created. If you are using maven, you can even set the scope of the JSON package you choose to use to be "test" so it will not be included in the actual build.

从技术上讲,部署代码的环境与开发代码的环境不同,因此我将使用现有的JSON读取器/编写器来测试您创建的代码。如果您使用的是maven,您甚至可以将您选择使用的JSON包的范围设置为“test”,这样它就不会包含在实际构建中。

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20090911</version>
  <scope>test</scope>
</dependency>

Alternatively, you can have your unit test inherit from your JSON writer. By inheriting, you can test all the protected and private internal bits instead of checking the actual String output.

或者,您可以从JSON编写器继承单元测试。通过继承,您可以测试所有受保护和私有内部位,而不是检查实际的String输出。

#2


2  

Since you cannot use external libraries in your server-side code, probably a better approach would be to limit the api unit-tests to the essential: - Test primitive object serialization - Test array serialization - Test special characters serialization - ...

由于您不能在服务器端代码中使用外部库,可能更好的方法是将api单元测试限制为必要: - 测试原始对象序列化 - 测试阵列序列化 - 测试特殊字符序列化 - ...

And then move part of the tests to the client side (using a JSON parser to ensure that at least your JSON is valid). Once you find a bug on the browser scripts execution, fix it and write a related unit-test to ensure that it doesn't show up again in the future releases.

然后将部分测试移至客户端(使用JSON解析器确保至少您的JSON有效)。一旦发现浏览器脚本执行上的错误,请修复它并编写相关的单元测试,以确保它在将来的版本中不再出现。

#1


5  

Technically the environment in which you are deploying your code is not the same as the environment in which you are developing it so I would use an existing JSON reader/writer to test the one you have created. If you are using maven, you can even set the scope of the JSON package you choose to use to be "test" so it will not be included in the actual build.

从技术上讲,部署代码的环境与开发代码的环境不同,因此我将使用现有的JSON读取器/编写器来测试您创建的代码。如果您使用的是maven,您甚至可以将您选择使用的JSON包的范围设置为“test”,这样它就不会包含在实际构建中。

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20090911</version>
  <scope>test</scope>
</dependency>

Alternatively, you can have your unit test inherit from your JSON writer. By inheriting, you can test all the protected and private internal bits instead of checking the actual String output.

或者,您可以从JSON编写器继承单元测试。通过继承,您可以测试所有受保护和私有内部位,而不是检查实际的String输出。

#2


2  

Since you cannot use external libraries in your server-side code, probably a better approach would be to limit the api unit-tests to the essential: - Test primitive object serialization - Test array serialization - Test special characters serialization - ...

由于您不能在服务器端代码中使用外部库,可能更好的方法是将api单元测试限制为必要: - 测试原始对象序列化 - 测试阵列序列化 - 测试特殊字符序列化 - ...

And then move part of the tests to the client side (using a JSON parser to ensure that at least your JSON is valid). Once you find a bug on the browser scripts execution, fix it and write a related unit-test to ensure that it doesn't show up again in the future releases.

然后将部分测试移至客户端(使用JSON解析器确保至少您的JSON有效)。一旦发现浏览器脚本执行上的错误,请修复它并编写相关的单元测试,以确保它在将来的版本中不再出现。