2

Currently am writing Junit for our webservice code.

WebService Code and Junit code is written in the code section

When I run the Junit am getting the below error

java.lang.AbstractMethodError: javax.ws.rs.core.Response$ResponseBuilder.status(ILjava/lang/String;)Ljavax/ws/rs/core/Response$ResponseBuilder;
at javax.ws.rs.core.Response$ResponseBuilder.status(Response.java:921)
at javax.ws.rs.core.Response.status(Response.java:592)
at javax.ws.rs.core.Response.status(Response.java:603)
at javax.ws.rs.core.Response.ok(Response.java:638)
at javax.ws.rs.core.Response.ok(Response.java:650)
at com.renault.rntbci.stl.service.demande.impl.ListDemandServiceImpl.getDemandeListCount(ListDemandServiceImpl.java:281)
at com.renault.rntbci.stl.service.demande.impl.ListDemandeTest.testGetDemandeListCount(ListDemandeTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

One of my friend advised me to change the scope to test in pom.xml for jax rs version as below.

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>jaxrs-api</artifactId>
        <scope>test</scope>
        <version>2.3.7.Final</version>
    </dependency>

But still the same error is persisting. Kindly anyone please help me with a solution.

Thanks a lot in advance.

Note: This is an existing application and I am new to this project which is with Java 1.7

public Response someMethod(String stringValue){

    Map<String, Long> outputMap = new TreeMap<String, Long>();

    try {
        Long outputValue = service.getCount(stringValue);
        //this call goes to a service and gets the db count for a table
        outputMap.put("listCount", outputValue);

    } catch (Exception e) {
        logger.info(e.getMessage(), e); 
    }
    return Response.ok(outputMap).build();
}

And Junit code is

@Mock
Service service;

@Mock
Response res;
@Test
public void testSomeMethod() throws SQLException {
    String stringValue="12";
    Long returnValue=10L;

    when(service.getCount(stringValue)).thenReturn(returnValue);
    res=obj.someMethod(stringValue);
    Assert.assertNotNull(res);
}
Xavi
  • 20,111
  • 14
  • 72
  • 63
Y M
  • 21
  • 1
  • 2
  • Are you using Jersey? – entpnerd Jan 24 '19 at 08:37
  • No am using resteasy – Y M Jan 24 '19 at 09:34
  • I'm assuming that you're using an IDE. Would you try to find your class in your project (searching non-project files)? Is it coming from more than one JAR file? Is `javax.ws.rs.core.Response$ResponseBuilder` coming from two different JAR files? I suspect that you are actually using two different versions of RestEasy due to a diamond dependency issue, but don't have enough info to confirm that. – entpnerd Jan 24 '19 at 17:22
  • I saw the following jax rs dependencies in the pom.xml file org.jboss.resteasy jaxrs-api 2.3.7.Final org.jboss.resteasy resteasy-multipart-provider 2.3.7.Final org.jboss.resteasy jaxrs-api – Y M Jan 25 '19 at 05:16
  • org.jboss.spec.javax.ws.rs jboss-jaxrs-api_1.1_spec 1.0.1.Final And this one as well. – Y M Jan 25 '19 at 05:24

1 Answers1

3

I suspect that what's happening here is that you have two different versions of the JAR file containing the javax.ws.rs.core.Response$ResponseBuilder class. Between the two versions of this class that you have floating around, the abstract status() method changed. You need to go through your list of Maven dependencies by running the mvn dependency:tree command as per this answer. You need to identify the two different JAR versions containing the two different javax.ws.rs.core.Response$ResponseBuilder classes. Exclude the dependency version you don't want and keep the version you do want to keep.

It could be as simple as deleting the jboss-jaxrs-api_1.1_spec Jar file but depending on what other dependencies you have (and which transitive dependencies) there may be more to it than that.

entpnerd
  • 10,049
  • 8
  • 47
  • 68