easymock

How do I write a unit test to verify that a function sorts its result? [duplicate]

一个人想着一个人 提交于 2019-12-05 05:18:45
This question already has answers here : How to test the ordering of elements in a Collection in JUnit test? (4 answers) Closed 2 years ago . I have a data source from which I can request a list of people that live in a (any) country, and a method which retrieves the people from that data source and sorts them by their name alphabetically. How should I write my unit test to make sure that the sorting part of my method works properly? This is what my SUT looks like: class PeopleStuff { public IData data; public List<Person> getSortedPeopleForCountry(String countryName) { List<Person> people =

How can I mock a void method to throw an exception?

穿精又带淫゛_ 提交于 2019-12-04 23:56:44
I have a structure like this: public class CacheWrapper { private Map<Object, Object> innerMap; public CacheWrapper() { //initialize the innerMap with an instance for an in-memory cache //that works on external server //current implementation is not relevant for the problem innerMap = ...; } public void putInSharedMemory(Object key, Object value) { innerMap.put(key, value); } public Object getFromSharedMemory(Object key) { return innerMap.get(key); } } And my client class (you could say it looks like this): public class SomeClient { //Logger here, for exception handling Logger log = ...;

Difference between 'same' and 'eq' in EasyMock

∥☆過路亽.° 提交于 2019-12-04 23:22:59
Is there a significant(or even any) difference between 'same' and 'eq' in EasyMock? same checks if both objects are actually the same instance (reference equality). eq calls equals and therefore checks if both have the same value (value equality). Keep in mind that the default equals implementation uses == internally, and therefore eq will do the same as same if you're using classes that do not have a proper equals override. But still, it's better to state your intent by using same for reference equality and eq for value equality. It might also help you finding bugs (i.e. finding that you

EasyMock : java.lang.IllegalStateException: 1 matchers expected, 2 recorded

蓝咒 提交于 2019-12-04 22:57:01
I am having a problem with EasyMock 2.5.2 and JUnit 4.8.2 (running through Eclipse). I have read all the similar posts here but have not found an answer. I have a class containing two tests which test the same method. I am using matchers. Each test passes when run alone. The first test always passes - this is true if I switch the order of the tests in the file. Here is a simplified version of the test code: private Xthing mockXthing; private MainThing mainThing; @Before public void setUp() { mockXthing = EasyMock.createMock(Xthing.class); mainThing = new MainThing(); mainThing.setxThing

How to mock the HttpServletRequest? [duplicate]

孤者浪人 提交于 2019-12-04 15:07:03
问题 This question already has answers here : Creating a mock HttpServletRequest out of a url string? (4 answers) Closed 5 years ago . I have a function that looks for a query parameter and returns a boolean: public static Boolean getBooleanFromRequest(HttpServletRequest request, String key) { Boolean keyValue = false; if(request.getParameter(key) != null) { String value = request.getParameter(key); if(keyValue == null) { keyValue = false; } else { if(value.equalsIgnoreCase("true") || value

How do I mock static function (Object function, not class function) in scala

狂风中的少年 提交于 2019-12-04 11:03:53
Object A { def a = { something} } // I've import A, but still have error message: not found: type A val x = mock[A] You may find this email thread instructive. Whilst pure mocking of the object is not possible with any tool yet, the thread above does have a few options for you. All of which involve changing your design to some degree. You don't. Not only A is not a type or class -- it is an instance -- but it is an instance of a singleton ( A.type ). What you do instead is put your methods on a trait , and make the object extend it. Then, you mock the trait instead of mocking the object. 来源:

Mocking a concrete class using EasyMock

时间秒杀一切 提交于 2019-12-04 03:28:20
问题 Is it possible? How do I do it? 回答1: See the EasyMock Class Extension documentation and download it from the SourceForge project. You can't mock final methods though. EDIT: This is now part of EasyMock for v3 and above, as noted in comments. 回答2: Powermock extends EasyMock and allows you to mock concrete types, even final and static methods. PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and

Is it possible to override a native method in a Java class in Android/dalvik?

只谈情不闲聊 提交于 2019-12-04 02:46:16
I am unit testing a class TestMe using EasyMock, and one of its methods (say method(N n) ) expects a parameter of type N which has a native method (say nativeMethod() ). class TestMe { void method(N n) { // Do stuff n.nativeMethod(); // Do more stuff } } method() needs to invoke N.nativeMethod() at some point, and the problem I'm having is that my Easymock mock object for N is unable to override the native method. I do not own class N but I can refactor TestMe in any way necessary. I decided to make my own class FakeN extends N which overrides nativeMethod to do nothing: class FakeN extends N

EasyMock matcher for class data type

只愿长相守 提交于 2019-12-04 01:46:36
问题 I am having nightmares with the syntax for this and easymock: public void foo(Class<?> clazz); EasyMock.expects(object.foo(EasyMock.isA(???))); What should I be putting if my argument is String.class? I initially thought: EasyMock.isA(((Class<?>)(String.class)).getClass()) Yet when I make the call foo(String.class) I get: java.lang.IllegalStateException: missing behavior definition for the preceding method call: 回答1: You're attempting to verify a generic type that will be erased at runtime

Can I mock a super class method call?

旧巷老猫 提交于 2019-12-04 01:18:42
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 Cem Catikkas Well, you can if you want to. I don't know if you are familiar with JMockit , go check it out. The current version is 0.999.17 In the mean time, let's take a look