easymock

How mock private method that modify private variables?

烂漫一生 提交于 2019-12-01 06:17:49
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 = ... } } 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 depends on another object for doing this calculation. Then you can mock this dependency of the class under test.

Java generics and overloading with Groovy

耗尽温柔 提交于 2019-12-01 05:35:18
问题 I write my unit tests for my Java application with Groovy, JUnit and EasyMock. In EasyMock there are several overloaded methods capture() which have been deprecated with the note "Because of harder erasure enforcement, doesn't compile in Java 7". The methods take as parameter an object of type Capture<T> . There exist, among others, the following methods: static boolean capture(Capture<Boolean> captured) static boolean capture(Capture<Integer> captured) ... static <T> T capture(Capture<T>

EasyMock: Mock out a constructor call in java

情到浓时终转凉″ 提交于 2019-12-01 03:54:39
I have a looked at similar questions on this board, but none of them answer my question. This sound strange, but is it possible to mock out a constructor call on the object you're mocking. Example: class RealGuy { .... public void someMethod(Customer customer) { Customer customer = new Customer(145); } } class MyUnitTest() { public Customer customerMock = createMock(Customer.class) public void test1() { //i can inject the mock object, but it's still calling the constuctor realGuyobj.someMethod(customerMock); //the constructor call for constructor makes database connections, and such. } } How

EasyMock: Mocked object is calling actual method

ε祈祈猫儿з 提交于 2019-12-01 03:27:34
I've following code snippet in my unit test, ClassToBeMocked mock = createMock(ClassToBeMocked.class); //I've statically imported EasyMock.* mock.callMethod(); //This is a void method expectLastCall(); replay(mock); But when I run the test, instead of seeting up the expectaion, callMethod() is actually called. Am I doing something wrong? I'm fairly new to EasyMock or any mocking framework and blocked because of this problem. Any help would be greatly appreciated. Thanks, AndyS This will happen if you are mocking a class with a 'final' method. EasyMock does not override a final method. If you

Java unit test can't access ResourceBundle

ε祈祈猫儿з 提交于 2019-12-01 03:24:54
问题 I am creating a Java unit test to test some code I recently changed. However, the method I am testing instantiates a class which uses ResourceBundle … ResourceBundle.getBundle("businessVariables").getString("product.name")); The resource file lives in the web package at Mycompany_web/src/main/webapp/WEB-INF/classes/businessVariables.properties My test lives in my xml package at Mycompany_xml/src/test/java/uk/co/mycompany/xmlapi/RequestProcessorTestNew.java During normal runtime the resource

Mock private static final variables in the testing class

房东的猫 提交于 2019-12-01 00:40:06
I have a few private static final fields in the class I want to test. Like follows public class ClassToTest{ .... private static final Myclass myclass = MyClassFactory.getMyClass(type.firstType); .... } The type is a enum in the MyClassFactory. That factory do is it initialize object according to type passed and return. My question is does powermock support this and if so how to do this. You can use reflection also if any mock library works for you. Field f = classToTest.getclass().getDeclaredField("myclass "); f.setAccessible(true); f.set(classToTest,/*NEW VALUE*/); DaveH PowerMock ( + a

EasyMock: Mock out a constructor call in java

痴心易碎 提交于 2019-12-01 00:36:33
问题 I have a looked at similar questions on this board, but none of them answer my question. This sound strange, but is it possible to mock out a constructor call on the object you're mocking. Example: class RealGuy { .... public void someMethod(Customer customer) { Customer customer = new Customer(145); } } class MyUnitTest() { public Customer customerMock = createMock(Customer.class) public void test1() { //i can inject the mock object, but it's still calling the constuctor realGuyobj

Testing private method using power mock which return list of Integers

不羁岁月 提交于 2019-11-30 21:45:32
问题 I have a private method which take a list of integer value returns me a list of integer value. How can i use power mock to test it. I am new to powermock.Can i do the test with easy mock..? how.. 回答1: From the documentation, in the section called "Common - Bypass encapsulation": Use Whitebox.invokeMethod(..) to invoke a private method of an instance or class. You can also find examples in the same section. 回答2: Here is a full example how to do to it: import java.util.ArrayList; import java

How to test void methods using EasyMock

…衆ロ難τιáo~ 提交于 2019-11-30 17:13:24
I've seen a few questions out there regarding this but I can't seem to make sense of any of the answers for my particular problem. I have a mock object, lets call "object1", which I send to some method for testing, lets call testMethod(). So I end up calling testMethod(object1); for testing. Now somewhere in this testMethod, there will be a part where it calls a method object1.toggleDisplay(); which is a void method. If the method were like object1.getDisplay() where it actually returns something, I usually do EasyMock.expect(object1.getDisplay()).andReturn(whatever); However, this is a void

EasyMock void method

自闭症网瘾萝莉.ら 提交于 2019-11-30 16:53:56
I'm trying to use EasyMock to mock out some database interface so I can test the business logic off a wrapping method. I've been going ok with methods that return by using the following in my setup of my test. DBMapper dbmapper = EasyMock.createMock(DBMapper.class); userService.setDBMapper(dbmapper); then within my actual test I run EasyMock.expect(dbmapper.getUser(userId1)).andReturn(mockUser1); EasyMock.replay(dbmapper); userService.getUser(userId1); This service then connects to the dbmapper and returns the object (the mapper is injected using setter methods) These type of mocks seem to