easymock


How to use EasyMock expect

会有一股神秘感。 提交于 2020-01-13 11:23:36
问题 The expect doesn't seem to work for me: package com.jjs.caf.library.client.drafting; import static org.junit.Assert.*; import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; import com.jjs.caf.library.client.CustomerManager; import com.jjs.caf.library.client.UserBookLimiter; public class DraftTest { UserBookLimiter userBookLimiter; int expected = 5; @Before public void setUp() throws Exception { userBookLimiter = EasyMock.createMock(UserBookLimiter.class); EasyMock

Testing code which calls native methods

痴心易碎 提交于 2020-01-12 07:10:11
问题 I have a class like this: public final class Foo { public native int getBar(); public String toString() { return "Bar: " + getBar(); } } Please note that getBar() is implemented with JNI and that the class is final . I want to write a junit test to test the toString() method. For this I need to mock the getBar() method and then run the original toString() method to check the output. My first thought was that this must be impossible but then I found PowerMock which supports testing final

Unit testing by mocking the data layer or using embedded database

我与影子孤独终老i 提交于 2020-01-06 09:00:08
问题 For unit testing is it better to mock the data layer or use an embedded database like derby? I know that it also depends on the purpose of the testing. But if I go with derby I don't have to mock all the objects and I assume that would be easier. On the other hand I understand that that is more towards integration testing. So which one is more common for unit testing? Thanks. Update according to comments: So I have derby configured now but my manager insists on using easymock. We are using

How to inject EasyMock mock into tested class private field

雨燕双飞 提交于 2020-01-03 13:16:08
问题 I'm using EasyMock to create mock that is one of private parameters (without setter) in tested class. I tried using reflection - but it does not work correctly. public class TestedClassTest{ @Test public void test(){ TestedClass instance = new TestedClass(); MockedClass mocked = EasyMock.createMock(MockedClass.class); Data data = new Data(); //Void setter DataType dataType = (myDataType.DataType) EasyMock.anyObject(); mocked.setDataType(dataType); EasyMock.expectLastCall(); //expect EasyMock

Unit-testing a class that calls a static method

[亡魂溺海] 提交于 2020-01-03 13:04:50
问题 I am trying to unit-test a class 'A' which calls a static method of a class 'B'. Class 'B' essentially has a google guava cache which retrieves a value(Object) from the cache given a key, or loads the object into the cache (in case of a cache-miss) using a service adapter. The service-adapter class in turn has other autowired dependencies to retrieve the object. These are the classes for illustration purposes: Class A public class A { public Object getCachedObject(String key) { return B

Unit-testing a class that calls a static method

时光总嘲笑我的痴心妄想 提交于 2020-01-03 13:04:07
问题 I am trying to unit-test a class 'A' which calls a static method of a class 'B'. Class 'B' essentially has a google guava cache which retrieves a value(Object) from the cache given a key, or loads the object into the cache (in case of a cache-miss) using a service adapter. The service-adapter class in turn has other autowired dependencies to retrieve the object. These are the classes for illustration purposes: Class A public class A { public Object getCachedObject(String key) { return B

Test a void method that redirect foward

放肆的年华 提交于 2020-01-02 08:03:07
问题 How can I test a void method that redirects me with RequestDispatcher? What I made until now. public void testAuthAction_userNull() { HttpServletRequest requestMock = createMock(HttpServletRequest.class); HttpServletResponse responseMock = createMock(HttpServletResponse.class); expect(requestMock.getSession().getAttribute("user")).andReturn(null); replay(requestMock); AuthAction action = new AuthAction(); RequestDispatcher rd = requestMock.getRequestDispatcher("/User/login.jsp"); } the method

Mocking Clojure protocols

落花浮王杯 提交于 2020-01-02 04:39:19
问题 Can one of the popular Java mocking frameworks like EasyMock or Mockito be used to mock Clojure protocols defined with defprotocol ? If so, how? 回答1: You should be able to mock protocols using any mock library. Under the covers, every protocol uses a Java interface as an implementation detail, and you could just mock that interface. That said, don't do this! Mocking in Java is absurdly complex because of reflection, protection levels, final classes, etc. Any time you want a Clojure object

EasyMock: How do I create a mock of a genericized class without a warning?

喜你入骨 提交于 2019-12-31 17:37:49
问题 The code private SomeClass<Integer> someClass; someClass = EasyMock.createMock(SomeClass.class); gives me a warning "Type safety: The expression of type SomeClass needs unchecked conversion to conform to SomeClass<Integer>". 回答1: AFAIK, you can't avoid the unchecked warning when a class name literal is involved, and the SuppressWarnings annotation is the only way to handle this. Note that it is good form to narrow the scope of the SuppressWarnings annotation as much as possible. You can apply

How mock private method that modify private variables?

…衆ロ難τιáo~ 提交于 2019-12-30 10:19:27
问题 How mock private method that modify private variables? class SomeClass{ private int one; private int second; public SomeClass(){} public int calculateSomething(){ complexInitialization(); return this.one + this.second; } private void complexInitialization(){ one = ... second = ... } } 回答1: You don't , because your test will depend on implementation details of the class it is testing and will therefore be brittle. You could refactor your code such that the class you are currently testing

工具导航Map