java mock http_Java MockHttpServletRequest.setParameters方法代码示例

Posted 泡^泡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java mock http_Java MockHttpServletRequest.setParameters方法代码示例相关的知识,希望对你有一定的参考价值。

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类

@Test

public void testIncludeNestedWithDefault() throws Exception

Node root = new Node(1L, null, null);

Locale engLocale = new Locale(1L, java.util.Locale.ENGLISH);

Node child1 = new Node(2L, root, Collections.emptySet());

NodeComment child1Comment = new NodeComment(1L, “Child 1”, child1, engLocale);

child1.setNodeComments(new LinkedHashSet<>(Collections.singleton(child1Comment)));

Node child2 = new Node(3L, root, Collections.emptySet(), Collections.emptySet());

root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));

nodeRepository.save(root);

nodeRepository.save(child1);

nodeRepository.save(child2);

MockHttpServletRequest request = new MockHttpServletRequest(servletContext);

request.setMethod(“GET”);

request.setServletPath(“/api”);

request.setPathInfo(“/nodes/1”);

request.setRequestURI(“/api/nodes/1”);

request.setQueryString(“include[nodes]=children.nodeComments”);

request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);

Map params = new HashMap<>();

params.put(“include[nodes]”, “children.nodeComments.langLocale”);

request.setParameters(params);

request.addHeader(“Accept”, “/”);

MockHttpServletResponse response = new MockHttpServletResponse();

servlet.service(request, response);

String responseContent = response.getContentAsString();

assertTopLevelNodesCorrectWithChildren(responseContent);

CSDN 社区图书馆,开张营业! 深读计划,写书评领图书福利~

以上是关于java mock http_Java MockHttpServletRequest.setParameters方法代码示例的主要内容,如果未能解决你的问题,请参考以下文章

Java Mock 哪家强?Mocktio VS JMockit

Java Mock 哪家强?Mocktio VS JMockit

Java Mock 哪家强?Mocktio VS JMockit

快速入门JAVA单元测试——mock

快速入门JAVA单元测试——mock

快速入门JAVA单元测试——mock