Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
Freshpass Poc
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Giridhari Sahoo
Freshpass Poc
Commits
73470518
Commit
73470518
authored
Dec 03, 2024
by
Giridhari Sahoo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial test cases commit
parent
c0c95e37
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
141 additions
and
0 deletions
+141
-0
EmployeeDto.java
src/main/java/com/nisum/Employeeinfo/dto/EmployeeDto.java
+10
-0
EmployeeControllerTest.java
...nisum/Employeeinfo/controller/EmployeeControllerTest.java
+131
-0
No files found.
src/main/java/com/nisum/Employeeinfo/dto/EmployeeDto.java
View file @
73470518
...
@@ -9,6 +9,16 @@ public class EmployeeDto {
...
@@ -9,6 +9,16 @@ public class EmployeeDto {
private
String
dept
;
private
String
dept
;
private
Double
sal
;
private
Double
sal
;
public
EmployeeDto
()
{
}
public
EmployeeDto
(
String
name
,
String
email
,
String
dept
,
Double
sal
)
{
this
.
name
=
name
;
this
.
email
=
email
;
this
.
dept
=
dept
;
this
.
sal
=
sal
;
}
public
String
getName
()
{
public
String
getName
()
{
return
name
;
return
name
;
}
}
...
...
src/test/java/com/nisum/Employeeinfo/controller/EmployeeControllerTest.java
0 → 100644
View file @
73470518
package
com
.
nisum
.
Employeeinfo
.
controller
;
import
com.nisum.Employeeinfo.dto.EmployeeDto
;
import
com.nisum.Employeeinfo.errorcode.ApiErrorCode
;
import
com.nisum.Employeeinfo.exception.EmployeeNotFoundException
;
import
com.nisum.Employeeinfo.model.Employee
;
import
com.nisum.Employeeinfo.service.EmployeeService
;
import
com.nisum.Employeeinfo.validator.CommonServiceValidator
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.*
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.test.StepVerifier
;
import
static
org
.
mockito
.
ArgumentMatchers
.*;
@SpringBootTest
public
class
EmployeeControllerTest
{
@InjectMocks
private
EmployeeController
employeeController
;
@Mock
private
EmployeeService
employeeService
;
@Mock
private
CommonServiceValidator
validator
;
@BeforeEach
public
void
setup
()
{
MockitoAnnotations
.
openMocks
(
this
);
}
@Test
public
void
addEmployeeTest
()
{
EmployeeDto
employeeDto
=
new
EmployeeDto
(
"sam"
,
"sam@gmail.com"
,
"IT"
,
28000.00
);
Employee
employee
=
new
Employee
(
1
,
"sam"
,
"sam@gmail.com"
,
"IT"
,
28000.00
);
Mockito
.
when
(
validator
.
isEmployeeNameValid
(
any
())).
thenReturn
(
Mono
.
just
(
Boolean
.
TRUE
));
Mockito
.
when
(
validator
.
isEmployeeSalaryValid
(
any
())).
thenReturn
(
Mono
.
just
(
Boolean
.
TRUE
));
Mockito
.
when
(
employeeService
.
addEmployee
(
any
())).
thenReturn
(
Mono
.
just
(
employee
));
StepVerifier
.
create
(
employeeController
.
addEmployee
(
Mono
.
just
(
employeeDto
)))
.
expectNextMatches
(
employeeResponseEntity
->
{
return
employeeResponseEntity
.
getBody
()
!=
null
&&
employeeResponseEntity
.
getBody
().
getId
()
==
1
&&
employeeResponseEntity
.
getBody
().
getName
().
equals
(
"sam"
)
&&
employeeResponseEntity
.
getBody
().
getEmail
().
equals
(
"sam@gmail.com"
)
&&
employeeResponseEntity
.
getBody
().
getDept
().
equals
(
"IT"
)
&&
employeeResponseEntity
.
getBody
().
getSal
()
==
28000.00
;
}).
verifyComplete
();
}
// @Test
// public void addEmployeeTest_ValidationFailure(){
//
// EmployeeDto employeeDto = new EmployeeDto(" ", "sam@gmail.com", "IT", 28000.00);
//
// Mockito.when(validator.isEmployeeNameValid(employeeDto.getName())).thenReturn(Mono.error(new EmployeeNotFoundException(ApiErrorCode.EMPLOYEE_NOT_FOUND,"Employee_Not_Found")));
//
// StepVerifier.create(employeeController.addEmployee(Mono.just(employeeDto)))
// .expectErrorMatches(throwable -> throwable instanceof RuntimeException && throwable.getMessage().contains("Employee not found") )
// .verify();
//
// }
@Test
public
void
getEmployeeByIdTest
()
{
Employee
emp
=
new
Employee
(
2
,
"Rahul"
,
"rahul@gmail.com"
,
"HR"
,
18000.0
);
Mockito
.
when
(
validator
.
isEmployeeIdValid
(
emp
.
getId
())).
thenReturn
(
Mono
.
just
(
Boolean
.
TRUE
));
Mockito
.
when
(
employeeService
.
getEmployeeById
(
emp
.
getId
())).
thenReturn
(
Mono
.
just
(
emp
));
StepVerifier
.
create
(
employeeController
.
getEmployeeById
(
2
))
//.expectNextCount(1)
.
expectNextMatches
(
employeeResponseEntity
->
{
return
employeeResponseEntity
.
getStatusCode
().
is2xxSuccessful
()
&&
employeeResponseEntity
.
getBody
()
!=
null
&&
2
==
employeeResponseEntity
.
getBody
().
getId
()
&&
"Rahul"
.
equals
(
employeeResponseEntity
.
getBody
().
getName
())
&&
"rahul@gmail.com"
.
equals
(
employeeResponseEntity
.
getBody
().
getEmail
())
&&
"HR"
.
equals
(
employeeResponseEntity
.
getBody
().
getDept
())
&&
18000.00
==
employeeResponseEntity
.
getBody
().
getSal
();
}).
expectComplete
()
.
verify
();
}
@Test
public
void
getAllEmployeeTest
(){
Employee
emp1
=
new
Employee
(
1
,
"sam"
,
"sam@gmail.com"
,
"IT"
,
28000.00
);
Employee
emp2
=
new
Employee
(
2
,
"Rahul"
,
"rahul@gmail.com"
,
"HR"
,
18000.0
);
Employee
emp3
=
new
Employee
(
3
,
"Dibya"
,
"dibya@gmail.com"
,
"ADMIN"
,
22000.0
);
Mockito
.
when
(
employeeService
.
getAllEmployee
()).
thenReturn
(
Flux
.
just
(
emp1
,
emp2
,
emp3
));
StepVerifier
.
create
(
employeeController
.
getAllEmployee
())
.
expectNextCount
(
3
)
.
verifyComplete
();
}
@Test
public
void
updateEmployeeTest
(){
EmployeeDto
employeeDto
=
new
EmployeeDto
(
"sam"
,
"sam@gmail.com"
,
"IT"
,
28000.00
);
Employee
updateEmployee
=
new
Employee
(
1
,
"sam"
,
"sam@gmail.com"
,
"IT"
,
28000.00
);
Mockito
.
when
(
validator
.
isEmployeeIdValid
(
anyInt
())).
thenReturn
(
Mono
.
just
(
Boolean
.
TRUE
));
Mockito
.
when
(
validator
.
isEmployeeNameValid
(
any
())).
thenReturn
(
Mono
.
just
(
Boolean
.
TRUE
));
Mockito
.
when
(
validator
.
isEmployeeSalaryValid
(
anyDouble
())).
thenReturn
(
Mono
.
just
(
Boolean
.
TRUE
));
Mockito
.
when
(
employeeService
.
updateEmployee
(
ArgumentMatchers
.
eq
(
1
),
ArgumentMatchers
.
any
())).
thenReturn
(
Mono
.
just
(
updateEmployee
));
StepVerifier
.
create
(
employeeController
.
updateEmployee
(
ArgumentMatchers
.
eq
(
1
),
Mono
.
just
(
employeeDto
)))
.
expectNextMatches
(
response
->{
return
response
.
getStatusCode
().
is2xxSuccessful
()
&&
response
.
getBody
()
!=
null
&&
response
.
getBody
().
getId
().
equals
(
1
)
&&
"sam"
.
equals
(
response
.
getBody
().
getName
())
&&
"sam@gmail.com"
.
equals
(
response
.
getBody
().
getEmail
())
&&
"IT"
.
equals
(
response
.
getBody
().
getDept
())
&&
Double
.
compare
(
response
.
getBody
().
getSal
(),
28000.00
)
==
0
;
}).
verifyComplete
();
}
}
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