如何重构我的类以便我可以对其进行单元测试?

发布于 2024-09-02 23:23:21 字数 545 浏览 2 评论 0原文

我正在尝试对一个进行 SAX 解析并创建对象的类进行单元测试。 该类采用一个字符串作为参数,表示互联网上文​​档的 URL,对其进行解析,然后根据内容创建一个对象。

我不想让单元测试实际访问网络,所以我想要一些测试 xml 文件来解析。但是我不知道如何从我的 AndroidTestCases 访问它们。我不想将测试文件包含在实际应用程序中,我希望它们包含在测试项目中(这是一个单独的项目,这是我收集到的 Android 测试的规范 - 由于需要自定义 AndroidManifest .xml 就是其中之一)。

一种方法是将 XML 文件放入测试项目的资产目录中,我可以使用 getContext().getAssets().open(filename) 将它们读取到 InputStream 中在测试用例中,但我的类需要一个 URL 字符串。我宁愿不必向此类提供 InputStream 而不是当前的 URL 字符串。我可以通过创建两种方法来测试解析,一种方法接受字符串,另一种方法接受输入流,然后测试第二个方法,但是如何测试只接受字符串的方法呢?

我应该如何设计我的课程和/或测试来规避这个问题?

I am trying to unit test a class that does SAX parsing and creates an object.
This class takes a string as a parameter representing the URL of a document on the internet, parses it and then creates an object based on the contents.

I don't want to have the unit tests actually access the network, so I'd like to have a few test xml files to parse. However I can't figure out how to access them from my AndroidTestCases. I don't want to include the test files with the actual application, I want them in the test project (it's a separate project, as is the norm for Android tests from what I could gather - due to the need to have a custom AndroidManifest.xml, for one).

One way would be to put the XML files in the test project's assets directory, I can read them using getContext().getAssets().open(filename) into an InputStream in the test case, but my class expects a URL string. I'd rather not have to provide an InputStream to this class instead of the current URL string. I can test just the parsing by making two methods, one that takes a string and one an Inputstream, and test the second, but how can I then test the one that just takes a string?

How should I design my class and or tests to circumvent this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-09-09 23:23:21

您想要使用依赖项注入。根据我读到的内容,您有一个如下所示的方法签名:

 string -> object // using lambda notation

听起来您用于获取 URL 内容的代码存在于您正在调用的同一方法中。您应该将其重构为一个单独的对象。然后,您可以创建一个模拟对象来测试,而不是进行网络访问,而是使用任何必要的参数对磁盘进行文件操作。在您的测试情况下,您可以在测试设置中注入模拟对象来代替真实的网络 getter 对象。

该方法并不关心它有什么对象,只要它返回一些要解析的文本即可,无论是 XHTML 还是 XML。

You'd want to go with Dependency injection. Based on what I read, you have a method signature like so:

 string -> object // using lambda notation

It sounds like the code you have for getting the contents of the URL exists in the same method you are calling. You should refactor that into a separate object. You can then make a mock object for testing that instead of doing network access, instead does a file operation to your disk with whatever parameters are necessary. In your testing situation, you inject the mock object in place of the real network getter object in your test setup.

The method then doesn't care what object it has, as long as it is returning some text to parse, be that XHTML or XML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文