Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_45598856
white-jotter
提交
3f084528
W
white-jotter
项目概览
qq_45598856
/
white-jotter
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
white-jotter
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
3f084528
编写于
12月 28, 2020
作者:
E
Evan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
chore: update .gitignore
上级
d9af831a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
0 addition
and
171 deletion
+0
-171
wj/.gitignore
wj/.gitignore
+0
-0
wj/src/test/java/com/gm/wj/WjApplicationTests.java
wj/src/test/java/com/gm/wj/WjApplicationTests.java
+0
-16
wj/src/test/java/com/gm/wj/controller/LoginControllerTest.java
...c/test/java/com/gm/wj/controller/LoginControllerTest.java
+0
-68
wj/src/test/java/com/gm/wj/service/UserServiceTest.java
wj/src/test/java/com/gm/wj/service/UserServiceTest.java
+0
-87
未找到文件。
.gitignore
→
wj/
.gitignore
浏览文件 @
3f084528
文件已移动
wj/src/test/java/com/gm/wj/WjApplicationTests.java
已删除
100644 → 0
浏览文件 @
d9af831a
package
com.gm.wj
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
public
class
WjApplicationTests
{
@Test
public
void
contextLoads
()
{
}
}
wj/src/test/java/com/gm/wj/controller/LoginControllerTest.java
已删除
100644 → 0
浏览文件 @
d9af831a
package
com.gm.wj.controller
;
import
cn.hutool.json.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.gm.wj.entity.User
;
import
com.gm.wj.result.Result
;
import
com.gm.wj.service.UserService
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mockito
;
import
org.skyscreamer.jsonassert.JSONAssert
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.mock.web.MockHttpServletResponse
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MvcResult
;
import
org.springframework.test.web.servlet.RequestBuilder
;
import
org.springframework.test.web.servlet.request.MockMvcRequestBuilders
;
import
javax.servlet.http.Cookie
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
/**
* @author Evan
* @date 2020/11/9 21:42
*
* A failed attempt at unit testing of controllers.
*/
//@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
@WebMvcTest
(
value
=
LoginController
.
class
)
public
class
LoginControllerTest
{
@Autowired
private
MockMvc
mockMvc
;
@MockBean
private
UserService
userService
;
@Test
public
void
testLogin_Normal
()
throws
Exception
{
User
testUser
=
User
.
builder
()
.
username
(
"test"
).
password
(
"123"
).
build
();
Mockito
.
when
(
userService
.
findByUsername
(
"test"
)).
thenReturn
(
testUser
);
String
JSON
=
"{\"username\":\"test\",\"password\":\"123\"}"
;
RequestBuilder
requestBuilder
=
MockMvcRequestBuilders
.
post
(
"/api/login"
)
.
accept
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
).
content
(
JSON
)
.
contentType
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
);
MvcResult
result
=
mockMvc
.
perform
(
requestBuilder
).
andReturn
();
MockHttpServletResponse
response
=
result
.
getResponse
();
Assert
.
assertEquals
(
HttpStatus
.
OK
.
value
(),
response
.
getStatus
());
System
.
out
.
println
(
result
.
getResponse
().
getContentAsString
()
+
"11111111111"
);
String
expected
=
"{\"code\":200,\"message\":\"成功\",\"result\":\"test\"}"
;
JSONAssert
.
assertEquals
(
expected
,
result
.
getResponse
().
getContentAsString
(),
false
);
}
}
wj/src/test/java/com/gm/wj/service/UserServiceTest.java
已删除
100644 → 0
浏览文件 @
d9af831a
package
com.gm.wj.service
;
import
com.gm.wj.dao.UserDAO
;
import
com.gm.wj.dto.UserDTO
;
import
com.gm.wj.entity.AdminRole
;
import
com.gm.wj.entity.User
;
import
lombok.extern.log4j.Log4j2
;
import
org.apache.shiro.crypto.hash.SimpleHash
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
static
org
.
mockito
.
Mockito
.*;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
@Log4j2
@RunWith
(
MockitoJUnitRunner
.
class
)
//@RunWith(SpringRunner.class)
@SpringBootTest
public
class
UserServiceTest
{
@Mock
private
UserDAO
userDAO
;
@Mock
private
AdminRoleService
adminRoleService
;
@InjectMocks
private
UserService
userService
;
List
<
User
>
users
=
new
ArrayList
<>();
List
<
AdminRole
>
roles
=
new
ArrayList
<>();
@Before
public
void
before
()
{
}
@Test
public
void
testList
()
{
User
testUser
=
User
.
builder
().
username
(
"utest"
).
build
();
AdminRole
testRole
=
AdminRole
.
builder
().
name
(
"rtest"
).
build
();
users
.
add
(
testUser
);
roles
.
add
(
testRole
);
when
(
userDAO
.
findAll
()).
thenReturn
(
users
);
when
(
adminRoleService
.
listRolesByUser
(
testUser
.
getUsername
())).
thenReturn
(
roles
);
List
<
UserDTO
>
userDTOS
=
userService
.
list
();
Assert
.
assertThat
(
userDTOS
.
get
(
0
).
getUsername
(),
is
(
"utest"
));
Assert
.
assertThat
(
userDTOS
.
get
(
0
).
getRoles
().
get
(
0
).
getName
(),
is
(
"rtest"
));
}
@Test
public
void
testRegister_Normal
()
{
User
testUser
=
User
.
builder
()
.
username
(
"utest"
).
password
(
"123"
).
name
(
"测试用户"
).
email
(
"123@456.com"
).
phone
(
"12312312312"
).
build
();
when
(
userDAO
.
save
(
any
(
User
.
class
))).
thenAnswer
(
i
->
i
.
getArguments
()[
0
]);
Assert
.
assertThat
(
userService
.
register
(
testUser
),
is
(
1
));
}
@Test
public
void
testResetPassword_Normal
()
{
User
testUser
=
User
.
builder
()
.
username
(
"utest"
).
password
(
"123456"
).
name
(
"测试用户"
).
email
(
"123@456.com"
).
phone
(
"12312312312"
).
build
();
when
(
userDAO
.
findByUsername
(
"utest"
)).
thenReturn
(
testUser
);
when
(
userDAO
.
save
(
any
(
User
.
class
))).
thenAnswer
(
i
->
i
.
getArguments
()[
0
]);
User
resetUser
=
userService
.
resetPassword
(
testUser
);
Assert
.
assertThat
(
resetUser
.
getPassword
(),
is
(
new
SimpleHash
(
"md5"
,
"123"
,
resetUser
.
getSalt
(),
2
).
toString
()));
}
@After
public
void
after
()
{
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录