I hope someone can help me with this issue. I have searched for issues related to JsonPath and made changes accordingly but I am not getting anywhere.
The error I get is: java.lang.AssertionError: No value at JSON path "$.echo"
In the console I see this
MockHttpServletResponse:
Status = 404
Error message = null
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
The code in question can be found here(original code) https://github.com/rohit-neeshu/UBS_Sample_test/blob/master/src/test/java/com/hackerrank/sample/SampleApplicationTests.java
I have made changes by implementing the service but that didn't help Here's what I did so far:
package com.hackerrank.sample;
import com.hackerrank.sample.dto.StringResponse;
import com.hackerrank.sample.service.HelloService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@AutoConfigureMockMvc
public class SampleApplicationTests {
@Autowired
private MockMvc mockMvc;
@MockBean
HelloService helloService;
@Before
public void setup() {
}
@Test
public void defaultHelloTest() throws Exception {
when(helloService.getDefaultHello()).thenReturn(new StringResponse("Hello World!"));
mockMvc.perform(get("/defaultHello"))
.andDo(print())
.andExpect(jsonPath("$.echo").value("Hello World!"))
.andExpect(
status().isOk());
}
}
Controller
package com.hackerrank.sample.controller;
import com.hackerrank.sample.dto.StringResponse;
import com.hackerrank.sample.service.HelloService;
import org.springframework.web.bind.annotation.*;
@RestController
public class SampleController {
HelloService helloService;
public SampleController(HelloService service) {
this.helloService = service;
}
@GetMapping(value = {"/defaultHello/{message}"})
public StringResponse getDefaultHello(@PathVariable String message) {
return helloService.getDefaultHello(message);
}
@GetMapping(value = {"/defaultHello"})
public StringResponse getDefaultHello() {
return helloService.getDefaultHello();
}
}
Service
package com.hackerrank.sample.service;
import com.hackerrank.sample.dto.StringResponse;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
@Repository
public interface HelloService {
public StringResponse getDefaultHello();
public StringResponse getDefaultHello(String message);
}
Service Implementation
package com.hackerrank.sample.service;
import com.hackerrank.sample.dto.StringResponse;
import org.springframework.stereotype.Service;
@Service
public class HelloServiceImpl implements HelloService {
private StringResponse stringResponse;
public HelloServiceImpl(StringResponse stringResponse) {
this.stringResponse = stringResponse;
}
@Override
public StringResponse getDefaultHello() {
return new StringResponse("Hello World!");
}
@Override
public StringResponse getDefaultHello(String message) {
return new StringResponse("Hello + " + message);
}
}
I think "value()" cannot be used to evaluate JSON . To evaluate JSON structure, we can use is()
.
.andExpect(jsonPath("$.echo", is("Hello World!"))