简体   繁体   English

Android - Kotlin - JUnit 5:如何测试私有方法?

[英]Android - Kotlin - JUnit 5: How to test private methods?

General software organization question: I have a Kotlin class file (under src/) with some 10 methods, only 1 of which should be visible outside the class (the rest should be private). General software organization question: I have a Kotlin class file (under src/) with some 10 methods, only 1 of which should be visible outside the class (the rest should be private). I also have a dozen JUnit test methods for testing the abovementioned methods, and I put all those in a different class file (under src/test/).我还有十几个 JUnit 测试方法用于测试上述方法,我将所有这些方法放在不同的 class 文件中(在 src/test/ 下)。 But the test methods cannot see the methods they're supposed to test, so I had to remove the private modifiers from the source code:(但是测试方法看不到他们应该测试的方法,所以我不得不从源代码中删除私有修饰符:(
What's a man to do?男人要做什么? JUnit 5, Kotlin for Android, on IntelliJ, if that matters. JUnit 5,Kotlin 用于 IntelliJ 上的 Android,如果这很重要的话。 Thank you谢谢

What's a man to do?男人要做什么?

Check this question and answer .检查这个问题和答案

Either a) test the private details indirectly by testing the public interface (which goes through the private methods) or b) Use @VisibleForTesting annotation to make a private function public for testing but clearly flagged as something that should not be used otherwise.要么a)通过测试公共接口(通过私有方法)间接测试私有细节,要么b)使用@VisibleForTesting注释将私有function公开用于测试,但明确标记为不应该使用的东西。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM