Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Team6-Digital Insight for Health
Manage
Activity
Members
Labels
Plan
Issues
12
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chen Liang
Team6-Digital Insight for Health
Commits
a682424c
Commit
a682424c
authored
5 months ago
by
wyl
Browse files
Options
Downloads
Patches
Plain Diff
test SuperAdminController API
parent
af4fb23c
No related branches found
No related tags found
1 merge request
!39
test SuperAdminController API
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/java/com/cardiff/client_project/SuperAdminControllerTest.java
+106
-0
106 additions, 0 deletions
.../com/cardiff/client_project/SuperAdminControllerTest.java
with
106 additions
and
0 deletions
src/test/java/com/cardiff/client_project/SuperAdminControllerTest.java
0 → 100644
+
106
−
0
View file @
a682424c
package
com.cardiff.client_project
;
import
com.cardiff.client_project.pojo.dto.SelectDTO
;
import
com.cardiff.client_project.pojo.dto.SignUserDTO
;
import
com.cardiff.client_project.service.SuperAdminService
;
import
com.cardiff.client_project.utils.Result
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.ArgumentMatchers
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.web.servlet.MockMvc
;
import
java.util.Arrays
;
import
java.util.List
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.*;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
jsonPath
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
@SpringBootTest
@AutoConfigureMockMvc
public
class
SuperAdminControllerTest
{
@Autowired
private
MockMvc
mockMvc
;
@MockBean
private
SuperAdminService
superAdminService
;
@Test
void
testSignIn
()
throws
Exception
{
// Construct test data
SignUserDTO
signDTO
=
new
SignUserDTO
();
signDTO
.
setName
(
"testUser"
);
signDTO
.
setPassword
(
"password123"
);
Result
expectedResult
=
Result
.
success
(
"User registered successfully"
);
when
(
superAdminService
.
insertUserInform
(
any
(
SignUserDTO
.
class
))).
thenReturn
(
expectedResult
);
// post
mockMvc
.
perform
(
post
(
"/superAdmin/sign"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
content
(
new
ObjectMapper
().
writeValueAsString
(
signDTO
)))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.msg"
).
value
(
"User registered successfully"
));
// Verify that the service layer is invoked
verify
(
superAdminService
,
times
(
1
)).
insertUserInform
(
any
(
SignUserDTO
.
class
));
}
@Test
void
testDeleteHospitalById
()
throws
Exception
{
List
<
Integer
>
ids
=
Arrays
.
asList
(
1
,
2
,
3
);
Result
mockResult
=
Result
.
success
(
"successfully"
);
when
(
superAdminService
.
deleteById
(
eq
(
ids
),
eq
(
"hospital"
))).
thenReturn
(
mockResult
);
mockMvc
.
perform
(
delete
(
"/superAdmin/deleteHospitalById"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
content
(
new
ObjectMapper
().
writeValueAsString
(
ids
)))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
))
.
andExpect
(
jsonPath
(
"$.msg"
).
value
(
"successfully"
));
verify
(
superAdminService
,
times
(
1
)).
deleteById
(
eq
(
ids
),
eq
(
"hospital"
));
}
@Test
void
testSelectByItem
()
throws
Exception
{
SelectDTO
selectDTO
=
new
SelectDTO
();
selectDTO
.
setName
(
"testQuery"
);
Result
mockResult
=
Result
.
success
(
"Query successful"
);
when
(
superAdminService
.
selectByItem
(
any
(
SelectDTO
.
class
))).
thenReturn
(
mockResult
);
mockMvc
.
perform
(
post
(
"/superAdmin/select"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
content
(
new
ObjectMapper
().
writeValueAsString
(
selectDTO
)))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
))
.
andExpect
(
jsonPath
(
"$.msg"
).
value
(
"Query successful"
));
verify
(
superAdminService
,
times
(
1
)).
selectByItem
(
any
(
SelectDTO
.
class
));
}
@Test
void
testUpdateById
()
throws
Exception
{
SelectDTO
selectDTO
=
new
SelectDTO
();
selectDTO
.
setId
(
1
);
selectDTO
.
setName
(
"newData"
);
Result
mockResult
=
Result
.
success
(
"Update successful"
);
when
(
superAdminService
.
update
(
any
(
SelectDTO
.
class
))).
thenReturn
(
mockResult
);
mockMvc
.
perform
(
put
(
"/superAdmin/update"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
content
(
new
ObjectMapper
().
writeValueAsString
(
selectDTO
)))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
))
.
andExpect
(
jsonPath
(
"$.msg"
).
value
(
"Update successful"
));
verify
(
superAdminService
,
times
(
1
)).
update
(
any
(
SelectDTO
.
class
));
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment