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
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
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
c16a683c
Commit
c16a683c
authored
3 months ago
by
Chen Liang
Browse files
Options
Downloads
Patches
Plain Diff
Delete LightWeightTest.java
parent
7fe70125
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/java/com/cardiff/lcTest/LightWeightTest.java
+0
-138
0 additions, 138 deletions
src/test/java/com/cardiff/lcTest/LightWeightTest.java
with
0 additions
and
138 deletions
src/test/java/com/cardiff/lcTest/LightWeightTest.java
deleted
100644 → 0
+
0
−
138
View file @
7fe70125
package
com.cardiff.lcTest
;
import
com.cardiff.client_project.controller.nursinghome.NursingHomeController
;
import
com.cardiff.client_project.pojo.dto.HospitalDTO
;
import
com.cardiff.client_project.service.NursingHomeService
;
import
com.cardiff.client_project.utils.Result
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.junit.jupiter.SpringExtension
;
import
org.springframework.test.web.servlet.MockMvc
;
import
java.util.Collections
;
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
.*;
@WebMvcTest
(
NursingHomeController
.
class
)
@ExtendWith
(
SpringExtension
.
class
)
public
class
LightWeightTest
{
//@Autowired:这两个注解使得Spring自动注入MockMvc和ObjectMapper对象。
// MockMvc用于模拟HTTP请求和响应,而ObjectMapper用于在JSON和Java对象之间进行转换。
//
//@MockBean:该注解是Spring Boot的特有注解,用于创建一个虚拟的(mock)NursingHomeService对象,并注入到Controller中。
// 通过这种方式,Service层的逻辑被“mock”掉了,测试可以专注于Controller的行为,而无需依赖真实的Service层逻辑。
@Autowired
private
MockMvc
mockMvc
;
@MockBean
private
NursingHomeService
nursingHomeService
;
@Autowired
private
ObjectMapper
objectMapper
;
//测试:获取可用床位
@Test
public
void
testGetAvailableBeds
()
throws
Exception
{
List
<
HospitalDTO
>
mockList
=
Collections
.
singletonList
(
new
HospitalDTO
());
when
(
nursingHomeService
.
getAvailableBeds
()).
thenReturn
(
Result
.
success
(
mockList
));
mockMvc
.
perform
(
get
(
"/api/hospitals/available"
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
));
}
//测试:添加医院信息
@Test
public
void
testAddHospital
()
throws
Exception
{
HospitalDTO
hospitalDTO
=
new
HospitalDTO
();
hospitalDTO
.
setName
(
"Test Hospital"
);
when
(
nursingHomeService
.
insertPatientInfo
(
any
(
HospitalDTO
.
class
)))
.
thenReturn
(
Result
.
success
(
hospitalDTO
));
mockMvc
.
perform
(
post
(
"/api/hospitals"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
content
(
objectMapper
.
writeValueAsString
(
hospitalDTO
)))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
));
}
//测试:更新床位数
@Test
public
void
testUpdateBedCount
()
throws
Exception
{
when
(
nursingHomeService
.
updateBedCount
(
1
,
20
)).
thenReturn
(
Result
.
success
(
"Updated"
));
mockMvc
.
perform
(
put
(
"/api/hospitals/1/beds"
)
.
param
(
"currentPatients"
,
"20"
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
));
}
//测试:删除医院信息
@Test
public
void
testDeleteHospital
()
throws
Exception
{
when
(
nursingHomeService
.
deletePatientById
(
List
.
of
(
1
)))
.
thenReturn
(
Result
.
success
(
"Deleted"
));
mockMvc
.
perform
(
delete
(
"/api/hospitals/1"
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
));
}
//测试:搜索医院(带参数)
@Test
public
void
testSearchHospitalsWithName
()
throws
Exception
{
when
(
nursingHomeService
.
searchHospitals
(
"Test"
))
.
thenReturn
(
Result
.
success
(
List
.
of
(
new
HospitalDTO
())));
mockMvc
.
perform
(
get
(
"/api/hospitals/search"
)
.
param
(
"name"
,
"Test"
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
));
}
//测试:搜索医院(无参数,返回全部)
@Test
public
void
testSearchHospitalsWithoutName
()
throws
Exception
{
when
(
nursingHomeService
.
getAvailableBeds
())
.
thenReturn
(
Result
.
success
(
List
.
of
(
new
HospitalDTO
())));
mockMvc
.
perform
(
get
(
"/api/hospitals/search"
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
));
}
//测试:更新审批状态
@Test
public
void
testUpdateApprovalStatus
()
throws
Exception
{
when
(
nursingHomeService
.
updateApprovalStatus
(
1
,
"approved"
))
.
thenReturn
(
Result
.
success
(
"Approved"
));
mockMvc
.
perform
(
put
(
"/api/hospitals/1/approval"
)
.
param
(
"status"
,
"approved"
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
));
}
//测试:获取待审批医院
@Test
public
void
testGetPendingApprovals
()
throws
Exception
{
when
(
nursingHomeService
.
getPendingApprovals
())
.
thenReturn
(
Result
.
success
(
List
.
of
(
new
HospitalDTO
())));
mockMvc
.
perform
(
get
(
"/api/hospitals/pending"
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
jsonPath
(
"$.code"
).
value
(
1
));
}
}
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