Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
0b0a5a9e
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
0b0a5a9e
编写于
7月 27, 2015
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish tests for HtmlUnit support
上级
accb8519
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
261 addition
and
211 deletion
+261
-211
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/AbstractWebRequestMatcherTests.java
.../web/servlet/htmlunit/AbstractWebRequestMatcherTests.java
+42
-0
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java
...st/web/servlet/htmlunit/DelegatingWebConnectionTests.java
+9
-6
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java
...gframework/test/web/servlet/htmlunit/HelloController.java
+2
-4
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java
...rk/test/web/servlet/htmlunit/HostRequestMatcherTests.java
+25
-37
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java
...est/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java
+11
-11
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java
...ervlet/htmlunit/MockMvcConnectionBuilderSupportTests.java
+20
-16
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java
...st/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java
+19
-14
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java
.../test/web/servlet/htmlunit/MockMvcWebConnectionTests.java
+21
-21
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java
...est/web/servlet/htmlunit/MockWebResponseBuilderTests.java
+50
-46
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java
...est/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java
+7
-15
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java
...htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java
+32
-23
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java
.../htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java
+23
-18
未找到文件。
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/AbstractWebRequestMatcherTests.java
0 → 100644
浏览文件 @
0b0a5a9e
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package
org.springframework.test.web.servlet.htmlunit
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
com.gargoylesoftware.htmlunit.WebRequest
;
import
static
org
.
junit
.
Assert
.*;
/**
* Abstract base class for testing {@link WebRequestMatcher} implementations.
*
* @author Sam Brannen
* @since 4.2
*/
public
class
AbstractWebRequestMatcherTests
{
protected
void
assertMatches
(
WebRequestMatcher
matcher
,
String
url
)
throws
MalformedURLException
{
assertTrue
(
matcher
.
matches
(
new
WebRequest
(
new
URL
(
url
))));
}
protected
void
assertDoesNotMatch
(
WebRequestMatcher
matcher
,
String
url
)
throws
MalformedURLException
{
assertFalse
(
matcher
.
matches
(
new
WebRequest
(
new
URL
(
url
))));
}
}
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/DelegatingWebConnectionTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -69,12 +69,14 @@ public class DelegatingWebConnectionTests {
private
WebRequest
request
;
private
WebResponse
expectedResponse
;
@Before
public
void
setUp
()
throws
Exception
{
request
=
new
WebRequest
(
new
URL
(
"http://localhost/"
));
WebResponseData
data
=
new
WebResponseData
(
""
.
getBytes
(
"UTF-8"
),
200
,
""
,
Collections
.<
NameValuePair
>
emptyList
());
WebResponseData
data
=
new
WebResponseData
(
""
.
getBytes
(
"UTF-8"
),
200
,
""
,
Collections
.<
NameValuePair
>
emptyList
());
expectedResponse
=
new
WebResponse
(
data
,
request
,
100L
);
webConnection
=
new
DelegatingWebConnection
(
defaultConnection
,
new
DelegateWebConnection
(
matcher1
,
connection1
),
new
DelegateWebConnection
(
matcher2
,
connection2
));
webConnection
=
new
DelegatingWebConnection
(
defaultConnection
,
new
DelegateWebConnection
(
matcher1
,
connection1
),
new
DelegateWebConnection
(
matcher2
,
connection2
));
}
@Test
...
...
@@ -86,7 +88,7 @@ public class DelegatingWebConnectionTests {
assertThat
(
response
,
sameInstance
(
expectedResponse
));
verify
(
matcher1
).
matches
(
request
);
verify
(
matcher2
).
matches
(
request
);
verifyNoMoreInteractions
(
connection1
,
connection2
);
verifyNoMoreInteractions
(
connection1
,
connection2
);
verify
(
defaultConnection
).
getResponse
(
request
);
}
...
...
@@ -100,7 +102,7 @@ public class DelegatingWebConnectionTests {
assertThat
(
response
,
sameInstance
(
expectedResponse
));
verify
(
matcher1
).
matches
(
request
);
verifyNoMoreInteractions
(
matcher2
,
connection2
,
defaultConnection
);
verifyNoMoreInteractions
(
matcher2
,
connection2
,
defaultConnection
);
verify
(
connection1
).
getResponse
(
request
);
}
...
...
@@ -114,12 +116,12 @@ public class DelegatingWebConnectionTests {
assertThat
(
response
,
sameInstance
(
expectedResponse
));
verify
(
matcher1
).
matches
(
request
);
verify
(
matcher2
).
matches
(
request
);
verifyNoMoreInteractions
(
connection1
,
defaultConnection
);
verifyNoMoreInteractions
(
connection1
,
defaultConnection
);
verify
(
connection2
).
getResponse
(
request
);
}
@Test
public
void
classl
evelJavadoc
()
throws
Exception
{
public
void
verifyExampleInClassL
evelJavadoc
()
throws
Exception
{
WebClient
webClient
=
new
WebClient
();
MockMvc
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
TestController
.
class
).
build
();
...
...
@@ -137,6 +139,7 @@ public class DelegatingWebConnectionTests {
assertThat
(
page
.
getWebResponse
().
getContentAsString
(),
not
(
isEmptyString
()));
}
@Controller
static
class
TestController
{}
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HelloController.java
浏览文件 @
0b0a5a9e
...
...
@@ -18,19 +18,17 @@ package org.springframework.test.web.servlet.htmlunit;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.Res
ponseBody
;
import
org.springframework.web.bind.annotation.Res
tController
;
/**
* @author Rob Winch
* @since 4.2
*/
@Controller
@
Rest
Controller
public
class
HelloController
{
@RequestMapping
@ResponseBody
public
String
header
(
HttpServletRequest
request
)
{
return
"hello"
;
}
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -16,66 +16,54 @@
package
org.springframework.test.web.servlet.htmlunit
;
import
java.net.URL
;
import
org.junit.Test
;
import
com.gargoylesoftware.htmlunit.WebRequest
;
import
static
org
.
hamcrest
.
CoreMatchers
.
equalTo
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
/**
* Unit tests for {@link HostRequestMatcher}.
*
* @author Rob Winch
* @author Sam Brannen
* @since 4.2
*/
public
class
HostRequestMatcherTests
{
public
class
HostRequestMatcherTests
extends
AbstractWebRequestMatcherTests
{
@Test
public
void
localhost
Matches
()
throws
Exception
{
public
void
localhost
()
throws
Exception
{
WebRequestMatcher
matcher
=
new
HostRequestMatcher
(
"localhost"
);
boolean
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://localhost/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
true
));;
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://example.com/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
false
));;
assertMatches
(
matcher
,
"http://localhost/jquery-1.11.0.min.js"
);
assertDoesNotMatch
(
matcher
,
"http://example.com/jquery-1.11.0.min.js"
);
}
@Test
public
void
multipleHosts
()
throws
Exception
{
WebRequestMatcher
matcher
=
new
HostRequestMatcher
(
"localhost"
,
"example.com"
);
boolean
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://localhost/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
true
));;
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://example.com/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
true
));;
WebRequestMatcher
matcher
=
new
HostRequestMatcher
(
"localhost"
,
"example.com"
);
assertMatches
(
matcher
,
"http://localhost/jquery-1.11.0.min.js"
);
assertMatches
(
matcher
,
"http://example.com/jquery-1.11.0.min.js"
);
}
@Test
public
void
specificPort
()
throws
Exception
{
WebRequestMatcher
matcher
=
new
HostRequestMatcher
(
"localhost:8080"
);
boolean
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://localhost:8080/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
true
));;
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://localhost:9090/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
false
));;
assertMatches
(
matcher
,
"http://localhost:8080/jquery-1.11.0.min.js"
);
assertDoesNotMatch
(
matcher
,
"http://localhost:9090/jquery-1.11.0.min.js"
);
}
@Test
public
void
default
PortInMatcher
()
throws
Exception
{
public
void
default
HttpPort
()
throws
Exception
{
WebRequestMatcher
matcher
=
new
HostRequestMatcher
(
"localhost:80"
);
assertMatches
(
matcher
,
"http://localhost:80/jquery-1.11.0.min.js"
);
assertMatches
(
matcher
,
"http://localhost/jquery-1.11.0.min.js"
);
assertDoesNotMatch
(
matcher
,
"https://localhost/jquery-1.11.0.min.js"
);
assertDoesNotMatch
(
matcher
,
"http://localhost:9090/jquery-1.11.0.min.js"
);
}
boolean
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://localhost:80/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
true
));;
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://localhost/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
true
));;
matches
=
matcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://localhost:9090/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
false
));;
@Test
public
void
defaultHttpsPort
()
throws
Exception
{
WebRequestMatcher
matcher
=
new
HostRequestMatcher
(
"localhost:443"
);
assertMatches
(
matcher
,
"https://localhost:443/jquery-1.11.0.min.js"
);
assertMatches
(
matcher
,
"https://localhost/jquery-1.11.0.min.js"
);
assertDoesNotMatch
(
matcher
,
"http://localhost/jquery-1.11.0.min.js"
);
assertDoesNotMatch
(
matcher
,
"https://localhost:9090/jquery-1.11.0.min.js"
);
}
}
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -31,8 +31,10 @@ import javax.servlet.http.HttpSession;
import
org.apache.commons.io.IOUtils
;
import
org.apache.http.auth.UsernamePasswordCredentials
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.mock.web.MockHttpSession
;
import
org.springframework.mock.web.MockServletContext
;
...
...
@@ -50,31 +52,30 @@ import static org.junit.Assert.assertThat;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
/**
* Unit tests for {@link HtmlUnitRequestBuilder}.
*
* @author Rob Winch
* @since 4.2
*/
public
class
HtmlUnitRequestBuilderTests
{
private
WebRequest
webRequest
;
private
final
WebClient
webClient
=
new
WebClient
()
;
private
ServletContext
servletContext
;
private
final
ServletContext
servletContext
=
new
MockServletContext
()
;
private
Map
<
String
,
MockHttpSession
>
sessions
;
private
final
Map
<
String
,
MockHttpSession
>
sessions
=
new
HashMap
<>()
;
private
Web
Client
webClien
t
;
private
Web
Request
webReques
t
;
private
HtmlUnitRequestBuilder
requestBuilder
;
@Before
public
void
setUp
()
throws
Exception
{
sessions
=
new
HashMap
<>();
webRequest
=
new
WebRequest
(
new
URL
(
"http://example.com:80/test/this/here"
));
webRequest
.
setHttpMethod
(
HttpMethod
.
GET
);
webRequest
.
setRequestParameters
(
new
ArrayList
<>());
webClient
=
new
WebClient
();
requestBuilder
=
new
HtmlUnitRequestBuilder
(
sessions
,
webClient
,
webRequest
);
servletContext
=
new
MockServletContext
();
}
// --- constructor
...
...
@@ -740,7 +741,7 @@ public class HtmlUnitRequestBuilderTests {
String
cookieName
=
"PARENT"
;
String
cookieValue
=
"VALUE"
;
MockMvc
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
new
HelloController
())
.
defaultRequest
(
get
(
"/"
).
cookie
(
new
Cookie
(
cookieName
,
cookieValue
)))
.
defaultRequest
(
get
(
"/"
).
cookie
(
new
Cookie
(
cookieName
,
cookieValue
)))
.
build
();
Cookie
[]
cookies
=
mockMvc
.
perform
(
requestBuilder
).
andReturn
().
getRequest
().
getCookies
();
...
...
@@ -756,7 +757,7 @@ public class HtmlUnitRequestBuilderTests {
String
attrName
=
"PARENT"
;
String
attrValue
=
"VALUE"
;
MockMvc
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
new
HelloController
())
.
defaultRequest
(
get
(
"/"
).
requestAttr
(
attrName
,
attrValue
))
.
defaultRequest
(
get
(
"/"
).
requestAttr
(
attrName
,
attrValue
))
.
build
();
assertThat
(
mockMvc
.
perform
(
requestBuilder
).
andReturn
().
getRequest
().
getAttribute
(
attrName
),
equalTo
(
attrValue
));
...
...
@@ -770,8 +771,7 @@ public class HtmlUnitRequestBuilderTests {
return
;
}
String
actual
=
jsessionidCookie
.
getValue
();
assertThat
(
"JSESSIONID="
+
actual
+
"; Path=/test; Domain=example.com"
,
equalTo
(
expected
));
assertThat
(
"JSESSIONID="
+
actual
+
"; Path=/test; Domain=example.com"
,
equalTo
(
expected
));
}
private
void
setParameter
(
String
name
,
String
value
)
{
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -18,12 +18,12 @@ package org.springframework.test.web.servlet.htmlunit;
import
java.io.IOException
;
import
java.net.URL
;
import
javax.servlet.http.HttpServletRequest
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.test.context.ContextConfiguration
;
...
...
@@ -47,6 +47,8 @@ import static org.junit.Assert.assertThat;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Integration tests for {@link MockMvcWebConnectionBuilderSupport}.
*
* @author Rob Winch
* @since 4.2
*/
...
...
@@ -56,19 +58,18 @@ import static org.mockito.Mockito.mock;
@SuppressWarnings
(
"rawtypes"
)
public
class
MockMvcConnectionBuilderSupportTests
{
@Autowired
WebApplicationContext
context
;
private
final
WebConnection
delegateConnection
=
mock
(
WebConnection
.
class
);
MockMvc
mockMvc
;
@Autowired
private
WebApplicationContext
wac
;
WebConnection
delegateConnection
;
private
MockMvc
mockMvc
;
WebConnection
connection
;
private
WebConnection
connection
;
@Before
public
void
setup
()
{
delegateConnection
=
mock
(
WebConnection
.
class
);
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
context
).
build
();
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
wac
).
build
();
connection
=
new
MockMvcWebConnectionBuilderSupport
(
mockMvc
){}
.
createConnection
(
delegateConnection
);
...
...
@@ -86,7 +87,7 @@ public class MockMvcConnectionBuilderSupportTests {
@Test
public
void
context
()
throws
Exception
{
connection
=
new
MockMvcWebConnectionBuilderSupport
(
context
)
{}
connection
=
new
MockMvcWebConnectionBuilderSupport
(
wac
)
{}
.
createConnection
(
delegateConnection
);
assertMvcProcessed
(
"http://localhost/"
);
...
...
@@ -101,7 +102,7 @@ public class MockMvcConnectionBuilderSupportTests {
@Test
public
void
mockMvcExampleDotCom
()
throws
Exception
{
connection
=
new
MockMvcWebConnectionBuilderSupport
(
context
)
{}
connection
=
new
MockMvcWebConnectionBuilderSupport
(
wac
)
{}
.
useMockMvcForHosts
(
"example.com"
)
.
createConnection
(
delegateConnection
);
...
...
@@ -112,7 +113,7 @@ public class MockMvcConnectionBuilderSupportTests {
@Test
public
void
mockMvcAlwaysUseMockMvc
()
throws
Exception
{
connection
=
new
MockMvcWebConnectionBuilderSupport
(
context
)
{}
connection
=
new
MockMvcWebConnectionBuilderSupport
(
wac
)
{}
.
alwaysUseMockMvc
()
.
createConnection
(
delegateConnection
);
...
...
@@ -121,18 +122,18 @@ public class MockMvcConnectionBuilderSupportTests {
@Test
public
void
defaultContextPathEmpty
()
throws
Exception
{
connection
=
new
MockMvcWebConnectionBuilderSupport
(
context
)
{}
connection
=
new
MockMvcWebConnectionBuilderSupport
(
wac
)
{}
.
createConnection
(
delegateConnection
);
assertThat
(
getWebResponse
(
"http://localhost/abc"
).
getContentAsString
(),
equalTo
(
""
));
;
assertThat
(
getWebResponse
(
"http://localhost/abc"
).
getContentAsString
(),
equalTo
(
""
));
}
@Test
public
void
defaultContextPathCustom
()
throws
Exception
{
connection
=
new
MockMvcWebConnectionBuilderSupport
(
context
)
{
}
.
contextPath
(
"/abc"
).
createConnection
(
delegateConnection
);
connection
=
new
MockMvcWebConnectionBuilderSupport
(
wac
)
{}
.
contextPath
(
"/abc"
).
createConnection
(
delegateConnection
);
assertThat
(
getWebResponse
(
"http://localhost/abc/def"
).
getContentAsString
(),
equalTo
(
"/abc"
));
;
assertThat
(
getWebResponse
(
"http://localhost/abc/def"
).
getContentAsString
(),
equalTo
(
"/abc"
));
}
private
void
assertMvcProcessed
(
String
url
)
throws
Exception
{
...
...
@@ -147,11 +148,14 @@ public class MockMvcConnectionBuilderSupportTests {
return
connection
.
getResponse
(
new
WebRequest
(
new
URL
(
url
)));
}
@Configuration
@EnableWebMvc
static
class
Config
{
@RestController
static
class
ContextPathController
{
@RequestMapping
public
String
contextPath
(
HttpServletRequest
request
)
{
return
request
.
getContextPath
();
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebClientBuilderTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -45,6 +45,8 @@ import static org.hamcrest.CoreMatchers.not;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* Integration tests for {@link MockMvcWebClientBuilder}.
*
* @author Rob Winch
* @since 4.2
*/
...
...
@@ -53,17 +55,17 @@ import static org.junit.Assert.assertThat;
@WebAppConfiguration
public
class
MockMvcWebClientBuilderTests
{
private
WebClient
webClient
=
new
WebClient
();
@Autowired
WebApplicationContext
context
;
private
WebApplicationContext
wac
;
MockMvc
mockMvc
;
private
MockMvc
mockMvc
;
WebClient
webClient
;
@Before
public
void
setup
()
{
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
context
).
build
();
webClient
=
new
WebClient
();
this
.
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
this
.
wac
).
build
();
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
...
...
@@ -77,19 +79,19 @@ public class MockMvcWebClientBuilderTests {
}
@Test
public
void
mockMvcSetup
c
onfigureWebClient
()
throws
Exception
{
webClient
=
MockMvcWebClientBuilder
.
mockMvcSetup
(
mockMvc
)
.
configureWebClient
(
webClient
);
public
void
mockMvcSetup
AndC
onfigureWebClient
()
throws
Exception
{
this
.
webClient
=
MockMvcWebClientBuilder
.
mockMvcSetup
(
this
.
mockMvc
)
.
configureWebClient
(
this
.
webClient
);
assertMvcProcessed
(
"http://localhost/test"
);
assertDelegateProcessed
(
"http://example.com/"
);
}
@Test
public
void
mockMvcSetupCreateWebClient
()
throws
Exception
{
webClient
=
MockMvcWebClientBuilder
.
mockMvcSetup
(
mockMvc
)
public
void
mockMvcSetup
And
CreateWebClient
()
throws
Exception
{
this
.
webClient
=
MockMvcWebClientBuilder
.
mockMvcSetup
(
this
.
mockMvc
)
.
createWebClient
();
assertMvcProcessed
(
"http://localhost/test"
);
...
...
@@ -97,7 +99,7 @@ public class MockMvcWebClientBuilderTests {
}
private
void
assertMvcProcessed
(
String
url
)
throws
Exception
{
assertThat
(
getWebResponse
(
url
).
getContentAsString
(),
equalTo
(
"mvc"
));
;
assertThat
(
getWebResponse
(
url
).
getContentAsString
(),
equalTo
(
"mvc"
));
}
private
void
assertDelegateProcessed
(
String
url
)
throws
Exception
{
...
...
@@ -105,14 +107,17 @@ public class MockMvcWebClientBuilderTests {
}
private
WebResponse
getWebResponse
(
String
url
)
throws
IOException
{
return
webClient
.
getWebConnection
().
getResponse
(
new
WebRequest
(
new
URL
(
url
)));
return
this
.
webClient
.
getWebConnection
().
getResponse
(
new
WebRequest
(
new
URL
(
url
)));
}
@Configuration
@EnableWebMvc
static
class
Config
{
@RestController
static
class
ContextPathController
{
@RequestMapping
public
String
contextPath
(
HttpServletRequest
request
)
{
return
"mvc"
;
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcWebConnectionTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -20,6 +20,7 @@ import java.io.IOException;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
...
...
@@ -30,68 +31,67 @@ import static org.hamcrest.CoreMatchers.equalTo;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* Integration tests for {@link MockMvcWebConnection}.
*
* @author Rob Winch
* @since 4.2
*/
public
class
MockMvcWebConnectionTests
{
MockMvc
mockMvc
;
private
final
WebClient
webClient
=
new
WebClient
();
private
MockMvc
mockMvc
;
WebClient
webClient
;
@Before
public
void
setup
()
{
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
new
HelloController
(),
new
ForwardController
())
.
build
();
webClient
=
new
WebClient
();
this
.
mockMvc
=
MockMvcBuilders
.
standaloneSetup
(
new
HelloController
(),
new
ForwardController
()).
build
();
}
@Test
public
void
contextPathNull
()
throws
IOException
{
webClient
.
setWebConnection
(
new
MockMvcWebConnection
(
mockMvc
,
null
));
this
.
webClient
.
setWebConnection
(
new
MockMvcWebConnection
(
this
.
mockMvc
,
null
));
Page
page
=
webClient
.
getPage
(
"http://localhost/context/a"
);
Page
page
=
this
.
webClient
.
getPage
(
"http://localhost/context/a"
);
assertThat
(
page
.
getWebResponse
().
getStatusCode
(),
equalTo
(
200
));
;
assertThat
(
page
.
getWebResponse
().
getStatusCode
(),
equalTo
(
200
));
}
@Test
public
void
contextPathExplicit
()
throws
IOException
{
webClient
.
setWebConnection
(
new
MockMvcWebConnection
(
mockMvc
,
"/context"
));
this
.
webClient
.
setWebConnection
(
new
MockMvcWebConnection
(
this
.
mockMvc
,
"/context"
));
Page
page
=
webClient
.
getPage
(
"http://localhost/context/a"
);
Page
page
=
this
.
webClient
.
getPage
(
"http://localhost/context/a"
);
assertThat
(
page
.
getWebResponse
().
getStatusCode
(),
equalTo
(
200
));
;
assertThat
(
page
.
getWebResponse
().
getStatusCode
(),
equalTo
(
200
));
}
@Test
public
void
contextPathEmpty
()
throws
IOException
{
webClient
.
setWebConnection
(
new
MockMvcWebConnection
(
mockMvc
,
""
));
this
.
webClient
.
setWebConnection
(
new
MockMvcWebConnection
(
this
.
mockMvc
,
""
));
Page
page
=
webClient
.
getPage
(
"http://localhost/context/a"
);
Page
page
=
this
.
webClient
.
getPage
(
"http://localhost/context/a"
);
assertThat
(
page
.
getWebResponse
().
getStatusCode
(),
equalTo
(
200
));
;
assertThat
(
page
.
getWebResponse
().
getStatusCode
(),
equalTo
(
200
));
}
@Test
public
void
forward
()
throws
IOException
{
webClient
.
setWebConnection
(
new
MockMvcWebConnection
(
mockMvc
,
""
));
this
.
webClient
.
setWebConnection
(
new
MockMvcWebConnection
(
this
.
mockMvc
,
""
));
Page
page
=
webClient
.
getPage
(
"http://localhost/forward"
);
Page
page
=
this
.
webClient
.
getPage
(
"http://localhost/forward"
);
assertThat
(
page
.
getWebResponse
().
getContentAsString
(),
equalTo
(
"hello"
));
;
assertThat
(
page
.
getWebResponse
().
getContentAsString
(),
equalTo
(
"hello"
));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
contextPathDoesNotStartWithSlash
()
throws
IOException
{
new
MockMvcWebConnection
(
mockMvc
,
"context"
);
new
MockMvcWebConnection
(
this
.
mockMvc
,
"context"
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
contextPathEndsWithSlash
()
throws
IOException
{
new
MockMvcWebConnection
(
mockMvc
,
"/context/"
);
new
MockMvcWebConnection
(
this
.
mockMvc
,
"/context/"
);
}
}
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java
浏览文件 @
0b0a5a9e
/*
* Copyright 20
12
the original author or authors.
* Copyright 20
02-2015
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package
org.springframework.test.web.servlet.htmlunit
;
...
...
@@ -28,34 +31,36 @@ import static org.hamcrest.CoreMatchers.equalTo;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* Tests for {@link MockWebResponseBuilder}.
*
* @author Rob Winch
* @since 4.2
*/
public
class
MockWebResponseBuilderTests
{
private
WebRequest
webRequest
;
private
final
MockHttpServletResponse
response
=
new
MockHttpServletResponse
()
;
private
MockHttpServletResponse
httpServletResponse
;
private
WebRequest
webRequest
;
private
MockWebResponseBuilder
responseBuilder
;
@Before
public
void
setUp
()
throws
Exception
{
webRequest
=
new
WebRequest
(
new
URL
(
"http://example.com:80/test/this/here"
));
httpServletResponse
=
new
MockHttpServletResponse
();
this
.
webRequest
=
new
WebRequest
(
new
URL
(
"http://example.com:80/test/this/here"
));
responseBuilder
=
new
MockWebResponseBuilder
(
System
.
currentTimeMillis
(),
webRequest
,
httpServletR
esponse
);
this
.
responseBuilder
=
new
MockWebResponseBuilder
(
System
.
currentTimeMillis
(),
this
.
webRequest
,
this
.
r
esponse
);
}
// --- constructor
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
constructorNullWebRequest
()
{
new
MockWebResponseBuilder
(
0L
,
null
,
httpServletR
esponse
);
public
void
constructor
With
NullWebRequest
()
{
new
MockWebResponseBuilder
(
0L
,
null
,
this
.
r
esponse
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
constructorNullResponse
()
throws
Exception
{
public
void
constructor
With
NullResponse
()
throws
Exception
{
new
MockWebResponseBuilder
(
0L
,
new
WebRequest
(
new
URL
(
"http://example.com:80/test/this/here"
)),
null
);
}
...
...
@@ -63,76 +68,75 @@ public class MockWebResponseBuilderTests {
@Test
public
void
buildContent
()
throws
Exception
{
httpServletResponse
.
getWriter
().
write
(
"expected content"
);
WebResponse
webResponse
=
responseBuilder
.
build
();
this
.
response
.
getWriter
().
write
(
"expected content"
);
WebResponse
webResponse
=
this
.
responseBuilder
.
build
();
assertThat
(
webResponse
.
getContentAsString
(),
equalTo
(
"expected content"
));
;
assertThat
(
webResponse
.
getContentAsString
(),
equalTo
(
"expected content"
));
}
@Test
public
void
buildContentCharset
()
throws
Exception
{
httpServletR
esponse
.
addHeader
(
"Content-Type"
,
"text/html; charset=UTF-8"
);
WebResponse
webResponse
=
responseBuilder
.
build
();
this
.
r
esponse
.
addHeader
(
"Content-Type"
,
"text/html; charset=UTF-8"
);
WebResponse
webResponse
=
this
.
responseBuilder
.
build
();
assertThat
(
webResponse
.
getContentCharset
(),
equalTo
(
"UTF-8"
));
;
assertThat
(
webResponse
.
getContentCharset
(),
equalTo
(
"UTF-8"
));
}
@Test
public
void
buildContentType
()
throws
Exception
{
httpServletR
esponse
.
addHeader
(
"Content-Type"
,
"text/html; charset-UTF-8"
);
WebResponse
webResponse
=
responseBuilder
.
build
();
this
.
r
esponse
.
addHeader
(
"Content-Type"
,
"text/html; charset-UTF-8"
);
WebResponse
webResponse
=
this
.
responseBuilder
.
build
();
assertThat
(
webResponse
.
getContentType
(),
equalTo
(
"text/html"
));
;
assertThat
(
webResponse
.
getContentType
(),
equalTo
(
"text/html"
));
}
@Test
public
void
buildResponseHeaders
()
throws
Exception
{
httpServletR
esponse
.
addHeader
(
"Content-Type"
,
"text/html"
);
httpServletR
esponse
.
addHeader
(
"X-Test"
,
"value"
);
WebResponse
webResponse
=
responseBuilder
.
build
();
this
.
r
esponse
.
addHeader
(
"Content-Type"
,
"text/html"
);
this
.
r
esponse
.
addHeader
(
"X-Test"
,
"value"
);
WebResponse
webResponse
=
this
.
responseBuilder
.
build
();
List
<
NameValuePair
>
responseHeaders
=
webResponse
.
getResponseHeaders
();
assertThat
(
responseHeaders
.
size
(),
equalTo
(
2
));
;
assertThat
(
responseHeaders
.
size
(),
equalTo
(
2
));
NameValuePair
header
=
responseHeaders
.
get
(
0
);
assertThat
(
header
.
getName
(),
equalTo
(
"Content-Type"
));
;
assertThat
(
header
.
getValue
(),
equalTo
(
"text/html"
));
;
assertThat
(
header
.
getName
(),
equalTo
(
"Content-Type"
));
assertThat
(
header
.
getValue
(),
equalTo
(
"text/html"
));
header
=
responseHeaders
.
get
(
1
);
assertThat
(
header
.
getName
(),
equalTo
(
"X-Test"
));
;
assertThat
(
header
.
getValue
(),
equalTo
(
"value"
));
;
assertThat
(
header
.
getName
(),
equalTo
(
"X-Test"
));
assertThat
(
header
.
getValue
(),
equalTo
(
"value"
));
}
@Test
public
void
buildStatus
()
throws
Exception
{
WebResponse
webResponse
=
responseBuilder
.
build
();
WebResponse
webResponse
=
this
.
responseBuilder
.
build
();
assertThat
(
webResponse
.
getStatusCode
(),
equalTo
(
200
));
;
assertThat
(
webResponse
.
getStatusMessage
(),
equalTo
(
"OK"
));
;
assertThat
(
webResponse
.
getStatusCode
(),
equalTo
(
200
));
assertThat
(
webResponse
.
getStatusMessage
(),
equalTo
(
"OK"
));
}
@Test
public
void
buildStatusNotOk
()
throws
Exception
{
httpServletR
esponse
.
setStatus
(
401
);
WebResponse
webResponse
=
responseBuilder
.
build
();
this
.
r
esponse
.
setStatus
(
401
);
WebResponse
webResponse
=
this
.
responseBuilder
.
build
();
assertThat
(
webResponse
.
getStatusCode
(),
equalTo
(
401
));
;
assertThat
(
webResponse
.
getStatusMessage
(),
equalTo
(
"Unauthorized"
));
;
assertThat
(
webResponse
.
getStatusCode
(),
equalTo
(
401
));
assertThat
(
webResponse
.
getStatusMessage
(),
equalTo
(
"Unauthorized"
));
}
@Test
public
void
buildStatusCustomMessage
()
throws
Exception
{
httpServletR
esponse
.
sendError
(
401
,
"Custom"
);
WebResponse
webResponse
=
responseBuilder
.
build
();
public
void
buildStatus
With
CustomMessage
()
throws
Exception
{
this
.
r
esponse
.
sendError
(
401
,
"Custom"
);
WebResponse
webResponse
=
this
.
responseBuilder
.
build
();
assertThat
(
webResponse
.
getStatusCode
(),
equalTo
(
401
));
;
assertThat
(
webResponse
.
getStatusMessage
(),
equalTo
(
"Custom"
));
;
assertThat
(
webResponse
.
getStatusCode
(),
equalTo
(
401
));
assertThat
(
webResponse
.
getStatusMessage
(),
equalTo
(
"Custom"
));
}
@Test
public
void
buildWebRequest
()
throws
Exception
{
WebResponse
webResponse
=
responseBuilder
.
build
();
WebResponse
webResponse
=
this
.
responseBuilder
.
build
();
assertThat
(
webResponse
.
getWebRequest
(),
equalTo
(
webRequest
));
;
assertThat
(
webResponse
.
getWebRequest
(),
equalTo
(
this
.
webRequest
))
;
}
}
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/UrlRegexRequestMatcherTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -16,30 +16,22 @@
package
org.springframework.test.web.servlet.htmlunit
;
import
java.net.URL
;
import
org.junit.Test
;
import
com.gargoylesoftware.htmlunit.WebRequest
;
import
static
org
.
hamcrest
.
MatcherAssert
.*;
import
static
org
.
hamcrest
.
Matchers
.*;
/**
* Unit tests for {@link UrlRegexRequestMatcher}.
*
* @author Rob Winch
* @author Sam Brannen
* @since 4.2
*/
public
class
UrlRegexRequestMatcherTests
{
public
class
UrlRegexRequestMatcherTests
extends
AbstractWebRequestMatcherTests
{
@Test
public
void
classl
evelJavadoc
()
throws
Exception
{
public
void
verifyExampleInClassL
evelJavadoc
()
throws
Exception
{
WebRequestMatcher
cdnMatcher
=
new
UrlRegexRequestMatcher
(
".*?//code.jquery.com/.*"
);
boolean
matches
=
cdnMatcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://code.jquery.com/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
true
));
matches
=
cdnMatcher
.
matches
(
new
WebRequest
(
new
URL
(
"http://localhost/jquery-1.11.0.min.js"
)));
assertThat
(
matches
,
equalTo
(
false
));
assertMatches
(
cdnMatcher
,
"http://code.jquery.com/jquery-1.11.0.min.js"
);
assertDoesNotMatch
(
cdnMatcher
,
"http://localhost/jquery-1.11.0.min.js"
);
}
}
\ No newline at end of file
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/MockMvcHtmlUnitDriverBuilderTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -17,13 +17,13 @@
package
org.springframework.test.web.servlet.htmlunit.webdriver
;
import
java.io.IOException
;
import
javax.servlet.http.HttpServletRequest
;
import
static
org
.
hamcrest
.
CoreMatchers
.*;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.openqa.selenium.htmlunit.HtmlUnitDriver
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -38,8 +38,14 @@ import org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.context.WebApplicationContext
;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc
;
import
static
org
.
hamcrest
.
CoreMatchers
.*;
import
static
org
.
junit
.
Assert
.*;
/**
* Integration tests for {@link MockMvcHtmlUnitDriverBuilder}.
*
* @author Rob Winch
* @author Sam Brannen
* @since 4.2
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
...
...
@@ -47,18 +53,18 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@WebAppConfiguration
public
class
MockMvcHtmlUnitDriverBuilderTests
{
p
ublic
static
final
String
EXPECTED_BODY
=
"MockMvcHtmlUnitDriverBuilderTests mvc"
;
p
rivate
static
final
String
EXPECTED_BODY
=
"MockMvcHtmlUnitDriverBuilderTests mvc"
;
@Autowired
WebApplicationContext
context
;
private
WebApplicationContext
wac
;
MockMvc
mockMvc
;
private
MockMvc
mockMvc
;
HtmlUnitDriver
driver
;
private
HtmlUnitDriver
driver
;
@Before
public
void
setup
()
{
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
context
).
build
();
this
.
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
this
.
wac
).
build
();
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
...
...
@@ -72,9 +78,9 @@ public class MockMvcHtmlUnitDriverBuilderTests {
}
@Test
public
void
mockMvcSetupConfigureDriver
()
throws
Exception
{
driver
=
MockMvcHtmlUnitDriverBuilder
.
mockMvcSetup
(
mockMvc
)
public
void
mockMvcSetup
And
ConfigureDriver
()
throws
Exception
{
this
.
driver
=
MockMvcHtmlUnitDriverBuilder
.
mockMvcSetup
(
this
.
mockMvc
)
.
configureDriver
(
new
WebConnectionHtmlUnitDriver
());
assertMvcProcessed
(
"http://localhost/test"
);
...
...
@@ -82,9 +88,9 @@ public class MockMvcHtmlUnitDriverBuilderTests {
}
@Test
public
void
mockMvcSetupCreateDriver
()
throws
Exception
{
driver
=
MockMvcHtmlUnitDriverBuilder
.
mockMvcSetup
(
mockMvc
)
public
void
mockMvcSetup
And
CreateDriver
()
throws
Exception
{
this
.
driver
=
MockMvcHtmlUnitDriverBuilder
.
mockMvcSetup
(
this
.
mockMvc
)
.
createDriver
();
assertMvcProcessed
(
"http://localhost/test"
);
...
...
@@ -92,22 +98,22 @@ public class MockMvcHtmlUnitDriverBuilderTests {
}
@Test
public
void
java
scriptEnabledDefaultEnabled
()
{
driver
=
MockMvcHtmlUnitDriverBuilder
.
mockMvcSetup
(
mockMvc
)
public
void
java
ScriptEnabledByDefault
()
{
this
.
driver
=
MockMvcHtmlUnitDriverBuilder
.
mockMvcSetup
(
this
.
mockMvc
)
.
createDriver
();
assertT
hat
(
driver
.
isJavascriptEnabled
(),
equalTo
(
true
));
assertT
rue
(
this
.
driver
.
isJavascriptEnabled
(
));
}
@Test
public
void
java
scriptEnabled
Disabled
()
{
driver
=
MockMvcHtmlUnitDriverBuilder
.
mockMvcSetup
(
mockMvc
)
public
void
java
Script
Disabled
()
{
this
.
driver
=
MockMvcHtmlUnitDriverBuilder
.
mockMvcSetup
(
this
.
mockMvc
)
.
javascriptEnabled
(
false
)
.
createDriver
();
assert
That
(
driver
.
isJavascriptEnabled
(),
equalTo
(
false
));
assert
False
(
this
.
driver
.
isJavascriptEnabled
(
));
}
private
void
assertMvcProcessed
(
String
url
)
throws
Exception
{
...
...
@@ -119,15 +125,18 @@ public class MockMvcHtmlUnitDriverBuilderTests {
}
private
String
get
(
String
url
)
throws
IOException
{
driver
.
get
(
url
);
return
driver
.
getPageSource
();
this
.
driver
.
get
(
url
);
return
this
.
driver
.
getPageSource
();
}
@Configuration
@EnableWebMvc
static
class
Config
{
@RestController
static
class
ContextPathController
{
@RequestMapping
public
String
contextPath
(
HttpServletRequest
request
)
{
return
EXPECTED_BODY
;
...
...
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/webdriver/WebConnectionHtmlUnitDriverTests.java
浏览文件 @
0b0a5a9e
...
...
@@ -17,7 +17,9 @@
package
org.springframework.test.web.servlet.htmlunit.webdriver
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
...
...
@@ -29,48 +31,51 @@ import com.gargoylesoftware.htmlunit.WebRequest;
import
static
org
.
hamcrest
.
CoreMatchers
.
equalTo
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
hamcrest
.
Matchers
.
notNullValue
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* Unit tests for {@link WebConnectionHtmlUnitDriver}.
*
* @author Rob Winch
* @author Sam Brannen
* @since 4.2
*/
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
WebConnectionHtmlUnitDriverTests
{
private
final
WebConnectionHtmlUnitDriver
driver
=
new
WebConnectionHtmlUnitDriver
();
@Mock
WebConnection
connection
;
private
WebConnection
connection
;
WebConnectionHtmlUnitDriver
driver
;
@Rule
public
ExpectedException
exception
=
ExpectedException
.
none
();
@Before
public
void
setup
()
throws
Exception
{
driver
=
new
WebConnectionHtmlUnitDriver
();
when
(
connection
.
getResponse
(
any
(
WebRequest
.
class
))).
thenThrow
(
new
InternalError
(
""
));
when
(
this
.
connection
.
getResponse
(
any
(
WebRequest
.
class
))).
thenThrow
(
new
InternalError
(
""
));
}
@Test
public
void
getWebConnectionDefaultNotNull
()
{
assertThat
(
driver
.
getWebConnection
(),
notNullValue
());
assertThat
(
this
.
driver
.
getWebConnection
(),
notNullValue
());
}
@Test
public
void
setWebConnection
()
{
driver
.
setWebConnection
(
connection
);
assertThat
(
driver
.
getWebConnection
(),
equalTo
(
connection
));
try
{
driver
.
get
(
"https://example.com"
);
fail
(
"Expected Exception"
);
}
catch
(
InternalError
success
)
{}
public
void
setWebConnectionToNull
()
{
this
.
exception
.
expect
(
IllegalArgumentException
.
class
);
this
.
driver
.
setWebConnection
(
null
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
setWebConnectionNull
()
{
driver
.
setWebConnection
(
null
);
@Test
public
void
setWebConnection
()
{
this
.
driver
.
setWebConnection
(
this
.
connection
);
assertThat
(
this
.
driver
.
getWebConnection
(),
equalTo
(
this
.
connection
));
this
.
exception
.
expect
(
InternalError
.
class
);
this
.
driver
.
get
(
"https://example.com"
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录