tests working in mockito 4.x are not working with mockito 5.x getting error after migrating spring from 2.7 to 3.1.4.
Error:
Suppressed: org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced or misused argument matcher detected here
@activeprofiles("test")
@ExtendWith(SpringExtension.class)
@WebMvcTest(SampleApiController.class)
@slf4j
@import({MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class})
public class GetFailedTest {
@MockBean
MockClass Mockobj;
JsonFileReader reader = new JsonFileReader();
@Autowired
MockMvc mockMvc;
@BeforeAll
static void setUpBeforeClass() throws Exception {
System.setProperty("junit", "true");
}
@test
void testSampleFail() throws Exception {
log.info("testSampleFail");
String url = "xxxxx";
when(Mockobj.getMethod(anyString(), org.mockito.Mockito.any(StringBuilder.class)))
.then(new Answer() {
@OverRide
public Integer answer(InvocationOnMock invocation) throws Throwable {
StringBuilder sbd = invocation.getArgument(1);
sbd.append(reader.getJsonStringFromFile(Constants.sample));
throw new RuntimeException("Runtime exception");
}
});
MvcResult result =
this.mockMvc.perform(get(url)).andExpect(status().is5xxServerError()).andReturn();
log.info(result.getResponse().getContentAsString());
}
}
New contributor
shogv48 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5