简体   繁体   中英

How to mock final variable initialized inside constructor ,using EasyMock and PowerMock?

Below mentioned code is implementation class, Need to mock the final variables inside junit .And to set the required value.

public class XyzInterceptor 
    private final int requestLimit;
    private final int responseLimit;
    LimitHealper limitHealper = LimitHealper.getInstance();
    //Constructor
    public XyzInterceptor() {
        requestLimit = limitHealper.getLimits("fff");
        responseLimit = limitHealper.getLimits("fff");
            }
            
        public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) {

        // Check if rate limit is disabled
        if (requestLimit == 0 && responseLimit == 0) {
            return true;
        }
        
        //logic inside
        
        }
}

Test Method

@Test
    public void preHandelWhenLimitIsavailable() {       
        
        MockHttpServletRequest req = get("/customResource")
                .buildRequest(new MockServletContext());        
              
        MockHttpServletResponse resp = new MockHttpServletResponse();           
            
        XyzInterceptor crrli = PowerMock.createPartialMock(XyzInterceptor.class, "XyzInterceptor");
        EasyMock.expect(limitHealper.getLimits(EasyMock.anyObject(String.class))).andReturn(6000).anyTimes();
        EasyMock.replay(limitHealper);
        crrli.preHandle(req, resp, "doesn't matter");        
        Assert.assertEquals(200,resp.getStatus());
        
    }

While running the test getting below error org.powermock.reflect.exceptions.MethodNotFoundException: No methods matching the name(s) XyzInterceptor were found in the class hierarchy of class com.xx.xx.xx.xx.XyzInterceptor.

Note : On top of the class i have these annotation @RunWith(PowerMockRunner.class) @PrepareForTest(XyzInterceptor .class)

I will heavily advocate for a design improvement. Do not use singleton and inject through the constructor. You will get

public class XyzInterceptor {
    private final int requestLimit;
    private final int responseLimit;
    
    public XyzInterceptor(LimitHealper limitHealper) {
        requestLimit = limitHealper.getLimits("fff");
        responseLimit = limitHealper.getLimits("fff");
    }

    public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) {

        // Check if rate limit is disabled
        if (requestLimit == 0 && responseLimit == 0) {
            return true;
        }

        //logic inside

    }
}

so you test becomes

@Test
public void preHandelWhenLimitIsavailable() {
    LimitHealper limitHealper = mock(LimitHealper.class);
    expect(limitHealper.getLimits(anyObject(String.class))).andStubReturn(6000);
    replay(limitHealper);

    XyzInterceptor crrli = new XyzInterceptor(limitHealper);

    MockHttpServletRequest req = get("/customResource").buildRequest(new MockServletContext());
    MockHttpServletResponse resp = new MockHttpServletResponse();
    crrli.preHandle(req, resp, "doesn't matter");
    
    assertEquals(200,resp.getStatus());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM