easymock

DAO Unit testing

十年热恋 提交于 2019-11-30 11:03:51
问题 I have been looking at EasyMock and tutorials/examples around using it for Unit Testing DAO classes, for an "outside container" test. However, I think most of them talk about testing the Service Layer instead, mocking the DAO class. I am a bit confused, is it really how you Unit Test the DAO layer? Some would say that the tests interacting with DB & EJBs are actually Integration tests and not Unit tests but then how would you know if your SQL is correct (assuming no ORM) and your DAO inserts

Mock Runtime.getRuntime()?

北战南征 提交于 2019-11-30 08:26:46
Can anyone make any suggestions about how best to use EasyMock to expect a call to Runtime.getRuntime().exec(xxx) ? I could move the call into a method in another class that implements an interface, but would rather not in an ideal world. interface RuntimeWrapper { ProcessWrapper execute(String command) throws IOException; } interface ProcessWrapper { int waitFor() throws InterruptedException; } I was wondering if anyone had any other suggestions? Your class shouldn't call Runtime.getRuntime() . it should expect a Runtime to be set as its dependency, and work with it. Then in your test you can

Is it possible to create a mock object that implements multiple interfaces with EasyMock?

烂漫一生 提交于 2019-11-30 07:50:02
Is it possible to create a mock object that implements several interfaces with EasyMock? For example, interface Foo and interface Closeable ? In Rhino Mocks you can provide multiple interfaces when creating a mock object, but EasyMock's createMock() method only takes one type. Is it possbile to achieve this with EasyMock, without resorting to the fallback of creating a temporary interface that extends both Foo and Closeable , and then mocking that? EasyMock doesn't support this so you're stuck with fallback of the temporary interface. As an aside, I smell a little bit of a code wiff - should a

Getting EasyMock mock objects to throw Exceptions

穿精又带淫゛_ 提交于 2019-11-30 07:24:53
问题 I'm in process of using EasyMock to write Unit tests for a number of collaborating classes. One of these classes (lets call it Foo ) opens a network connection to a remote server and parses that servers' XML response into something the rest of the classes can use. Presently my tests only encompass scenarios in which everything is hunky-dory and the remote server is up and running and returning XML as expected. However, I would be happier if I could mock Foo so that I simulate what happens if

EasyMock andReturn() vs andStubReturn()

