0

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?

Tijunelis
  • 13
  • 5
  • In other words, the live code demonstrates that the test is broken. So much for the testing strategy... – access violation Nov 03 '22 at 13:24
  • Would not agree. All of the functionalities are working as expected and this is my first time implementing integration tests onto a project. Postman is a UI so obviously it's a lot easier to use while setting up the tests with it. – Tijunelis Nov 03 '22 at 13:29
  • Did you inspect `content` in a debugger to see that the JSON was read correctly? Does the console output show any error messages from the parsing/binding of the API call ("server" side)? – E-Riz Nov 03 '22 at 13:37
  • Check the content (body) of the response to see if there is anything useful in there to help you troubleshoot. See [this answer](https://stackoverflow.com/a/18336481/639520) for how to do that. – E-Riz Nov 03 '22 at 13:38
  • yes, the JSON string itself is read correctly and no there isin't any parsing/binding issues. – Tijunelis Nov 03 '22 at 13:42
  • @E-Riz one thing is that if I do ".content(content)" instead of "post('url', content)" I receive a null pointer, which Is how I assume that the post requests get's sent with no body, thoughts? – Tijunelis Nov 03 '22 at 13:49
  • @E-Riz apologies for the spam, but using the link you provided, the body is indeed empty in the request with the "post(..)" approach and the ".content(content)" is a null pointer – Tijunelis Nov 03 '22 at 13:51

1 Answers1

0

For one thing, the overloaded method post(String, Object...) that you're calling doesn't do what you think it does. The secondary Object params are to populate the URL template that's passed in the first parameter. See the JavaDoc for explanation. So you need do to call content(String) on the MockHttpServletRequestBuilder that you get back from the post() method.

Now, in order to troubleshoot a problem like this I suggest structuring your test a little differently, to make it easier to debug.

For example:

String content = "{}";
MockHttpServletRequestBuilder request =
        MockMvcRequestBuilders.post("/api/v1/something")
                                .contentType(APPLICATION_JSON)
                                .content(content);
MvcResult result = mvc.perform(request)
                        .andReturn();
                        // andExpect(status().isOk())
                        // andExpect(...)
                        // ;

I would start with that hard-coded JSON content (or something else hard-coded but simple) and use a debugger to check along the way that each local variable is what I expect it to be.

With that, you can inspect the MvcResult that you get back to help determine what's going wrong.

Once you have it working, you can remove the andReturn() if you want and uncomment the assertions (such as andExpect(status().isOk())) .

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • I've tried with .content(content), however with it I receive a nullPointerException, which is not something I get if I test with Postman. Any ideas? – Tijunelis Nov 04 '22 at 07:14
  • Where is the NPE coming from? How can you expect anyone to help you troubleshoot an exception without a stack trace? – E-Riz Nov 07 '22 at 13:57