如何重构方法以使其更容易测试(How to refactor a method to make it easier to test)

编程入门 行业动态 更新时间:2024-10-28 07:26:21
如何重构方法以使其更容易测试(How to refactor a method to make it easier to test)

下面是一个方法,我很难弄清楚如何使用JUnit进行测试。 这种方法很难测试,因为它取决于其他方法的结果(例如getClosestDcoumentCode)。

基于我对JUnit的阅读,这表明我应该重构该方法。 但是怎么样? 如果不需要重构,您如何测试依赖于其他方法的方法?

谢谢,

埃利奥特

private static String findPrincipal(List<DocumentKey> documentkeys_) { Hashtable<String, Integer> codecounts = new Hashtable<String, Integer>(); for (DocumentKey document : documentkeys_) { int x = 0; String closestCode = getClosestDocumentCode(document.candidates); if (closestCode == null) continue; int thecount = 0; if (codecounts.containsKey(closestCode)) thecount = codecounts.get(closestCode); if (document.hasKey) thecount += 2; else thecount++; codecounts.put(closestCode, new Integer(thecount)); x++; } String closestCode = getClosestCode(codecounts); return closestCode; }

Below is a method that I'm having a hard time figuring out how to test using JUnit. This method is difficult to test because it depends on the results of other methods (e.g. getClosestDcoumentCode).

Based on my reading of JUnit, this suggests I should refactor the method. But how? And if refactoring is not necessary, how do you test a method that depends on other methods?

Thank you,

Elliott

private static String findPrincipal(List<DocumentKey> documentkeys_) { Hashtable<String, Integer> codecounts = new Hashtable<String, Integer>(); for (DocumentKey document : documentkeys_) { int x = 0; String closestCode = getClosestDocumentCode(document.candidates); if (closestCode == null) continue; int thecount = 0; if (codecounts.containsKey(closestCode)) thecount = codecounts.get(closestCode); if (document.hasKey) thecount += 2; else thecount++; codecounts.put(closestCode, new Integer(thecount)); x++; } String closestCode = getClosestCode(codecounts); return closestCode; }

最满意答案

听起来像getClosestCode和getClosestDocumentCode属于一组不同于findPrincipal方法的责任 。 所以你要首先将它们分成两个不同的类。 为每个要创建的类创建一个接口。 然后,实现findPrincipal方法的类可以依赖于另一个接口作为构造函数参数,如下所示:

public class PrincipalFinderImpl implements PrincipalFinder { private CodeFinder codeFinder; public PrincipalFinderImpl(CodeFinder codeFinder) { this.codeFinder = codeFinder; } public String findPrincipal(List<DocumentKey> documentkeys_) { Hashtable<String, Integer> codecounts = new Hashtable<String, Integer>(); for (DocumentKey document : documentkeys_) { int x = 0; String closestCode = codeFinder.getClosestDocumentCode(document.candidates); if (closestCode == null) continue; int thecount = 0; if (codecounts.containsKey(closestCode)) thecount = codecounts.get(closestCode); if (document.hasKey) thecount += 2; else thecount++; codecounts.put(closestCode, new Integer(thecount)); x++; } String closestCode = codeFinder.getClosestCode(codecounts); return closestCode; } }

现在,应该很容易创建另一个实现CodeFinder接口的类,手动或使用Mocking框架。 然后,您可以控制每次调用getClosestCode和getClosestDocumentCode ,并确保使用您希望调用的参数完全调用这些方法中的每一个。

It sounds to me like getClosestCode and getClosestDocumentCode belong to a different set of responsibilities than the findPrincipal method. So you'll want to begin by separating these into two different classes. Create an interface for each class to implement. The class that implements the findPrincipal method can then rely on the other interface as a constructor argument, like this:

public class PrincipalFinderImpl implements PrincipalFinder { private CodeFinder codeFinder; public PrincipalFinderImpl(CodeFinder codeFinder) { this.codeFinder = codeFinder; } public String findPrincipal(List<DocumentKey> documentkeys_) { Hashtable<String, Integer> codecounts = new Hashtable<String, Integer>(); for (DocumentKey document : documentkeys_) { int x = 0; String closestCode = codeFinder.getClosestDocumentCode(document.candidates); if (closestCode == null) continue; int thecount = 0; if (codecounts.containsKey(closestCode)) thecount = codecounts.get(closestCode); if (document.hasKey) thecount += 2; else thecount++; codecounts.put(closestCode, new Integer(thecount)); x++; } String closestCode = codeFinder.getClosestCode(codecounts); return closestCode; } }

Now it should be easy to create another class the implements the CodeFinder interface, either manually or using a Mocking framework. You can then control the results of each call to getClosestCode and getClosestDocumentCode, and ensure that each of these methods gets called with exactly the arguments you expect it to be called with.

更多推荐

本文发布于:2023-07-30 07:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336765.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使其   更容易   重构   测试   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!