spring-webmvc升级时出现NoSuchMethodError

时间:2020-01-22 17:24:11

标签: java maven spring-mvc junit mockito

我们已升级到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>

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

依赖项中是否有特定版本的spring-test?

不久前,我遇到了类似的问题-事实证明,弹簧测试有严格的版本限制,并将其升级到5.2.3-RELEASE可以解决此问题。

答案 2 :(得分:0)

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