[深入JUnit] 为什么别测试private函数
[深入JUnit] 为什么别测试private函数
摘自http://www.tuicool.com/articles/iumaayJ
比如说,Bird是我们要测试的class,它有public, protected,以及private的方法。
// 文件位置:src/test/sample/Bird.java
package test.sample;
class Bird {
public void fly() { ... }
public void eat() { ... }
protected void singRandomly() {
final Song s = findASong(<some_random_number>);
singASong(s);
}
private Song findASong() { ... }
private void singASong() { ... }
}
现在有一个 BirdTest class。对这个class而言,它可见的所有函数,是 Bird.class.getDeclaredMethods() 的返回值。
代码细节请看
junit.internal.MethodSorter#getDeclaredMethods()
以及 java.lang.Class#getDeclaredMethods()
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/Class.java#Class.getDeclaredMethods%28%29
所有的 public, protected, private
方法 BirdTest
都能看到。但是, 看到不等于能调用
。
// 文件位置:tst/test/sample/BirdTest.java
package test.sample;
class BirdTest {
@Test
public void testFly_CaseDescription1() {
...
bird.fly(); //当然ok,因为Bird#fly是public的
}
@Test
public void testSingRandomly_CaseDescription1() {
...
bird.sing(); //ok,因为BirdTest也在test.sample package下面。否则是非法的。
}
@Test
public void testFindASong() {
...
bird.findASong(); // 非法,不能调用Bird的私有函数。
}
}
在上面的代码里,由于 BirdTest 与 Bird 在一个package test.sample 里,所以 Bird 所有的 public 和 protected 函数,对 BirdTest 可见。但是, private 应该是不可调用的。
当然,有人会告诉你如何利用java reflection的API来让 private method也可以调用
// 无关紧要的parameter用 '_' 略去了
Method findASong = targetClass.getDeclaredMethod("findASong", _);
findASong.setAccessible(true);
return findASong.invoke(_, _);
但是,这打破了 Bird 类的封装,是非常不好的。设想,改动 private 的方法的声明会造成test failure,那么 private 就失去意义了。与 protected 其实区别不大。
那么应该怎么做呢?
不去测试private函数,好的private函数都应该是很小很简单的,测试那调用了private函数的public和protected方法即可。
或者,也许这个private函数其实应该被声明称protected。
如果以上方法你都觉得不合适,而某一个private函数很复杂,很需要测试。那么,根据Single Responsibility原则,这个private函数就应该被放到一个单独的class里面。
class BirdSongs {
protected Song findASong(Integer id) { ... }
protected void singASong(Song s) { ... }
}
然后,对 BirdSongs#findASong 进行测试。
如果您有不同意见,欢迎与我讨论。
[深入JUnit] 为什么别测试private函数的更多相关文章
- Junit测试private方法
package com.bill99.junit; public class ACase { private String echoRequest(String request) { return & ...
- Spring-test使用JUnit时,测试类autowired报错,create bean error
Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...
- Android中使用自身携带的Junit新建一个测试工程
1.新建立一个Android工程 package com.shellway.junit; public class Service { public int divide(int a,int b){ ...
- 测试一个函数的运行时间(C++)
#include <ctime> static clock_t Start,Finish; Start=clock(); fun(); Finish = clock(); double t ...
- 如何在单元测试中测试异步函数,block回调这种
大概有四种方法: runloop 阻塞主进程等待结果 semphaore 阻塞主进程等待结果 使用XCTestExpectation 阻塞主线程等待(我用这个,xcode自带的,为啥不用) 使用第三方 ...
- Java高级特性 第11节 JUnit 3.x和JUnit 4.x测试框架
一.软件测试 1.软件测试的概念及分类 软件测试是使用人工或者自动手段来运行或测试某个系统的过程,其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别.它是帮助识别开发完成(中间或最终 ...
- Maven聚合、Maven仓库jar包以及Spring+MyBatis+JUnit+Maven整合测试的搭建过程
一.Maven将父项目创建到父项目的内部 在父项目的pom.xml上 点右键,选择maven-->new-->maven module project 二.Maven聚合 在某个项目的p ...
- .NET中如何测试Private和Protected方法
TDD是1)写测试2)写通过这些测试的代码,3)然后重构的实践.在,NET社区中, 这个概念逐渐变得非常流行,这归功于它所增加的质量保证.此时,它很容易测试public方法,但是一个普遍的问题出现了, ...
- 使用timeit测试Python函数的性能
timeit是Python标准库内置的小工具,可以快速测试小段代码的性能. 认识timeit timeit 函数: timeit.timeit(stmt, setup,timer, number) 参 ...
随机推荐
- h5新特性
!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></ ...
- 【C#网络基础】C# get post请求
using KTCommon.Helper; using KTCommon.LOG; using System; using System.Collections.Generic; using Sys ...
- Cache-control
网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.其作用根据 ...
- MC, MCMC, Gibbs採样 原理&实现(in R)
本文用讲一下指定分布的随机抽样方法:MC(Monte Carlo), MC(Markov Chain), MCMC(Markov Chain Monte Carlo)的基本原理,并用R语言实现了几个样 ...
- 使用boost中的线程池
#include <boost/thread/thread.hpp>#include <boost/bind.hpp>#include <iostream> u ...
- REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解
转载自处blog.csdn.net/tantexian. SOA: 维基百科解释:SOA:面向服务的软件架构(Service Oriented Architecture),是一种计算机软件的设计模式, ...
- 关于一个下载的源代码中的”*.vssscc“文件的问题
今天下载了一份程序的源代码,老是提示我要连接源代码管理服务器,这个……你的账号密码我怎么知道,有木有.于是上网搜罗了一番找来了解决方案,在这里分享给可能出现同样问题的童鞋. 首先说明一下什么是vsss ...
- Cretiria查询应用(一)
1.查询所有 Criteria criteria=session.createCriteria(Dept.class); //调用list()方法 List<Dept> li ...
- (转)url重写
使用URLRewriter.dll后,根本不需要使用任何代码,我之前做的项目就是用的做URL重写的,其实不是进化,其实表面上看是.html扩展名而已,当然你还可以用其他的任意扩展名下面是你的配置 &l ...
- sqlserver编程基本语法
一.定义变量 --简单赋值 declare @a int set @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @ ...