So I am trying to add integration test to my API, I tested all of the GET/POST/PUT.. methods via Postman and they all work, however I cannot get them to work on JUnit with mockito, any ideas?
@SpringBootTest
@AutoConfigureMockMvc
public class ResponseStatusControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
private String content;
@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new NsaController())
.build();
try(FileInputStream inputStream = new FileInputStream("\\java\\resources\\post.json")) {
content = IOUtils.toString(inputStream);
}catch (Exception e){
e.printStackTrace();
}
}
@Test
public void endpointTest() throws Exception {
this.mockMvc.perform(post("/api/v1/nsascholarship",content)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk());}
}
The code should take the JSON data and post it to the H2 database, however depending on how I give the JSON object (in a string) I get:
Status expected:<200> but was:<400> (400 is when the provided data has nulls where there should be data)
My assumption is for some reason I am providing the JSON body incorrectly and the method is unable to add it to the database.
TLDR; Post requests works on Postman, does not work on Junit. I think its because I am not providing the JSON content correctly via the test method, any ideas?