扶醉桌前 提交于 2019-11-30 06:28:53
问题 What is the difference between using andReturn(T value) vs andStubReturn(T value) for EasyMock? In what situation would you use andStubReturn() where andReturn() can't achieve the same result? 回答1: You use a stub return for a method call on the mock that you expect to happen but aren't otherwise interested in. You use a regular return for a "regular" method call. Consider the following method: public void someMethod(String arg) { if (logger.isDebugEnabled()) { logger.debug("Calling

《Java实践指南》--读后

只愿长相守 提交于 2019-11-30 04:35:42
闲读《Java实践指南》... 1.lvy 某些项目中能够看到ivy.xml。早期使用ant的项目中,常常用ivy.xml来下载项目依赖。 2.ant 作为java程序员,应该都知道ant,虽然可能用过的人不多。为什么ant慢慢被maven或者gradle代替?因为ant只能构建,不能下载依赖。说白了maven=ant+ivy,人们都喜欢简单而又强大的工具。 3.maven 上面其实已经说了maven,专业点说,maven同时具备依赖管理和项目构建。 4.gradle ant的构建非常灵活,maven的优点众多。但两者终究还是两者,所以有了gradle,可以认为gradle同时具备两者的优点。 5.EasyMock 早期项目,老项目中出现的较多。 6.Mockito 最常用的模拟测试框架 7.PowerMock 建立在EasyMock和Mockito基础之上,并添加了更多功能 8.Gson Gson是线程安全的,所以一般只需要为应用创建一个即可。 9.commons.io Java的原生IO操作要有多繁琐就有多繁琐,commons.io是不错的选择。 总结,差不多只适用于初级程序员,中高级真的只是塞牙缝的感觉了。快读的话,一个小时就能看完了。说实话,不太推荐买。 来源: https://my.oschina.net/vright/blog/3108802

EasyMock的使用

非 Y 不嫁゛ 提交于 2019-11-30 00:39:21
1.Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一些在应用中不容易构造或者比较复杂的对象,从而把测试与测试边界以外的对象隔离开。同时也可以当调用别人的模块,而该模块又没有实现时(只提供接口),我们可以在独立的环境中测试自己的模块逻辑。 2.使用前的准备,下载所需的jar包:easymock-3.0.jar(或以上版本),junit-4.4.jar,cglib-nodep-2.1_3.jar 3.使用方法较简单。主要有以下步骤: *•使用 EasyMock 生成 Mock 对象; *•设定 Mock 对象的预期行为和输出; *•将 Mock 对象切换到 Replay 状态; *•调用 Mock 对象方法进行单元测试; *•对 Mock 对象的行为进行验证。 测试实例:假如我有一个IStudent接口类和StudentApplication类,StudentApplication类中用到了IStudent中的没实现的方法,而我想测试StudentApplication,这时用EasyMock构造一个IStudent的Mock对象,并给要用到的的未实现的方法设定已知返回值。 code: 1 public interface IStudent { 2 public String doMethod1(); 3 public String doMethod2(); 4

EasyMock的使用

空扰寡人 提交于 2019-11-30 00:39:08
1.Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一些在应用中不容易构造或者比较复杂的对象,从而把测试与测试边界以外的对象隔离开。同时也可以当调用别人的模块,而该模块又没有实现时(只提供接口),我们可以在独立的环境中测试自己的模块逻辑。 2.使用前的准备,下载所需的jar包:easymock-3.0.jar(或以上版本),junit-4.4.jar,cglib-nodep-2.1_3.jar 3.使用方法较简单。主要有以下步骤: *•使用 EasyMock 生成 Mock 对象; *•设定 Mock 对象的预期行为和输出; *•将 Mock 对象切换到 Replay 状态; *•调用 Mock 对象方法进行单元测试; *•对 Mock 对象的行为进行验证。 测试实例:假如我有一个IStudent接口类和StudentApplication类,StudentApplication类中用到了IStudent中的没实现的方法,而我想测试StudentApplication,这时用EasyMock构造一个IStudent的Mock对象,并给要用到的的未实现的方法设定已知返回值。 code: 1 public interface IStudent { 2 public String doMethod1(); 3 public String doMethod2(); 4

EasyMock的使用

Deadly 提交于 2019-11-30 00:38:52
1.Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一些在应用中不容易构造或者比较复杂的对象,从而把测试与测试边界以外的对象隔离开。同时也可以当调用别人的模块,而该模块又没有实现时(只提供接口),我们可以在独立的环境中测试自己的模块逻辑。 2.使用前的准备,下载所需的jar包:easymock-3.0.jar(或以上版本),junit-4.4.jar,cglib-nodep-2.1_3.jar 3.使用方法较简单。主要有以下步骤: *•使用 EasyMock 生成 Mock 对象; *•设定 Mock 对象的预期行为和输出; *•将 Mock 对象切换到 Replay 状态; *•调用 Mock 对象方法进行单元测试; *•对 Mock 对象的行为进行验证。 测试实例:假如我有一个IStudent接口类和StudentApplication类,StudentApplication类中用到了IStudent中的没实现的方法,而我想测试StudentApplication,这时用EasyMock构造一个IStudent的Mock对象,并给要用到的的未实现的方法设定已知返回值。 code: 1 public interface IStudent { 2 public String doMethod1(); 3 public String doMethod2(); 4

DAO Unit testing

旧时模样 提交于 2019-11-29 23:07:47
I have been looking at EasyMock and tutorials/examples around using it for Unit Testing DAO classes, for an "outside container" test. However, I think most of them talk about testing the Service Layer instead, mocking the DAO class. I am a bit confused, is it really how you Unit Test the DAO layer? Some would say that the tests interacting with DB & EJBs are actually Integration tests and not Unit tests but then how would you know if your SQL is correct (assuming no ORM) and your DAO inserts/queries the right data from your real (read, local database which is similar to that in production)