【问题标题】:NoSuchMethodError with spring-webmvc upgradeNoSuchMethodError 与 spring-webmvc 升级
【发布时间】:2020-05-08 22:23:01
【问题描述】:

我们升级到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-webmvc,您需要将所有 spring 依赖项升级到相同版本。像这样的错误/异常来自混合来自不同版本的 Spring 的 jar。

标签: java maven spring-mvc junit mockito


【解决方案1】:

我把所有spring依赖升级到4.3.18-RELEASE,就没有报错了。

【讨论】:

    【解决方案2】:

    您的依赖项中有特定版本的 spring-test 吗?

    不久前我遇到了类似的问题 - 原来 spring-test 有严格的版本限制,将其升级到 5.2.3-RELEASE 解决了这个问题。

    【讨论】:

      【解决方案3】:

      如果您不想更改 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-12
        • 2016-10-26
        • 1970-01-01
        • 2022-08-24
        • 2023-04-03
        • 2021-05-14
        • 2020-10-27
        • 2015-01-08
        相关资源
        最近更新 更多