java - EasyMock with withConstructor ignoring addMockedMethod called within constructor -


I want to test a method of a class. For this I should be duplicate the second method of class, which has been told from the constructor. Apart from this, I have to pass a cool object constructor. When I'm not using the constellation, it's choosing addMockedMethod properly. But whenever I am experimenting with the constellator, it is not using the method given in the addMockedMethod. (Obtaining an exception for the following code) Can I do anything here to solve it? The following code is

main category:

  public class A {bb; Cc; Public A (B _ B) {B = _ B; C = getC (); } Public Zero Run (string _value) {string val = b.get value (); // something to do) public stable getc () {StaticD.getC (); }} Public class StaticD {public static C getC () (new runtime exception ("error");}} test {test (name test = name = "ATest") extends the public class asthma EasyMockSupport {public void testRun ()) {Bbmcok =  

createMock (B.class); Expect (bMock.getValue ()) andReturn ("test"). replay all(); An obj = createMockBuilder (A class). AddMockedMethod ("getc"). WithConstructor (bMock) CreateMock (); Obj.run (); VerifyAll (); reset all(); }

meet code> a class When it is said during the instant, he is never working with easy mock. You are basically trying to call some object before it was made, so it can not work.

After saying this, Powermock is your friend here. Powermock has the ability to duplicate the way that it can not do Izak. Here in your example, it will be very helpful to powermoke the ability to duplicate in a steady manner.

Here is a sample test that I have put together for your case, which will allow you to make your test. It also allows you to create an actual A object because you are not trying to duplicate it in any way.

  asks @RunWith (PowerMockRunner.class) / / category to use PowerMock @PrepareForTest (StaticD.class) to prepare the / / class that you call Powermock on public class esters Want to use, EasyMockSupport {@Test Public Null Tester ()) Reaches {Last Bbm = vs. Mac (Bexas); Last c cock = vs (c. Square); PowerMock.mockStatic (StaticD.class); // EasyMock.expect (StaticD.getC) creates all static methods of this class available for joke. And Raton (CMcock); // Expected behavior adds PowerMock.replay (StaticD.class); // AIRAL = NEW A (BMCOC) to powermok to run the stable class final again; EasyMock.expect (bMock.getValue ()). And return ("test"); replay all(); AReal.run ("test"); VerifyAll (); reset all(); }}  

The version of PowerMock requires you depending on the version of the genus that you are using, but all these types of things


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -