简体   繁体   English

Junit最佳实践:公共方法调用多个私有方法

[英]Junit Best Practice: Public method calling multiple private methods

I am starting to write JUnit test cases for a legacy codebase. 我开始为遗留代码库编写JUnit测试用例。 One of the public methods has multiple if statements and based on a condition it is calling different private methods. 公共方法之一具有多个if语句,并且基于条件它正在调用不同的私有方法。
Should I write just one test method and test for all the conditions? 我应该只编写一种测试方法并针对所有条件进行测试吗? or one method for each condition? 针对每种情况的一种方法?

Wouldn't I lose consistency if I write individual methods for each if condition? 如果为每个if条件编写单独的方法,我会不会失去一致性?

What is the approach to test private methods? 测试私有方法的方法是什么? Private method logic could be more complicated than public methods. 私有方法逻辑可能比公共方法更复杂。

Base the number of methods on the number of scenarios you want to test, it has nothing to do with the methods that the thing being tested has. 根据要测试的方案的数量来确定方法的数量,这与被测试事物所具有的方法无关。

If each scenario takes its own code to set up, then you will get one test method for each scenario. 如果每种情况都需要使用自己的代码来设置,那么每种情况都将获得一种测试方法。 If you can parameterize the tests then you may be able to have one test method and pass in different data for each scenario. 如果您可以对测试进行参数化,那么您也许可以使用一种测试方法,并为每种情况传递不同的数据。

The important thing is that for each combination of inputs you want the test to succeed or fail independently of the other tests. 重要的是,对于每种输入组合,您希望测试独立于其他测试而成功或失败。 If you shoehorn all the tests into one method then that can't happen, the first test failure will prevent the remaining tests from running. 如果将所有测试合并为一个方法,那么那不可能发生,则第一个测试失败将阻止其余测试运行。

I agree with Nathan. 我同意内森。 Tests should go by scenarios not methods. 测试应该通过方案而不是方法进行。 Sometimes legacy code is written in a way that you need to test private methods directly though. 有时,遗留代码是以您需要直接测试私有方法的方式编写的。 And yes, the code should be refactored. 是的,应该重构代码。 But if you can't refactor or want a test in place first... 但是,如果您不能重构或想要先进行测试……

Option 1 - make methods package private access 选项1-使方法包私有访问

This is a very safe refactoring. 这是一个非常安全的重构。

Option 2 - use reflection to call the static method directly 选项2-使用反射直接调用静态方法

If you REALLY can't touch the code, this is the best you can do. 如果您真的不能触摸代码,这是您可以做的最好的事情。 I'd question the requirement to not touch the code. 我要问不要碰代码的要求。 If we can't improve the code, should we leave it in the corner to rot? 如果我们无法改进代码,是否应该将其遗留在角落以免腐烂?

From my point of view having smaller units in unit testing normally results in better tests. 从我的角度来看,在单元测试中使用较小的单元通常会带来更好的测试。 For you this would mean to change the private methods to package private and to write tests for each of them. 对您来说,这意味着将私有方法更改为打包私有并为每个方法编写测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个特定的公共方法调用私有方法或单个通用公共方法? - Multiple specific public methods calling a private method or a single generic public method? 在侦听器的handle方法中是否有最佳方法来调用控制器类的方法? - Is there a best practice way for calling methods of controller class in handle method of a listener? 对调用多个私有方法的公共方法进行单元测试 - Unit testing a public method that calls multiple private methods Java调用方法非/静态和私有/公共 - Java calling methods non/static and private/public Junit:没有实例调用私有方法 - Junit: Calling private method without instance 多个参数化JUnit测试的最佳实践 - Best Practice for multiple Parameterized JUnit Tests 在一种公共方法中使用相同的私有方法 - Same private methods to be used in one public method 哪种更好的做法,在类中使用私有变量或公共方法? - Which is better practice, using private variables or public methods within the class? 为在私有方法中创建的公共方法编写 junit(mockito) - Writing junit(mockito) for a public method created inside a private method 如何在Junit中验证公共方法中私有方法的调用次数? - How to verify number of call of private method in public method in Junit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM