0

I have this restcontroller that returns paged items with:

 @GetMapping("/api/items") public Page<Item> getPagedItems(Pageable pageable, @RequestParam(defaultValue="") String searchBy, @RequestParam(defaultValue="") String searchValue) { Page<Item> pageItem; switch (searchBy) { case "name" : pageItem = itemRepository.findByNameStartingWith(searchValue, pageable); break; case "description" : pageItem = itemRepository.findByDescriptionStartingWith(searchValue, pageable); break; default: pageItem = itemRepository.findAll(pageable); break; } return pageItem; } 

So far my MvcTest looks like this:

 @Test public void testGetItems() throws Exception{ Page<Item> itemPage = itemRepository.findAll(PageRequest.of(0, 20)); given(itemRepository.findAll(PageRequest.of(0, 20))).willReturn(itemPage) this.mvc.perform(get("/api/items") .contentType(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()) .andExpect(jsonPath("$",hasSize(1))); } 

I want the contents of this /api/items request. It is returning

java.lang.AssertionError: No value at JSON path "$" at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:290) at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74) at org.springframework.test.web.servlet.result.JsonPathResultMatchers.lambda$value$0(JsonPathResultMatchers.java:87) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:179) at com.eprocurement.ItemRestMvcTest.testGetItems(ItemRestMvcTest.java:60) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73) at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) Caused by: java.lang.IllegalArgumentException: json can not be null or empty at com.jayway.jsonpath.internal.Utils.notEmpty(Utils.java:386) at com.jayway.jsonpath.JsonPath.read(JsonPath.java:342) at com.jayway.jsonpath.JsonPath.read(JsonPath.java:329) at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:287) ... 35 more

What can I do to make this test work? What seems to be the problems?

If it helps also I'm using Spring Security. I've annotated the test class with @WithMockUser(roles="ADMIN").

1 Answer 1

2

When you return Page<Item> as a response, the response JSON would be something like:

{ content: [ { id: 1, name: "abc" }, { id: 2, name: "xyz" } ], pageable: { sort: { sorted: false, unsorted: true }, offset: 0, pageSize: 10, pageNumber: 0, paged: true, unpaged: false }, last: true, totalPages: 1, totalElements: 2, size: 10, number: 0, first: true, numberOfElements: 2, sort: { sorted: false, unsorted: true } } 

So, your assertion path should be $.content like .andExpect(jsonPath("$.content",hasSize(1)));

Sign up to request clarification or add additional context in comments.

7 Comments

It still returns java.lang.AssertionError: No value at JSON path "$.content" though also my MockHttServletRepose returns an empty body with a null content type
Oh, then it must be the error with Mock Data setup. I see you are calling Page<Item> itemPage = itemRepository.findAll(PageRequest.of(0, 20)); to setup the mock response. Can you check is itemPage is NULL?
Assuming I have not yet logged in, I get the login page (tested using the browser and Postman), when I'm logged in i get {"content":[{"id":1,"name":"Update item","description":"Update description"},{"id":2,"name":"Item 2","description":"Description\r\n"}],"pageable":{"sort":{"sorted":false,"unsorted":true},"offset":0,"pageNumber":0,"pageSize":20,"paged":true,"unpaged":false},"last":true,"totalPages":1,"totalElements":2,"size":20,"number":0,"sort":{"sorted":false,"unsorted":true},"numberOfElements":2,"first":true}
If it is the problem with user authentication you can use @WithMockUser(roles = "ADMIN") on test method to simulate authenticated request.
You need to add org.springframework.security:spring-security-test maven dependency to use @WithMockUser.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.