简体   繁体   English

带有 spring-webmvc 升级的 NoSuchMethodError

[英]NoSuchMethodError with spring-webmvc upgrade

We upgraded to spring-webmvc 5.2.3.RELEASE .我们升级到spring-webmvc 5.2.3.RELEASE
This is causing a JUnit to fail.这会导致 JUnit 失败。
I think the issue is with the .build() method - it can't be found.我认为问题在于.build()方法 - 它无法找到。

The code:编码:

@RunWith(MockitoJUnitRunner.class)
public class CommunicationDeliveryControllerTest {

    @InjectMocks
    CommunicationDeliveryController controller = new CommunicationDeliveryController();

    @Mock
    private RequestHandler requestHandler;

    @Mock
    private HttpServletRequest httpServletRequest;

    private MockMvc mockMvc;
    private Gson gson = new Gson();
    private CommunicationDeliveryController spiedController;

    @Before
    public void setup() {
        spiedController = spy(controller);
        mockMvc = standaloneSetup(spiedController).build();
    }

    @Test
    public void initiateBatchRunTest() throws Exception{
        MvcResult result = mockMvc.perform(post("/api/BatchRun").contentType(MediaType.APPLICATION_JSON_VALUE).content(gson.toJson(BulkCommunicationRequestData.getBulkEmailRequestForPositive()))).andReturn();
        assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
    }

    @Test
    public void deliverCommunicationTest() throws Exception{
        MvcResult result = mockMvc.perform(post("/api/deliverCommunication").contentType(MediaType.APPLICATION_JSON_VALUE).content(gson.toJson(BulkCommunicationRequestData.populateAllEmailDetail()))).andReturn();
        assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
    }
}

The error:错误:

java.lang.NoSuchMethodError: org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StandaloneConfiguration.getInterceptors()[Ljava/lang/Object;
        at company.custcomm.service.communicationdelivery.controllers.CommunicationDeliveryControllerTest.setup(CommunicationDeliveryControllerTest.java:45)

Are there compatibility issues between our versions of spring-webmvc, mockito, and JUnit?我们的 spring-webmvc、mockito 和 JUnit 版本之间是否存在兼容性问题?

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>

我将所有 spring 依赖项升级到 4.3.18-RELEASE,然后就没有错误了。

Is there a specific version of spring-test in your dependencies?您的依赖项中是否有特定版本的 spring-test?

I ran into a similar problem a while ago - turned out that there was a strict version limit for spring-test, and upgrading it to 5.2.3-RELEASE resolved the issue.不久前我遇到了类似的问题 - 结果是 spring-test 有严格的版本限制,并将其升级到 5.2.3-RELEASE 解决了这个问题。

If you do not want to change the version of your Junit and spring-webmvc then you can specify spring-test version explicitly in your pom.xml.如果您不想更改 Junit 和 spring-webmvc 的版本,那么您可以在 pom.xml 中明确指定 spring-test 版本。 This works for me.这对我有用。

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>5.2.0.RELEASE</version>
           <scope>test</scope>
      </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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