Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
Junit5-mockito
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
vikram singh
Junit5-mockito
Commits
5a6c6cec
Commit
5a6c6cec
authored
May 19, 2020
by
vikram singh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added junit and component testcases for the item and orders
parent
8890135c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
193 additions
and
0 deletions
+193
-0
ItemControllerJunitTest.java
...est/java/com/ecom/controller/ItemControllerJunitTest.java
+130
-0
ItemServiceImplTest.java
src/test/java/com/ecom/service/ItemServiceImplTest.java
+63
-0
No files found.
src/test/java/com/ecom/controller/ItemControllerJunitTest.java
0 → 100644
View file @
5a6c6cec
package
com
.
ecom
.
controller
;
import
com.ecom.config.TestConfig
;
import
com.ecom.model.Item
;
import
com.ecom.repository.ItemRepository
;
import
com.ecom.service.impl.ItemServiceImpl
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.Arguments
;
import
org.junit.jupiter.params.provider.MethodSource
;
import
org.junit.jupiter.params.provider.ValueSource
;
import
org.mockito.ArgumentMatchers
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.context.ContextConfiguration
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.stream.Stream
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.
doNothing
;
import
static
org
.
mockito
.
Mockito
.
when
;
@SpringBootTest
(
classes
=
{
ItemController
.
class
})
public
class
ItemControllerJunitTest
{
@Autowired
private
ItemController
itemController
;
@MockBean
private
ItemServiceImpl
itemServiceImpl
;
@DisplayName
(
"This method will test the save method it will mock db "
)
@ParameterizedTest
@MethodSource
(
"getItemSource"
)
public
void
saveTest
(
Item
item
){
when
(
itemServiceImpl
.
save
(
ArgumentMatchers
.
any
(
Item
.
class
))).
thenReturn
(
item
);
ResponseEntity
<
Item
>
responseEntity
=
itemController
.
save
(
item
);
assertAll
(
"savetest"
,
()->
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
CREATED
,()->
"status should be 201"
)
,()->
assertEquals
(
responseEntity
.
getBody
().
getId
(),
1
,()->
"id should be mocked id only"
)
);
}
@DisplayName
(
"This method will test the save method it will mock db "
)
@ParameterizedTest
@MethodSource
(
"getItemSource"
)
public
void
saveNegativeTest
(
Item
item
){
when
(
itemServiceImpl
.
save
(
ArgumentMatchers
.
any
(
Item
.
class
))).
thenReturn
(
null
);
ResponseEntity
<
Item
>
responseEntity
=
itemController
.
save
(
item
);
assertAll
(
"savetestnegative"
,
()->
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
INTERNAL_SERVER_ERROR
,()->
"status should be 500"
)
,()->
assertNull
(
responseEntity
.
getBody
(),()->
"body is null"
)
);
}
@DisplayName
(
"This method will test the get method it will mock db "
)
@ParameterizedTest
@ValueSource
(
ints
={
1
})
public
void
getByIdTest
(
int
id
){
Item
response
=
mockItem
();
when
(
itemServiceImpl
.
findById
(
ArgumentMatchers
.
anyInt
())).
thenReturn
(
Optional
.
of
(
response
));
ResponseEntity
<
Optional
<
Item
>>
responseEntity
=
itemController
.
getById
(
id
);
assertAll
(
"gettest"
,
()->
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
,()->
"status should be 200"
)
,()->
assertNotNull
(
responseEntity
.
getBody
(),()->
"body is not null"
)
);
}
@DisplayName
(
"This method will test the get method for negative scenario it will mock db "
)
@ParameterizedTest
@ValueSource
(
ints
={
1
})
public
void
getByIdTestNegative
(
int
id
){
Item
response
=
mockItem
();
when
(
itemServiceImpl
.
findById
(
ArgumentMatchers
.
anyInt
())).
thenReturn
(
Optional
.
ofNullable
(
null
));
ResponseEntity
<
Optional
<
Item
>>
responseEntity
=
itemController
.
getById
(
id
);
assertAll
(
"gettestnegative"
,
()->
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
NOT_FOUND
,()->
"status should be NotFound"
)
,()->
assertEquals
(
responseEntity
.
getBody
(),
Optional
.
empty
(),()->
"body is null"
)
);
}
@DisplayName
(
"This method will test the getAll method it will mock db "
)
@Test
public
void
getAllTest
(){
List
<
Item
>
response
=
Arrays
.
asList
(
mockItem
());
when
(
itemServiceImpl
.
findAll
()).
thenReturn
(
response
);
ResponseEntity
<
List
<
Item
>>
responseEntity
=
itemController
.
getAll
();
assertAll
(
"getAllTest"
,
()->
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
OK
,()->
"status should be 200"
)
,()->
assertEquals
(
responseEntity
.
getBody
(),
response
,()->
"body is List"
)
);
}
@DisplayName
(
"This method will test the getAll method for Negeate test it will mock db "
)
@Test
public
void
getAllTestNegative
(){
List
<
Item
>
response
=
Arrays
.
asList
(
mockItem
());
when
(
itemServiceImpl
.
findAll
()).
thenReturn
(
null
);
ResponseEntity
<
List
<
Item
>>
responseEntity
=
itemController
.
getAll
();
assertAll
(
"getAllTest"
,
()->
assertEquals
(
responseEntity
.
getStatusCode
(),
HttpStatus
.
NOT_FOUND
,()->
"status should be Not found"
)
,()->
assertNull
(
responseEntity
.
getBody
(),()->
"body is null"
)
);
}
private
static
Stream
<
Arguments
>
getItemSource
(){
return
Stream
.
of
(
Arguments
.
of
(
mockItem
()));
}
public
static
Item
mockItem
()
{
Item
item
=
new
Item
();
item
.
setQty
(
1
);
item
.
setId
(
1
);
item
.
setName
(
"test"
);
item
.
setPrice
(
1
);
return
item
;
}
}
src/test/java/com/ecom/service/ItemServiceImplTest.java
0 → 100644
View file @
5a6c6cec
package
com
.
ecom
.
service
;
import
com.ecom.model.Item
;
import
com.ecom.repository.ItemRepository
;
import
com.ecom.service.impl.ItemServiceImpl
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.Arguments
;
import
org.junit.jupiter.params.provider.MethodSource
;
import
org.mockito.ArgumentMatchers
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.test.context.junit.jupiter.SpringExtension
;
import
java.util.stream.Stream
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertAll
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
mockito
.
Mockito
.
when
;
@SpringBootTest
(
classes
=
{
ItemServiceImpl
.
class
})
@ExtendWith
(
SpringExtension
.
class
)
//takes care of all the spring annotation test
@ExtendWith
(
MockitoExtension
.
class
)
// checks redundent mocking // also no need to add Mockito.initMock(this);
public
class
ItemServiceImplTest
{
@Autowired
private
ItemServiceImpl
itemServiceImpl
;
@MockBean
private
ItemRepository
itemRepository
;
@DisplayName
(
"This method will test the save method it will mock db "
)
@ParameterizedTest
@MethodSource
(
"getItemSource"
)
public
void
saveTest
(
Item
item
){
when
(
itemRepository
.
save
(
ArgumentMatchers
.
any
(
Item
.
class
))).
thenReturn
(
item
);
Item
itemResponse
=
itemServiceImpl
.
save
(
item
);
assertAll
(
"savetest"
,
()->
assertEquals
(
itemResponse
.
getId
(),
item
.
getId
(),()->
"item id should be matched"
),
()->
assertEquals
(
itemResponse
.
getName
(),
item
.
getName
(),()->
"item name should be matched"
)
);
}
private
static
Stream
<
Arguments
>
getItemSource
(){
return
Stream
.
of
(
Arguments
.
of
(
mockItem
()));
}
public
static
Item
mockItem
()
{
Item
item
=
new
Item
();
item
.
setQty
(
1
);
item
.
setId
(
1
);
item
.
setName
(
"test"
);
item
.
setPrice
(
1
);
return
item
;
}
}
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