【发布时间】: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