In a typical JUnit test, some data is set up that is passed to the code under test,
and the return value is verified. All this does is test data — or state.
Mock objects allow the classes surrounding the class under to test to be
replaced with fake or mock versions. This allows a class to be tested in isolation
from its dependencies.
如上描述JUnit测试的是状态,Mock Objects测试的是同其他类的交互
Mock Objects 是对类或接口的伪造实现,通常用来mock被测试类的依赖环境(empty shells),他们比较简单所以不许要再被测试
The Mock Objects Library contains the following parts:
1. An expectation library to help define the expectations when creating
mock objects
2. Alternative APIs for many of the common JDK and J2EE libraries,
including network sockets, I/O, JDBC, Servlets, JMS, JNDI, and Java-
Mail. These alternative APIs extend the conventional APIs to include
clean interface separations from implementations, which allows code
that depends on these interfaces to be passed mock implementations —
as well as any other custom implementation — as needed.
3. Mock implementations for the APIs mentioned previously
4. A library for dynamically creating mocks at runtime from your unit
tests without having to define extra classes. This uses dynamic proxies.
The Mock Objects Library 通过CGLib的动态代理实现mock功能
博客介绍了JUnit和Mock Objects测试。JUnit主要测试数据或状态,而Mock Objects可将被测试类周围的类替换为伪造版本,实现类与依赖的隔离测试。还介绍了Mock Objects Library的组成部分,其通过CGLib的动态代理实现mock功能。
274

被折叠的 条评论
为什么被折叠?



