将私有成员更改为默认用于测试 [英] Change private member to default for testing

查看:29
本文介绍了将私有成员更改为默认用于测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将私有类成员更改为默认(包访问)以测试其行为是个好主意吗?我的意思是测试用例应该在 test 目录中,但与测试成员的类在同一个包中.

Is that good idea to change private class members to default(package access) for testing their behavior? I mean test case should destinate in test directory but in same package as tested member's class.

你们所有人都说的是真的.但是类经常有辅助私有方法.而且这些方法可能很复杂,因此需要进行测试.这太糟糕了 - 测试公共方法以确保私有复杂方法的正确工作.你不这么认为吗?

All you guys tell the true. But classes have helper private methods often. And these methods can be complicated so need to be tested. And that is too bad - to test public methods for ensure correct working for private complicated methods. Don't you think so?

推荐答案

我通常更喜欢以针对公共 API 编写测试的方式来编写我的类和测试.所以基本上我是说,如果您需要访问被测类的私有状态,那么您的测试可能已经过多地参与了该类的内部结构..

I generally prefer writing my classes and tests in a way that writing the tests against the public API makes sense. So basically I'm saying if you need to access the private state of your class under test you're probably already too involved in the internals of that class with your test..

这篇关于将私有成员更改为默认用于测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