jmock

Mockito 简明教程

耗尽温柔 提交于 2020-10-07 04:00:47
什么是 Mock 测试 Mock 测试就是在测试过程中,对于某些不容易构造(如 HttpServletRequest 必须在Servlet 容器中才能构造出来)或者不容易获取比较复杂的对象(如 JDBC 中的ResultSet 对象),用一个虚拟的对象(Mock 对象)来创建以便测试的测试方法。 Mock 最大的功能是帮你把单元测试的耦合分解开,如果你的代码对另一个类或者接口有依赖,它能够帮你模拟这些依赖,并帮你验证所调用的依赖的行为。 比如一段代码有这样的依赖: 当我们需要测试A类的时候,如果没有 Mock,则我们需要把整个依赖树都构建出来,而使用 Mock 的话就可以将结构分解开,像下面这样: Mock 对象使用范畴 真实对象具有不可确定的行为,产生不可预测的效果(如:股票行情,天气预报) : 真实对象很难被创建的 真实对象的某些行为很难被触发 真实对象实际上还不存在的(和其他开发小组或者和新的硬件打交道)等等 使用 Mock 对象测试的关键步骤 使用一个接口来描述这个对象 在产品代码中实现这个接口 在测试代码中实现这个接口 在被测试代码中只是通过接口来引用对象,所以它不知道这个引用的对象是真实对象,还是 Mock 对象。 Mock 与 Stub 的区别 Mock 不是 Stub,两者是有区别的: 前者被称为 mockist TDD,而后者一般称为 classic TDD ;

Mockito 简明教程

前提是你 提交于 2020-10-06 11:47:52
什么是 Mock 测试 Mock 测试就是在测试过程中,对于某些不容易构造(如 HttpServletRequest 必须在Servlet 容器中才能构造出来)或者不容易获取比较复杂的对象(如 JDBC 中的ResultSet 对象),用一个虚拟的对象(Mock 对象)来创建以便测试的测试方法。 Mock 最大的功能是帮你把单元测试的耦合分解开,如果你的代码对另一个类或者接口有依赖,它能够帮你模拟这些依赖,并帮你验证所调用的依赖的行为。 比如一段代码有这样的依赖: 当我们需要测试A类的时候,如果没有 Mock,则我们需要把整个依赖树都构建出来,而使用 Mock 的话就可以将结构分解开,像下面这样: Mock 对象使用范畴 真实对象具有不可确定的行为,产生不可预测的效果(如:股票行情,天气预报) : 真实对象很难被创建的 真实对象的某些行为很难被触发 真实对象实际上还不存在的(和其他开发小组或者和新的硬件打交道)等等 使用 Mock 对象测试的关键步骤 使用一个接口来描述这个对象 在产品代码中实现这个接口 在测试代码中实现这个接口 在被测试代码中只是通过接口来引用对象,所以它不知道这个引用的对象是真实对象,还是 Mock 对象。 Mock 与 Stub 的区别 Mock 不是 Stub,两者是有区别的: 前者被称为 mockist TDD,而后者一般称为 classic TDD ;

java.lang.VerifyError with Mockito 1.10.17

瘦欲@ 提交于 2020-01-24 04:59:26
问题 I am trying to replace JMock with Mockito (1.10.17). I have already done some unit tests successfully, but now I want to use the timeout feature verify(publisher, timeout(5000)).notifySubscribers(any(BecameMasterMessage.class)); and I get this exception: java.lang.VerifyError: (class: org/mockito/internal/verification/VerificationOverTimeImpl, method: verify signature: (Lorg/mockito/internal/verification/api/VerificationData;)V) Incompatible argument to function at org.mockito.verification

Can I mock a super class method call?

别来无恙 提交于 2020-01-22 13:37:31
问题 Sometimes, you want to test a class method and you want to do an expectation on a call of a super class method. I did not found a way to do this expectation in java using easymock or jmock (and I think it is not possible). There is a (relative) clean solution, to create a delegate with the super class method logic and then set expectations on it, but I don't know why and when use that solution ¿any ideas/examples? Thanks 回答1: Well, you can if you want to. I don't know if you are familiar with

Can I mock a super class method call?

十年热恋 提交于 2020-01-22 13:36:08
问题 Sometimes, you want to test a class method and you want to do an expectation on a call of a super class method. I did not found a way to do this expectation in java using easymock or jmock (and I think it is not possible). There is a (relative) clean solution, to create a delegate with the super class method logic and then set expectations on it, but I don't know why and when use that solution ¿any ideas/examples? Thanks 回答1: Well, you can if you want to. I don't know if you are familiar with

Jmock - how to automate & mock out console user input?

十年热恋 提交于 2019-12-25 05:04:25
问题 I have some functionality that I want to mock out being called from main (static: I've read about that too - jmock mocking a static method). i recently read that JMock doesn't support the mocking of static functions. Well, the associated code (that's giving me a problem) must be called from main, and must be in the class with main... Sample source Test code Right now, I want to ensure that my main has a test to make sure that the file exists before it proceeds. Problem is, I have my program

How to mock JdbcTemplate.update using Jmockit?

╄→尐↘猪︶ㄣ 提交于 2019-12-24 20:43:29
问题 I'm new to Jmockit and I'm trying to mock jdbcTemplate.udpate() using the following verification, new Expectations() {{ someRef.flushUpdates(); }}; new Verifications() {{ String query; jdbcTemplate.update(query = withCapture(), withInstanceOf(Date.class)); times = 1; }}; The flushUpdate has an update query, public void flushUpdates(){ Date now = new Date(); String query = "Update table_name set last_updated = ? "; jdbcTemplate.update(query,now); } The test is to verify if update query is

Mock a superclass constructor

谁说我不能喝 提交于 2019-12-23 17:40:27
问题 I would like to know if I can mock a super class constructors call and its super() calls. For example, I have the following classes class A { A(..) { super(..) } } class B extends A { B(C c) { super(c) } } So, I am planning to unit test some methods in class B, but when creating an instance it does call the super class constructors making it tough to write unit tests. So, how can I mock all the super class constructor calls. Also I would like to mock few methods in class A so that it does

JUnit, JMock, JUnitRuleMockery: what am I missing

前提是你 提交于 2019-12-23 16:53:43
问题 This is strange: I am using JUnit with JMock using the Rule JUnitRuleMockery for a while and it always worked perfectly fine: expectations where checked at the end of the test and if one (or more) was missing the test was failing. However, this snippet is not working for me in Eclipse ( DAO is the interface described in org.mongodb.morphia.dao.DAO ): @Rule public JUnitRuleMockery context = new JUnitRuleMockery(); private DAO<Cobrand, ObjectId> dao = context.mock(DAO.class); private final

What is the best way to use Guice and JMock together?

被刻印的时光 ゝ 提交于 2019-12-23 08:53:27
问题 I have started using Guice to do some dependency injection on a project, primarily because I need to inject mocks (using JMock currently) a layer away from the unit test, which makes manual injection very awkward. My question is what is the best approach for introducing a mock? What I currently have is to make a new module in the unit test that satisfies the dependencies and bind them with a provider that looks like this: public class JMockProvider<T> implements Provider<T> { private T mock;