I have an old test in code which uses EasyMock instead of Mockito. When I run test locally then it works, however when it runs in gitlab pipeline then I get:
junit.framework.AssertionFailedError:
Expectation failure on verify:
finalize(): expected: 1, actual: 0
finalize(): expected: 1, actual: 0
at org.easymock.internal.MocksControl.verify(MocksControl.java:184)
at myproject.mocks.EasyMockTestCase.verify(EasyMockTestCase.java:72)
at myproject.controllers.TestController.userIsAbleToSave(TestController.java:132)
of course I can see a lot of the same questions on stackoverflow and in the net but here it's strange because of finalize() method.
My code:
@RequestMapping(value = ACTION, params = "buttonSubmit")
@Secured(Permissions.UPDATE_INSTRUMENT)
public ModelAndView buttonSubmit(TypeRequest typeRequest, final HttpServletRequest request) throws Exception {
ModelAndView mav = getView();
Product product;
try {
product = productLifeCycleService.createNew(new AppRequest(request));
} catch (DuplicateException e) {
return handleDuplicate(typeRequest, request, false, e);
} catch (ValidationException e) {
return handleValidation(request, false, e);
}
return redirectToProductDetail(mav, product.getId());
}
and test for this:
@Test
public void userIsAbleToSave() throws Exception {
expect(productLifeCycleService.createNew((Request)anyObject())).andStubReturn(product);
expect(product.getId()).andStubReturn(STANDARD);
TypeRequest typeRequest = new TypeRequest (QQ, HH, GOAL);
replay();
underTest.buttonSubmit(typeRequest, httpRequest);
verify();
}
Why is it failing on finalize(), why it works locally and don't work on Gitlab? It started to fail in pipeline after adding appender to connect to Splunk in logback.xml.
When product is created this way:
Product product;
product = productLifeCycleService.createNew(new AppRequest(request));
it's also not easy to use Mockito here.
@Edit
I see that adding into logback.xml:
<appender-ref ref="http"/>
spoil test. In @Before I have mockHttpRequest() method which do:
private void mockHttpRequest() {
httpRequest = mock(MockHttpServletRequest.class);
expect(httpRequest.getRequestURI()).andStubReturn(URI);
expect(httpRequest.getHeader(HEADER)).andStubReturn(null);
expect(httpRequest.getParameter(TYPE)).andStubReturn(TypeName.LEGAL.getFormName());
expect(httpRequest.getMethod()).andStubReturn(POST);
}
This is really strange. It it expecting
finalize. It means that an expectation was set for it. But I don't see an expectation set. So it could be that it is set somewhere else in your code, or that it's called by the GC while you are setting your expectations. But I doubt it, because it wouldn't be in the same thread.So my assumption is that a
finalizeis expected somewhere else and should beexpectLastCall().andStubReturn()instead of justmock.finalize().