Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fast
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pengguohui
fast
Commits
6969dc13
Commit
6969dc13
authored
Apr 18, 2019
by
Pengguohui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add junit mock test
parent
bf6e9ee0
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
265 additions
and
0 deletions
+265
-0
src/test/java/com/maxbox/Bean/Person.java
+22
-0
src/test/java/com/maxbox/common/FastApplicationTests.java
+29
-0
src/test/java/com/maxbox/controller/HelloController.java
+41
-0
src/test/java/com/maxbox/controller/WorldController.java
+21
-0
src/test/java/com/maxbox/test/SimpleDateFormatTest.java
+78
-0
src/test/java/com/maxbox/test/TestHelloController.java
+33
-0
src/test/java/com/maxbox/test/TestSuits.java
+13
-0
src/test/java/com/maxbox/test/TestWorldController.java
+28
-0
No files found.
src/test/java/com/maxbox/Bean/Person.java
0 → 100644
View file @
6969dc13
package
com
.
maxbox
.
Bean
;
import
lombok.Data
;
import
java.io.Serializable
;
/**
* Created by guohui on 2019/4/12.
*/
@Data
public
class
Person
implements
Serializable
{
private
Integer
age
;
private
String
name
;
public
Person
()
{
}
public
Person
(
Integer
age
,
String
name
)
{
this
.
age
=
age
;
this
.
name
=
name
;
}
}
src/test/java/com/maxbox/common/FastApplicationTests.java
0 → 100644
View file @
6969dc13
package
com
.
maxbox
.
common
;
import
com.maxbox.controller.HelloController
;
import
com.maxbox.controller.WorldController
;
import
org.junit.Before
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
/**
* Created by guohui on 2019/4/17.
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
public
class
FastApplicationTests
{
public
MockMvc
mvc
;
@Autowired
HelloController
helloController
;
@Autowired
WorldController
worldController
;
@Before
public
void
setUp
()
{
mvc
=
MockMvcBuilders
.
standaloneSetup
(
helloController
,
worldController
).
build
();
}
}
src/test/java/com/maxbox/controller/HelloController.java
0 → 100644
View file @
6969dc13
package
com
.
maxbox
.
controller
;
import
com.alibaba.fastjson.JSONObject
;
import
com.maxbox.Bean.Person
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* Created by guohui on 2019/4/11.
*/
@RestController
public
class
HelloController
{
@GetMapping
(
value
=
"/hello"
)
public
String
getHello
(
@RequestParam
(
name
=
"name"
)
String
name
)
{
System
.
out
.
println
(
"hello!!!"
+
name
);
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"code"
,
"123"
);
return
"123"
;
}
@PostMapping
(
value
=
"/saveHello"
)
public
String
saveHello
(
@RequestBody
Person
person
)
{
System
.
out
.
println
(
"请求参数:"
+
JSONObject
.
toJSONString
(
person
));
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"code"
,
0
);
return
jsonObject
.
toJSONString
();
}
@GetMapping
(
value
=
"/hello/{id}"
)
public
String
getHelloById
(
@PathVariable
(
value
=
"id"
)
Integer
id
)
{
System
.
out
.
println
(
"请求参数:hello"
+
id
);
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"code"
,
0
);
return
jsonObject
.
toJSONString
();
}
}
src/test/java/com/maxbox/controller/WorldController.java
0 → 100644
View file @
6969dc13
package
com
.
maxbox
.
controller
;
import
com.alibaba.fastjson.JSONObject
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* Created by guohui on 2019/4/11.
*/
@RestController
public
class
WorldController
{
@GetMapping
(
value
=
"/world"
)
public
String
getHello
(
@RequestParam
(
name
=
"name"
)
String
name
)
{
System
.
out
.
println
(
"world!!!"
+
name
);
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"code"
,
"123"
);
return
"123"
;
}
}
src/test/java/com/maxbox/test/SimpleDateFormatTest.java
0 → 100644
View file @
6969dc13
package
com
.
maxbox
.
test
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.Date
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
/**
* SimpleDateFormat线程不安全
* DateTimeFormatter 线程安全的
* Created by guohui on 2019/4/10.
*/
public
class
SimpleDateFormatTest
{
// private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private
static
final
String
pattern
=
"yyyy-MM-dd HH:mm:ss"
;
private
static
final
SimpleDateFormat
formatter2
=
new
SimpleDateFormat
(
pattern
);
private
static
final
DateTimeFormatter
formatter1
=
DateTimeFormatter
.
ofPattern
(
pattern
);
public
static
String
formatDate
(
LocalDateTime
date
){
return
formatter1
.
format
(
date
);
}
public
static
LocalDateTime
parse
(
String
dateNow
){
return
LocalDateTime
.
parse
(
dateNow
,
formatter1
);
}
public
static
String
formatDate2
(
Date
date
)
throws
ParseException
{
return
formatter2
.
format
(
date
);
}
public
static
Date
parse2
(
String
dateNow
)
throws
ParseException
{
return
formatter2
.
parse
(
dateNow
);
}
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
ExecutorService
executorService
=
Executors
.
newFixedThreadPool
(
100
);
/*for (int i =0 ;i<1;i++){
executorService.execute(()->{
// for (int j = 0; j < 10; j++) {
try {
System.out.println(parse2(formatDate2(new Date())));
// System.out.println(parse(formatDate(LocalDateTime.now())));
} catch (Exception e) {
e.printStackTrace();
}
// }
});
}*/
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
executorService
.
execute
(
new
Runnable
()
{
@Override
public
void
run
()
{
try
{
Thread
.
sleep
(
4000
);
System
.
out
.
println
(
parse
(
formatDate
(
LocalDateTime
.
now
())));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
});
}
executorService
.
shutdown
();
// executorService.awaitTermination(1, TimeUnit.DAYS);
while
(!
executorService
.
awaitTermination
(
3
,
TimeUnit
.
SECONDS
)){
System
.
out
.
println
(
"service not stop"
);
}
System
.
out
.
println
(
"all thread complete"
);
}
}
src/test/java/com/maxbox/test/TestHelloController.java
0 → 100644
View file @
6969dc13
package
com
.
maxbox
.
test
;
import
com.alibaba.fastjson.JSONObject
;
import
com.maxbox.Bean.Person
;
import
com.maxbox.common.FastApplicationTests
;
import
org.junit.Test
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.web.servlet.request.MockMvcRequestBuilders
;
import
org.springframework.test.web.servlet.result.MockMvcResultHandlers
;
import
org.springframework.test.web.servlet.result.MockMvcResultMatchers
;
public
class
TestHelloController
extends
FastApplicationTests
{
@Test
public
void
getHello
()
throws
Exception
{
//--1、验证controller是否正常响应并打印返回结果
/*mvc.perform(MockMvcRequestBuilders.get("/hello").param("name","1234").accept(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print()).andReturn();*/
//--2、验证controller是否正常响应并判断返回结果是否正确
/* mvc.perform(MockMvcRequestBuilders.get("/hello").
param("name", "1234").accept(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string(("123")));*/
//--3、Spring Mock的方式发送post请求,参数正确设置到请求方法体中
Person
person
=
new
Person
(
1
,
"张三"
);
mvc
.
perform
(
MockMvcRequestBuilders
.
post
(
"/saveHello"
).
contentType
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
).
content
(
JSONObject
.
toJSONString
(
person
))).
andExpect
(
MockMvcResultMatchers
.
status
().
isOk
()).
andDo
(
MockMvcResultHandlers
.
print
()).
andReturn
();
//--4、restful请求方式
String
apiUrl
=
"/hello/123"
;
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
apiUrl
).
accept
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)).
andExpect
(
MockMvcResultMatchers
.
status
().
isOk
())
.
andDo
(
MockMvcResultHandlers
.
print
()).
andReturn
();
//Assert.assertSame("企业数量有误",500,new Object());
}
}
src/test/java/com/maxbox/test/TestSuits.java
0 → 100644
View file @
6969dc13
package
com
.
maxbox
.
test
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.Suite
;
/**
* 测试套件-打包测试
* Created by guohui on 2019/4/18.
*/
@RunWith
(
Suite
.
class
)
@Suite
.
SuiteClasses
({
TestHelloController
.
class
,
TestWorldController
.
class
})
public
class
TestSuits
{
}
src/test/java/com/maxbox/test/TestWorldController.java
0 → 100644
View file @
6969dc13
package
com
.
maxbox
.
test
;
import
com.maxbox.common.FastApplicationTests
;
import
org.junit.Test
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.web.servlet.request.MockMvcRequestBuilders
;
import
org.springframework.test.web.servlet.result.MockMvcResultMatchers
;
public
class
TestWorldController
extends
FastApplicationTests
{
@Test
public
void
getHello
()
throws
Exception
{
//验证controller是否正常响应并打印返回结果
/*mvc.perform(MockMvcRequestBuilders.get("/world").param("name","1234").accept(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print()).andReturn();*/
//验证controller是否正常响应并判断返回结果是否正确
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/world"
).
param
(
"name"
,
"1234"
).
accept
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)).
andExpect
(
MockMvcResultMatchers
.
status
().
isOk
()).
andExpect
(
MockMvcResultMatchers
.
content
().
string
((
"123"
)));
// Person person = new Person(1, "张三");
/* ObjectMapper mapper = new ObjectMapper();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String str = ow.writeValueAsString(person);
mvc.perform(MockMvcRequestBuilders.post("/saveHello").content(str).accept(MediaType.APPLICATION_JSON_VALUE)).andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();*/
//Assert.assertSame("企业数量有误",500,new Object());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment