Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Java 质量保障
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
LI Daobing
August 04, 2011
Programming
310
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Java 质量保障
LI Daobing
August 04, 2011
More Decks by LI Daobing
See All by LI Daobing
How to attack TLS in PQC decade, part I
lidaobing
0
49
出了问题不要靠猜
lidaobing
40
4.1k
HTTP协议相关的若干安全问题
lidaobing
9
1.2k
Debian & Packaging
lidaobing
1
580
OAuth: How and Why?
lidaobing
1
160
从 Struts 迁移到 Spring MVC,以及为什么?
lidaobing
2
670
glusterfs 文件系统
lidaobing
2
210
如何学习 Shell
lidaobing
3
340
Other Decks in Programming
See All in Programming
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
210
Oxcを導入して開発体験が向上した話
yug1224
4
320
ふつうのFeature Flag実践入門
irof
7
4k
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
200
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
6
1.3k
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
170
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.3k
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.2k
肥大化するレガシーコードに立ち向かうためのインターフェース分離と依存の逆転 / JJUG CCC 2026 Spring
hirokunimaeta
0
570
Inside Stream API
skrb
1
730
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
RTSPクライアントを自作してみた話
simotin13
0
610
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Building the Perfect Custom Keyboard
takai
2
800
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
320
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
390
Bash Introduction
62gerente
615
220k
A Tale of Four Properties
chriscoyier
163
24k
The Limits of Empathy - UXLibs8
cassininazir
1
360
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
Transcript
Java 质量保障 LI Daobing <
[email protected]
> 2011-08-04
Outline 如何做单元测试 如何让代码对单元测试友好 一种中规中矩的结构设计 总结
注意 : 以下都是个人观点
单元测试 单元测试的目的 验证类实现了设计时承诺的行为 帮助再现 bug 帮助发现
bug 保障重构
单元测试的基本约定 使用 JUnit4 或者 TestNG 源码与测试的包 (package) 名保持一致,
FooService 对应的测试类为 FooServiceTest
测试名最好是一个句子 测试名最好是一个句子 好的名字 : runShouldRaiseFooExceptionWithNullArgumen t 一般的名字
: testRun 糟糕的名字 : test2 最好的情况是看见一个测试类的全部方法名就知 道这个类能干什么,什么情况下会有什么行为
为测试准备单独的环境 可以用 System.getenv 或者 System.getProperty 来判定当前的环境 为测试准备一份单独的配置文件 /
工厂类 /Spring Configuration
使用依赖注入来简化测试
使用 protected 函数来 mock 底 层依赖
如何正确测试异常
其他要点 测试不易过大,每个单元测试尽量只检测类的一 个特性 测试不仅仅是执行代码,更要检测代码执行后结 果是否满足期望 不要在单元测试中随意 catch
异常,尽量让异 常抛出来 如果使用 Spring, 可以使用 Spring 现有的测试 支持
Outline 如何做单元测试 如何让代码对单元测试友好 一种中规中矩的包设计 总结
服务应当是一个对象而不是一个类 测试需要构建一个测试用对象,依赖于一些 mock 的底层服务 甚至需要创建多个服务 这个时候使用对象比使用类方便
对象意味着晚绑定 , 晚绑定意味着灵活
服务应当是一个对象而不是一个类 比如 A 服务依赖于 B 服务,如果 B 服务是一 个对象,那么可以构造出一个
B 服务的 Mock 对象,注入到 A 服务,方便测试 A 服务 如果 B 服务是一个类,那么测试起来就会很麻 烦
服务的依赖尽量在构造时一次传入 在服务创建时可以一次检查所有可能的问题 避免服务处于一个无效的状态
使用工厂简化服务构造
按 package 划分 Factory 如果工程比较大,只用一个 Factory 类会显得 过于复杂,一般建议按 package
来划分 Factory 同时一个 package 最好只有一个 Factory, Factory 具有配置的隐喻,而配置应该集中而不 是分散 比如 com.snda.llap.replicator.client.DatanodeChoos erClientFactory
你其实也可以使用 Spring
声明方法时不要使用通用异常类
不要拦截你不知道如何处理的异常
在边界处检查异常 尽量在系统边界处检查异常,而在系统中间让异 常透明通过 比如 web 或者 socket 服务,只在相应前做一
个通用异常检查,并返回信息给用户 当然在另外一个边界处也要做适当检查,避免所 依赖的其他服务出现异常时延迟发现。
不要使用 e.printStackTrace()
组织你的异常类 Exception KeybaseException FooException RuntimeException
KeybaseRuntimeException BarException
其他建议 熟悉 commons-lang,codec,io, ... 库 熟悉 guava 库
一种中规中矩的工程结构
Summary Java 静态,面向对象 安全,易用,对多线程支持较好 推崇单元测试,文档,代码检查。
推崇设计模式,重构,团队合作。 也是在企业应用领域使用最广泛的语言。 不是动态语言 (Python, Ruby, …) 对高并发支持不好 (Scala, ...)
None
Q&A
Thanks for your attention