Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
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
idatt2106-v23-03
backend
Commits
8d950faf
Commit
8d950faf
authored
1 year ago
by
Anders Austlid
Browse files
Options
Downloads
Patches
Plain Diff
Added ShoppingListController unit tests
parent
f4432e21
No related branches found
Branches containing commit
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/ntnu/idatt2016/v233/SmartMat/controller/ShoppingListControllerTest.java
+91
-0
91 additions, 0 deletions
.../v233/SmartMat/controller/ShoppingListControllerTest.java
with
91 additions
and
0 deletions
src/test/java/ntnu/idatt2016/v233/SmartMat/controller/ShoppingListControllerTest.java
0 → 100644
+
91
−
0
View file @
8d950faf
package
ntnu.idatt2016.v233.SmartMat.controller
;
import
ntnu.idatt2016.v233.SmartMat.dto.request.ShoppingListRequest
;
import
ntnu.idatt2016.v233.SmartMat.entity.ShoppingList
;
import
ntnu.idatt2016.v233.SmartMat.service.ShoppingListService
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.junit.jupiter.MockitoExtension
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
java.util.Optional
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
mockito
.
Mockito
.
when
;
@ExtendWith
(
MockitoExtension
.
class
)
public
class
ShoppingListControllerTest
{
@InjectMocks
private
ShoppingListController
shoppingListController
;
@Mock
private
ShoppingListService
shoppingListService
;
private
ShoppingList
shoppingList
;
@BeforeEach
public
void
setUp
()
{
shoppingList
=
new
ShoppingList
();
// Set properties for the shoppingList object
}
@Test
public
void
createShoppingList
()
{
ShoppingListRequest
request
=
new
ShoppingListRequest
(
1L
);
// Set properties for the request object
when
(
shoppingListService
.
createShoppingList
(
request
)).
thenReturn
(
shoppingList
);
ResponseEntity
<
ShoppingList
>
response
=
shoppingListController
.
createShoppingList
(
request
);
assertEquals
(
HttpStatus
.
CREATED
,
response
.
getStatusCode
());
assertEquals
(
shoppingList
,
response
.
getBody
());
}
@Test
public
void
getShoppingListById_found
()
{
long
id
=
1
;
when
(
shoppingListService
.
getShoppingListById
(
id
)).
thenReturn
(
Optional
.
of
(
shoppingList
));
ResponseEntity
<
ShoppingList
>
response
=
shoppingListController
.
getShoppingListById
(
id
);
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
shoppingList
,
response
.
getBody
());
}
@Test
public
void
getShoppingListById_notFound
()
{
long
id
=
1
;
when
(
shoppingListService
.
getShoppingListById
(
id
)).
thenReturn
(
Optional
.
empty
());
ResponseEntity
<
ShoppingList
>
response
=
shoppingListController
.
getShoppingListById
(
id
);
assertEquals
(
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
}
@Test
public
void
getAllShoppingListsByGroupId_found
()
{
long
groupId
=
1
;
when
(
shoppingListService
.
getShoppingListByGroupId
(
groupId
)).
thenReturn
(
Optional
.
of
(
shoppingList
));
ResponseEntity
<
ShoppingList
>
response
=
shoppingListController
.
getAllShoppingListsByGroupId
(
groupId
);
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
assertEquals
(
shoppingList
,
response
.
getBody
());
}
@Test
public
void
getAllShoppingListsByGroupId_notFound
()
{
long
groupId
=
1
;
when
(
shoppingListService
.
getShoppingListByGroupId
(
groupId
)).
thenReturn
(
Optional
.
empty
());
ResponseEntity
<
ShoppingList
>
response
=
shoppingListController
.
getAllShoppingListsByGroupId
(
groupId
);
assertEquals
(
HttpStatus
.
NOT_FOUND
,
response
.
getStatusCode
());
}
}
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