簡體   English   中英

帶有 spring-webmvc 升級的 NoSuchMethodError

[英]NoSuchMethodError with spring-webmvc upgrade

我們升級到spring-webmvc 5.2.3.RELEASE
這會導致 JUnit 失敗。
我認為問題在於.build()方法 - 它無法找到。

編碼:

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

錯誤:

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)

我們的 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,然后就沒有錯誤了。

您的依賴項中是否有特定版本的 spring-test?

不久前我遇到了類似的問題 - 結果是 spring-test 有嚴格的版本限制,並將其升級到 5.2.3-RELEASE 解決了這個問題。

如果您不想更改 Junit 和 spring-webmvc 的版本,那么您可以在 pom.xml 中明確指定 spring-test 版本。 這對我有用。

    <!-- 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