Junit throwing java.lang.AbstractMethodError: javax.ws.rs.core.Response$ResponseBuilder.status

牧云@^-^@ 提交于 2020-02-25 02:23:28

问题


Currently am writing Junit for our webservice code.

WebService Code and Junit code is written in the code sectioon

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);
}

回答1:


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.



来源:https://stackoverflow.com/questions/54342244/junit-throwing-java-lang-abstractmethoderror-javax-ws-rs-core-responseresponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!