6

I have created a spring boot application and this is how my controller looks like. I am using postman to send json in request body and a string in request header, then further hashing the json and comparing it with the string got by request header. The problem is I am unaware of getting the request body and request header in order to test the respective Controller class using MockMvc.

Controller Logic

@RestController
public class Comparison {

    @PostMapping(path = "/test")
    public boolean compareHash(@RequestBody String json, 
                               @RequestHeader(value = "code") String oldHashValue) {

        Hash hashObj = new Hash();
        String newHashValue = hashObj.sha512(json);
        return oldHashValue.equals(newHashValue);
    }
}

Test Logic

public class ComparisionTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup () {
        DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
        this.mockMvc = builder.build();
    }

    @Test
    public void contextLoads() throws Exception {
         RecordedRequest recordedRequest = server.takeRequest();
    }
}

Please help me out in the above code to retrieve the body and header value from request and equating the hash(body) with header value

buræquete
  • 14,226
  • 4
  • 44
  • 89
Sunil
  • 63
  • 1
  • 1
  • 4

1 Answers1

10
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApplicationTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test() {

        mockMvc.perform(post("<<url>>").content("<<jsonStrig>>").header("key","value"));
    }

}

In your case:

   @Autowired
    private MockMvc mockMvc;

    @Test
public void test() throws  Exception {

    String jsonString="{\"country\": \"India\", \"currency\": \"INR\", \"president\": \"Ram Nath Kovind\" } ";
    mockMvc.perform(MockMvcRequestBuilders.post("/test").content(jsonString).header("code","12400f74dc4d8d69b713b1fe53f371c25a28a8c5fac2a91eea1f742ab4567c9c"));
}

output:

JSON STRING {"country": "India", "currency": "INR", "president": "Ram Nath Kovind" }  header value 12400f74dc4d8d69b713b1fe53f371c25a28a8c5fac2a91eea1f742ab4567c9c
Barath
  • 5,093
  • 1
  • 17
  • 42
  • Will this send the json string to the req hash function to get hash value n then compare it with the header value – Sunil Sep 10 '17 at 07:17
  • it will reach the controller form there on your function should handle it – Barath Sep 10 '17 at 07:17
  • and I have a doubt that how does it take the json string from the content() and do i need to mention the header name in the "key" ...please do explain in brief since i am new to this thing – Sunil Sep 10 '17 at 08:03
  • check the updated answeer.this will work mockMvc.perform(MockMvcRequestBuilders.post("/test").content("hello").header("code","hello")); – Barath Sep 10 '17 at 08:06
  • the request body in has the JSON String {"country": "India", "currency": "INR", "president": "Ram Nath Kovind" } and request header is 12400f74dc4d8d69b713b1fe53f371c25a28a8c5fac2a91eea1f742ab4567c9c and the key is pkg_hashcode ....this is been sent as POST request from postman ....can you tell me what is that i need to put in the place of "code" – Sunil Sep 10 '17 at 08:10
  • is that json your pojo ? or simply string ? – Barath Sep 10 '17 at 08:12
  • yeah its worked...but the test case is being passed ....i need to check it for the fail case too – Sunil Sep 10 '17 at 08:21