2

I'm trying to mock ContainerRequestContext using Mockito, but i got the follow error:

java.lang.ClassFormatError: Absent Code attribute in method
    that is not native or abstract in class file
    javax/ws/rs/core/Response

My goal is set headers inside context to test an interceptor class.

@RunWith(MockitoJUnitRunner.class) public class
MyInterceptorTest



@Mock
ContainerRequestContext context;

@Mock
MyService service;

@InjectMocks
MyInterceptor interceptor;



@Test
public void shouldAuthorizeUsingHEader() {
    when(context.getHeaderString("header1")).thenReturn("123456");
    when(context.getHeaderString("header2")).thenReturn("BigBall");

    interceptor.filter(context);

    verify(context, never()).abortWith(any(Response.class));
}
DwB
  • 37,124
  • 11
  • 56
  • 82
Vipercold
  • 599
  • 1
  • 7
  • 27
  • provide some code examples. I only can suppose that you simply mock it in this way: ContainerRequestContext requestContext = mock(ContainerRequestContext.class); and the ClassFormatError thrown. – MeetJoeBlack Jun 19 '18 at 18:14
  • Updated @MeetJoeBlack – Vipercold Jun 19 '18 at 18:19
  • You are still not showing enough. Are you using `@RunWith(MockitoJunitRunner.class)` are you calling `MockitoAnnotations.init(this)` perhaps something else? – DwB Jun 19 '18 at 18:46
  • I'm properly instantiating the Mock – Vipercold Jun 19 '18 at 18:59
  • Your problem is caused by something you are not telling us. I replicated your test (as described in your question) and there is no issue. Based on the error message, the problem appears to be caused by incompatabile jars on your class path. For my test I used only `javax.ws.rs-api` – DwB Jun 19 '18 at 21:09
  • Here is a "similar" issue. Your cause may be the similar; test dependencies differ from runtime dependencies. Possibly a classpath ordering issue. – DwB Jun 19 '18 at 21:13
  • A post here: https://stackoverflow.com/questions/15948598/classformaterror-absent-code-attribute-in-method-that-is-not-native-or-abstract suggests that you may need to make the non-api jars available during test phase. – DwB Jun 19 '18 at 21:19

2 Answers2

1

Try this
Instead of using the when().thenReturn() mocking pattern, try the doReturn().when() mocking pattern.

For example, change this: when(context.getHeaderString("header1")).thenReturn("123456");

to this: doReturn("123456").when(mockContext).getHeaderString("header1")

Also, consider using the prefix mock when naming your mock objects. It can be surprisingly helpful when reading the junits six months after you wrote them.

I found a different suggestion at Mkyong make sure you are using the correct javaee.jar in your dependencies. Again, not an exact match though.

DwB
  • 37,124
  • 11
  • 56
  • 82
1

Just remove or update the following dependency and everything works fine:

     <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
Vipercold
  • 599
  • 1
  • 7
  • 27