I'm trying to run unit tests but I'm getting it, but to no avail. my test
@RunWith(SpringRunner.class)
@SpringBootTest(value = "SpringBootTest.WebEnvironment.MOCK")
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class BoletoResourceTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private BoletoService boletoService;
@Test
@WithMockUser(username = "sa")
public void criarNovoBoletoTeste() throws Exception {
Boleto mockBoleto = new Boleto(1L, "Testando XUXU");
Mockito.when(boletoService.inserir(Mockito.any(Boleto.class))).thenReturn(mockBoleto);
ObjectWriter objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
String mockBoletoJSON = objectWriter.writeValueAsString(mockBoleto);
this.mockMvc.perform(MockMvcRequestBuilders.post("/api/teste")
.with(SecurityMockMvcRequestPostProcessors.csrf())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(mockBoletoJSON))
.andExpect(MockMvcResultMatchers.status().is(201))
.andExpect(MockMvcResultMatchers.content().json(mockBoletoJSON));
}
}
And the error I'm getting.
org.json.JSONException: Unparsable JSON string:
Im using jdk 11 and gradle 4.10.2
Hello,
RépondreSupprimerI had the same issue and as a solution:
You need to replace this line
Mockito.when(boletoService.inserir(Mockito.any(Boleto.class))).thenReturn(mockBoleto);
with the following line:
given(boletoService.inserir(any(Boleto.class))).willReturn(mockBoleto);