easymock

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

ぐ巨炮叔叔 提交于 2019-12-06 05:19:28
问题 Object A { def a = { something} } // I've import A, but still have error message: not found: type A val x = mock[A] 回答1: 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. 回答2: 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

Is it possible to mock a single method in an already existing object?

夙愿已清 提交于 2019-12-06 03:24:21
For an integration test, I need to mock a specific method in a java service client without destroying the rest of the information in it. It has no self-constructor, so a solution like this is out of the question: private DBClient mockClient = new DBClient(alreadyExistingClient){ @Override void deleteItem(Item i){ //my stuff goes here } }; Is there a way to mock the deleteItem method such that the credentials, endpoints, etc... are preserved in an existing DBClient object? edit: mockito is not available for use in this case cahen You can use a Dynamic Proxy to intercept any method invocation

How Configure EasyMock Class Extension 3.1?

廉价感情. 提交于 2019-12-06 01:05:50
I want to add EasyMock Class Extension 3.1 to my project and I have a problem with dependencies of EasyMock 3.1 CE. I add dependencies : cglib-2.2.2.jar and asm-4.0.jar and throws exception : java.lang.VerifyError: class net.sf.cglib.core.DebuggingClassWriter overrides final method visit.(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V When I use cglib-nodep-2.1_3.jar and asm-4.0.jar throws another exception: java.lang.NoClassDefFoundError: org/objenesis/ObjenesisHelper at org.easymock.internal.ObjenesisClassInstantiator.newInstance(ObjenesisClassInstantiator.java

Test a void method that redirect foward

旧城冷巷雨未停 提交于 2019-12-05 20:07:35
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 I want to the test is. public void execute(HttpServletRequest request, HttpServletResponse response) {

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

限于喜欢 提交于 2019-12-05 19:05:27
问题 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

JUnit mocking with Mockito, EasyMock, etc

╄→гoц情女王★ 提交于 2019-12-05 17:30:00
I'm trying to mock a method of an object inside the class I'm testing. For instance class ClassToTest { public doSomething () { SomeObject a = new SomeObject (); a.doSomethingElse (); } } Is there a way to mock the methods of the variable "a"? I'd like doSomethingElse to do nothing during testing. I'm currently using Mockito but I'm open to any mocking framework. Thanks Yes, there is a way, as shown by the following JMockit test: public void testDoSomething(final SomeObject mock) { new ClassToTest().doSomething(); new Verifications() {{ mock.doSomethingElse(); }}; } No need to refactor code

How to use EasyMock expect

[亡魂溺海] 提交于 2019-12-05 13:17:06
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.expect(userBookLimiter.getMaxNumberOfBooksAllowed()).andReturn(5); } @Test public final void test() {

Mocking Clojure protocols

断了今生、忘了曾经 提交于 2019-12-05 10:49:10
Can one of the popular Java mocking frameworks like EasyMock or Mockito be used to mock Clojure protocols defined with defprotocol ? If so, how? Stuart Dabbs Halloway 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 that implements a protocol, simply call reify , e.g. (defprotocol Foo (method-a [_])

How do I mock a method inherited from an abstract class with EasyMock?

淺唱寂寞╮ 提交于 2019-12-05 10:10:26
I'm struggling with EasyMock. I've written two small classes to illustrate my problem: public abstract class A { private AtomicReference<Integer> id = new AtomicReference<Integer>(null); public final int getId() { return id.get(); } public final boolean setId(int id) { return this.id.compareAndSet(null, id); } } public class B extends A { } Then I proceed to write a test method as follows: public class EasyMockTester extends EasyMockSupport { @Test public void test() { B b = EasyMock.createStrictMock(B.class); EasyMock.expect(b.getId()).andReturn(100); replayAll(); int id = b.getId(); System

Mocking a final method with PowerMock + EasyMock

↘锁芯ラ 提交于 2019-12-05 06:48:21
I'm trying to mock a call to the final method ResourceBundle.getString() . With PowerMock 1.4.12 and EasyMock 3.1, the call is not being mocked; instead, the "real" method is called. My test class: @RunWith(PowerMockRunner.class) @PrepareForTest(ResourceBundle.class) public class TestSuite { @Before public void setUp() throws Exception { ResourceBundle resourceBundleMock = PowerMock.createNiceMock(ResourceBundle.class); expect(resourceBundleMock.getString(BundleConstants.QUEUE)).andReturn("Queue"); PowerMock.replay(resourceBundleMock); beanBeingTested.setMessages(resourceBundleMock); } ... }