Loading...
Spring Framework Reference Documentation 7.0.2의 MockMvc and HtmlUnit의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
This section describes how to integrate MockMvc and HtmlUnit. Use this option if you want to use the raw HtmlUnit 라이브러리.
First, make sure that you have included a test 의존성 on org.htmlunit:htmlunit.
We can easily create an HtmlUnit WebClient that integrates with MockMvc by using the
MockMvcWebClientBuilder, as follows:
1WebClient webClient; 2 3@BeforeEach 4void setup(WebApplicationContext context) { 5 webClient = MockMvcWebClientBuilder 6 .webAppContextSetup(context) 7 .build(); 8}
1lateinit var webClient: WebClient 2 3@BeforeEach 4fun setup(context: WebApplicationContext) { 5 webClient = MockMvcWebClientBuilder 6 .webAppContextSetup(context) 7 .build() 8}
MockMvcWebClientBuilder를 사용하는 간단한 예입니다. 고급 사용법은<br>AdvancedMockMvcWebClientBuilder를 참조하십시오.
This ensures that any URL that references localhost as the 서버 is directed to our
MockMvc 인스턴스 without the need for a real HTTP 연결. Any other URL is
requested by using a 네트워크 연결, as normal. This lets us easily test the use of
CDNs.
Now we can use HtmlUnit as we normally would but without the need to deploy our 애플리케이션 to a Servlet 컨테이너. For example, we can request the 뷰 to create a 메시지 with the following:
1HtmlPage createMsgFormPage = webClient.getPage("http://localhost/messages/form");
1val createMsgFormPage = webClient.getPage("http://localhost/messages/form")
기본 컨텍스트 경로는
""입니다. 또는, AdvancedMockMvcWebClientBuilder에 설명된 대로 컨텍스트 경로를 지정할 수 있습니다.
Once we have a reference to the HtmlPage, we can then fill out the 폼 and submit it
to create a 메시지, as the following example shows:
1HtmlForm form = createMsgFormPage.getHtmlElementById("messageForm"); 2HtmlTextInput summaryInput = createMsgFormPage.getHtmlElementById("summary"); 3summaryInput.setValueAttribute("Spring Rocks"); 4HtmlTextArea textInput = createMsgFormPage.getHtmlElementById("text"); 5textInput.setText("In case you didn't know, Spring Rocks!"); 6HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit"); 7HtmlPage newMessagePage = submit.click();
1val form = createMsgFormPage.getHtmlElementById("messageForm") 2val summaryInput = createMsgFormPage.getHtmlElementById("summary") 3summaryInput.setValueAttribute("Spring Rocks") 4val textInput = createMsgFormPage.getHtmlElementById("text") 5textInput.setText("In case you didn't know, Spring Rocks!") 6val submit = form.getOneHtmlElementByAttribute("input", "type", "submit") 7val newMessagePage = submit.click()
Finally, we can verify that a new 메시지 was created successfully. The following assertions use the AssertJ 라이브러리:
1assertThat(newMessagePage.getUrl().toString()).endsWith("/messages/123"); 2String id = newMessagePage.getHtmlElementById("id").getTextContent(); 3assertThat(id).isEqualTo("123"); 4String summary = newMessagePage.getHtmlElementById("summary").getTextContent(); 5assertThat(summary).isEqualTo("Spring Rocks"); 6String text = newMessagePage.getHtmlElementById("text").getTextContent(); 7assertThat(text).isEqualTo("In case you didn't know, Spring Rocks!");
1assertThat(newMessagePage.getUrl().toString()).endsWith("/messages/123") 2val id = newMessagePage.getHtmlElementById("id").getTextContent() 3assertThat(id).isEqualTo("123") 4val summary = newMessagePage.getHtmlElementById("summary").getTextContent() 5assertThat(summary).isEqualTo("Spring Rocks") 6val text = newMessagePage.getHtmlElementById("text").getTextContent() 7assertThat(text).isEqualTo("In case you didn't know, Spring Rocks!")
The preceding 코드 improves on our MockMvc 테스트 in a number of ways. First, we no longer have to explicitly verify our 폼 and then create a 요청 that looks like the 폼.
Instead, we request the 폼, fill it out, and submit it, thereby significantly reducing the 오버헤드.
Another important factor is that HtmlUnit uses the Mozilla Rhino 엔진 to evaluate JavaScript. This means that we can also test the 동작 of JavaScript within our 페이지.
See the HtmlUnit 문서 for additional information about using HtmlUnit.
MockMvcWebClientBuilderIn the examples so far, we have used MockMvcWebClientBuilder in the simplest way
possible, by building a WebClient based on the WebApplicationContext loaded for us by
the Spring TestContext 프레임워크. This 접근 방식 is repeated in the following example:
1WebClient webClient; 2 3@BeforeEach 4void setup(WebApplicationContext context) { 5 webClient = MockMvcWebClientBuilder 6 .webAppContextSetup(context) 7 .build(); 8}
1lateinit var webClient: WebClient 2 3@BeforeEach 4fun setup(context: WebApplicationContext) { 5 webClient = MockMvcWebClientBuilder 6 .webAppContextSetup(context) 7 .build() 8}
We can also specify additional 설정 options, as the following example shows:
1WebClient webClient; 2 3@BeforeEach 4void setup() { 5 webClient = MockMvcWebClientBuilder 6 // demonstrates applying a MockMvcConfigurer (Spring Security) 7 .webAppContextSetup(context, springSecurity()) 8 // for illustration only - defaults to "" 9 .contextPath("") 10 // By default MockMvc is used for localhost only; 11 // the following will use MockMvc for example.com and example.org as well 12 .useMockMvcForHosts("example.com","example.org") 13 .build(); 14}
1lateinit var webClient: WebClient 2 3@BeforeEach 4fun setup() { 5 webClient = MockMvcWebClientBuilder 6 // demonstrates applying a MockMvcConfigurer (Spring Security) 7 .webAppContextSetup(context, springSecurity()) 8 // for illustration only - defaults to "" 9 .contextPath("") 10 // By default MockMvc is used for localhost only; 11 // the following will use MockMvc for example.com and example.org as well 12 .useMockMvcForHosts("example.com","example.org") 13 .build() 14}
As an alternative, we can perform the exact same 설정 by configuring the MockMvc
인스턴스 separately and supplying it to the MockMvcWebClientBuilder, as follows:
1MockMvc mockMvc = MockMvcBuilders 2 .webAppContextSetup(context) 3 .apply(springSecurity()) 4 .build(); 5 6webClient = MockMvcWebClientBuilder 7 .mockMvcSetup(mockMvc) 8 // for illustration only - defaults to "" 9 .contextPath("") 10 // By default MockMvc is used for localhost only; 11 // the following will use MockMvc for example.com and example.org as well 12 .useMockMvcForHosts("example.com","example.org") 13 .build();
1val mockMvc = MockMvcBuilders 2 .webAppContextSetup(context) 3 .apply<DefaultMockMvcBuilder>(springSecurity()) 4 .build() 5 6webClient = MockMvcWebClientBuilder 7 .mockMvcSetup(mockMvc) 8 // for illustration only - defaults to "" 9 .contextPath("") 10 // By default MockMvc is used for localhost only; 11 // the following will use MockMvc for example.com and example.org as well 12 .useMockMvcForHosts("example.com", "example.org") 13 .build()
This is more verbose, but, by building the WebClient with a MockMvc 인스턴스, we have
the full power of MockMvc at our fingertips.
MockMvc인스턴스 생성에 대한 추가 정보는<br>Configuring MockMvc를 참조하십시오.
Why HtmlUnit Integration?
MockMvc and WebDriver