Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xyqb-user2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
head_group
xyqb-user2
Commits
dcbbdf37
Commit
dcbbdf37
authored
Jan 09, 2018
by
技术部-任文超
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完成所有Spring-test jUnit Demo
parent
48a4efd7
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
4 additions
and
353 deletions
+4
-353
pom.xml
pom.xml
+2
-2
LoginForH5Tests.java
src/test/java/login/LoginForH5Tests.java
+0
-146
TestUserLogin.java
src/test/java/login/TestUserLogin.java
+1
-1
TestUserService.java
src/test/java/service/TestUserService.java
+1
-1
UserApiServiceTests.java
src/test/java/service/UserApiServiceTests.java
+0
-70
OneTimeTokenTests.java
src/test/java/token/OneTimeTokenTests.java
+0
-133
No files found.
pom.xml
View file @
dcbbdf37
...
...
@@ -147,7 +147,7 @@
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpcore
</artifactId>
<version>
4.
3.3
</version>
<version>
4.
4
</version>
</dependency>
<dependency>
...
...
@@ -158,7 +158,7 @@
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.
3.6
</version>
<version>
4.
4.1
</version>
</dependency>
<!-- hibernate end -->
...
...
src/test/java/login/LoginForH5Tests.java
deleted
100644 → 0
View file @
48a4efd7
package
login
;
import
cn.quantgroup.xyqb.Bootstrap
;
import
cn.quantgroup.xyqb.Constants
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.octo.captcha.service.CaptchaServiceException
;
import
org.apache.commons.lang3.StringUtils
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MvcResult
;
import
org.springframework.test.web.servlet.request.MockMvcRequestBuilders
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.web.context.WebApplicationContext
;
import
java.nio.charset.Charset
;
import
java.util.Base64
;
import
java.util.Optional
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
=
Bootstrap
.
class
)
@WebAppConfiguration
public
class
LoginForH5Tests
{
final
String
phoneNo
=
"13461067662"
;
final
String
userName
=
"root"
;
final
String
password
=
"!QAZ2wsx"
;
private
MockMvc
mvc
;
@Autowired
WebApplicationContext
webApplicationConnect
;
@Before
public
void
setUp
()
throws
JsonProcessingException
{
mvc
=
MockMvcBuilders
.
webAppContextSetup
(
webApplicationConnect
).
build
();
}
/**
* 测试Server是否可达
* @throws Exception
*/
@Test
public
void
testServer
()
throws
Exception
{
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
());
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/user/loginForH5"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
());
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/user/login/fastForH5"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
());
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/api/captcha"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
());
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/api/sms/send_login_code"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
());
}
/**
* 测试图形验证码账密登录
* @throws Exception
*/
@Test
public
void
testLoginFoH5
()
throws
Exception
{
Assert
.
assertTrue
(
false
);
}
/**
* 测试TokenOnce切面
* @throws Exception
*/
@Test
public
void
testLoginFastFoH5
()
throws
Exception
{
// 获取TokenOnce
String
oneTimeTokenUri
=
"/token/oneTime"
;
MvcResult
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
oneTimeTokenUri
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
param
(
"phoneNo"
,
phoneNo
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
String
content
=
mvcResult
.
getResponse
().
getContentAsString
();
JSONObject
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
Object
code
=
jsonResult
.
get
(
"code"
);
Assert
.
assertEquals
(
"0000"
,
code
);
Object
data
=
jsonResult
.
get
(
"data"
);
Assert
.
assertNotNull
(
data
);
String
oneTimeToken
=
String
.
valueOf
(
data
);
Assert
.
assertNotEquals
(
""
,
oneTimeToken
);
String
smsUri
=
"/api/sms/send_login_code"
;
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
oneTimeTokenUri
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
param
(
"phoneNo"
,
phoneNo
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
content
=
mvcResult
.
getResponse
().
getContentAsString
();
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
code
=
jsonResult
.
get
(
"code"
);
Assert
.
assertEquals
(
"0000"
,
code
);
data
=
jsonResult
.
get
(
"data"
);
Assert
.
assertNotNull
(
data
);
String
verificationCode
=
"1234"
;
StringBuilder
verificationBuilder
=
new
StringBuilder
();
String
authorization
=
"Verification "
+
new
String
(
Base64
.
getEncoder
().
encodeToString
(
verificationBuilder
.
append
(
phoneNo
).
append
(
":"
).
append
(
verificationCode
).
toString
().
getBytes
(
Charset
.
forName
(
"UTF-8"
))));
// 第一次使用TokenOnce
String
aspectUri
=
"/user/login/fastForH5"
;
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
aspectUri
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
header
(
"authorization"
,
authorization
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
content
=
mvcResult
.
getResponse
().
getContentAsString
();
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
code
=
jsonResult
.
get
(
"code"
);
Object
businessCode
=
jsonResult
.
get
(
"businessCode"
);
Assert
.
assertEquals
(
"0000"
,
code
);
Assert
.
assertNotEquals
(
"0002"
,
businessCode
);
// 使用过期的TokenOnce与verificationCode
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
aspectUri
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
header
(
"authorization"
,
authorization
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
content
=
mvcResult
.
getResponse
().
getContentAsString
();
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
code
=
jsonResult
.
get
(
"code"
);
Assert
.
assertEquals
(
"0000"
,
code
);
businessCode
=
jsonResult
.
get
(
"businessCode"
);
Assert
.
assertEquals
(
"0002"
,
businessCode
);
// 不使用TokenOnce和verificationCode
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
aspectUri
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
content
=
mvcResult
.
getResponse
().
getContentAsString
();
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
code
=
jsonResult
.
get
(
"code"
);
Assert
.
assertEquals
(
"0000"
,
code
);
businessCode
=
jsonResult
.
get
(
"businessCode"
);
Assert
.
assertEquals
(
"0002"
,
businessCode
);
}
}
src/test/java/
user
/TestUserLogin.java
→
src/test/java/
login
/TestUserLogin.java
View file @
dcbbdf37
package
user
;
package
login
;
import
cn.quantgroup.xyqb.Bootstrap
;
import
cn.quantgroup.xyqb.Constants
;
...
...
src/test/java/
userS
ervice/TestUserService.java
→
src/test/java/
s
ervice/TestUserService.java
View file @
dcbbdf37
package
userS
ervice
;
package
s
ervice
;
import
cn.quantgroup.xyqb.Bootstrap
;
import
cn.quantgroup.xyqb.entity.Address
;
...
...
src/test/java/service/UserApiServiceTests.java
deleted
100644 → 0
View file @
48a4efd7
//package service;
//
//import cn.quantgroup.xyqb.Bootstrap;
//import cn.quantgroup.xyqb.entity.Address;
//import cn.quantgroup.xyqb.model.UserFullInfo;
//import cn.quantgroup.xyqb.service.api.IUserApiService;
//import cn.quantgroup.xyqb.service.user.IAddressService;
//import cn.quantgroup.xyqb.service.user.IContactService;
//import com.google.common.collect.Lists;
//import lombok.extern.slf4j.Slf4j;
//import org.junit.Before;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.junit.runners.Parameterized;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.annotation.Rollback;
//import org.springframework.test.context.TestContextManager;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.sql.Timestamp;
//import java.time.LocalDateTime;
//import java.util.Arrays;
//import java.util.Collection;
//import java.util.List;
//
///**
// * Created by xuran on 2017/12/26.
// */
//@SpringBootTest(classes = Bootstrap.class )
//@Slf4j
//@Rollback
//@Transactional
//@RunWith(Parameterized.class)
//public class UserApiServiceTests {
// private TestContextManager testContextManager;
// @Autowired
// private IUserApiService userInfoService;
//
// public UserApiServiceTests(UserFullInfo userInfo) {
// super();
// this.addressObj = addressObj;
// }
// @Before
// public void setUpContext() throws Exception {
// //this is where the magic happens, we actually do "by hand" what the spring runner would do for us,
// // read the JavaDoc for the class bellow to know exactly what it does, the method names are quite accurate though
// this.testContextManager = new TestContextManager(getClass());
// this.testContextManager.prepareTestInstance(this);
// }
// @Parameterized.Parameters
// public static Collection data() {
// UserFullInfo userInfo = new UserFullInfo();
// return Arrays.asList(new Object[][]{{userInfo}});
// }
//
//
// @Test
// public void testAddress() {
// userInfoService.save(addressObj);
// addressObj=addressService.findByUserId(addressObj.getUserId());
// System.out.println("测试地址"+addressObj);
// List<Long> userIds= Lists.newArrayList();
// userIds.add(123456L);
// List<Address> addresses= addressService.findByUserIds(userIds);
// System.out.println("测试地址集合"+addresses.get(0));
// }
//
//
//}
src/test/java/token/OneTimeTokenTests.java
deleted
100644 → 0
View file @
48a4efd7
package
token
;
import
cn.quantgroup.xyqb.Bootstrap
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MvcResult
;
import
org.springframework.test.web.servlet.request.MockMvcRequestBuilders
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.web.context.WebApplicationContext
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
Bootstrap
.
class
)
@WebAppConfiguration
public
class
OneTimeTokenTests
{
final
String
userName
=
"root"
;
final
String
password
=
"!QAZ2wsx"
;
final
String
phoneNo
=
"13461067662"
;
private
MockMvc
mvc
;
@Autowired
WebApplicationContext
webApplicationConnect
;
@Before
public
void
setUp
()
throws
JsonProcessingException
{
mvc
=
MockMvcBuilders
.
webAppContextSetup
(
webApplicationConnect
).
build
();
}
/**
* 测试Server是否可达
* @throws Exception
*/
@Test
public
void
testServer
()
throws
Exception
{
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
());
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
"/token/oneTime"
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
());
}
/**
* 测试OneTime-Token发放服务
* @throws Exception
*/
@Test
public
void
testOneTimeToken
()
throws
Exception
{
String
tokenOnceUri
=
"/token/oneTime"
;
MvcResult
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
tokenOnceUri
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
String
content
=
mvcResult
.
getResponse
().
getContentAsString
();
JSONObject
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
Object
code
=
jsonResult
.
get
(
"code"
);
Assert
.
assertEquals
(
"0000"
,
code
);
Object
data
=
jsonResult
.
getString
(
"data"
);
Assert
.
assertNotNull
(
data
);
String
oneTimeToken
=
String
.
valueOf
(
data
);
Assert
.
assertNotEquals
(
""
,
oneTimeToken
);
}
/**
* 测试OneTime-Token切面
* @throws Exception
*/
// TODO 用户注册先不加OneTime-Token校验,进一步确认后再添加或删除本用例
//@Test
public
void
testAspect
()
throws
Exception
{
// 获取OneTime-Token
String
oneTimeTokenUri
=
"/token/oneTime"
;
MvcResult
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
oneTimeTokenUri
).
accept
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
String
content
=
mvcResult
.
getResponse
().
getContentAsString
();
JSONObject
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
Object
code
=
jsonResult
.
get
(
"code"
);
Assert
.
assertEquals
(
"0000"
,
code
);
Object
data
=
jsonResult
.
getString
(
"data"
);
Assert
.
assertNotNull
(
data
);
String
oneTimeToken
=
String
.
valueOf
(
data
);
Assert
.
assertNotEquals
(
""
,
oneTimeToken
);
// 第一次使用OneTime-Token
String
aspectUri
=
"/user/loginForH5"
;
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
aspectUri
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
param
(
"phoneNo"
,
userName
)
.
param
(
"password"
,
password
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
content
=
mvcResult
.
getResponse
().
getContentAsString
();
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
code
=
jsonResult
.
get
(
"code"
);
Object
businessCode
=
jsonResult
.
get
(
"businessCode"
);
Assert
.
assertEquals
(
"0000"
,
code
);
Assert
.
assertNotEquals
(
"0002"
,
businessCode
);
// 使用过期的TokenOnce
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
aspectUri
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
param
(
"phoneNo"
,
userName
)
.
param
(
"password"
,
password
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
content
=
mvcResult
.
getResponse
().
getContentAsString
();
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
code
=
jsonResult
.
get
(
"code"
);
Assert
.
assertEquals
(
"0000"
,
code
);
businessCode
=
jsonResult
.
get
(
"businessCode"
);
Assert
.
assertEquals
(
"0002"
,
businessCode
);
// 不使用TokenOnce
mvcResult
=
mvc
.
perform
(
MockMvcRequestBuilders
.
get
(
aspectUri
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
param
(
"phoneNo"
,
userName
)
.
param
(
"password"
,
password
))
.
andExpect
(
status
().
isOk
())
.
andReturn
();
content
=
mvcResult
.
getResponse
().
getContentAsString
();
jsonResult
=
JSON
.
parseObject
(
new
String
(
content
));
code
=
jsonResult
.
get
(
"code"
);
Assert
.
assertEquals
(
"0000"
,
code
);
businessCode
=
jsonResult
.
get
(
"businessCode"
);
Assert
.
assertEquals
(
"0002"
,
businessCode
);
}
}
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