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
2e86530a
Commit
2e86530a
authored
1 year ago
by
Anders Austlid
Browse files
Options
Downloads
Patches
Plain Diff
Added unit tests for GroupService
parent
6bfad71e
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/service/group/GroupServiceTest.java
+92
-0
92 additions, 0 deletions
...att2016/v233/SmartMat/service/group/GroupServiceTest.java
with
92 additions
and
0 deletions
src/test/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupServiceTest.java
0 → 100644
+
92
−
0
View file @
2e86530a
package
ntnu.idatt2016.v233.SmartMat.service.group
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
java.util.Collections
;
import
java.util.Optional
;
import
ntnu.idatt2016.v233.SmartMat.entity.group.Group
;
import
ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository
;
import
ntnu.idatt2016.v233.SmartMat.util.GroupUtil
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
class
GroupServiceTest
{
@InjectMocks
private
GroupService
groupService
;
@Mock
private
GroupRepository
groupRepository
;
@BeforeEach
void
setUp
()
{
MockitoAnnotations
.
openMocks
(
this
);
}
@Test
void
testGetGroupByName
()
{
String
groupName
=
"Test Group"
;
Group
group
=
new
Group
();
group
.
setGroupName
(
groupName
);
when
(
groupRepository
.
findByGroupName
(
groupName
)).
thenReturn
(
Optional
.
of
(
group
));
Optional
<
Group
>
result
=
groupService
.
getGroupByName
(
groupName
);
assertTrue
(
result
.
isPresent
());
assertEquals
(
groupName
,
result
.
get
().
getGroupName
());
}
@Test
void
testGetGroupById
()
{
long
groupId
=
1L
;
Group
group
=
new
Group
();
group
.
setGroupId
(
groupId
);
when
(
groupRepository
.
findById
(
groupId
)).
thenReturn
(
Optional
.
of
(
group
));
Optional
<
Group
>
result
=
groupService
.
getGroupById
(
groupId
);
assertTrue
(
result
.
isPresent
());
assertEquals
(
groupId
,
result
.
get
().
getGroupId
());
}
@Test
void
testCreateGroup
()
{
String
groupName
=
"New Group"
;
Group
group
=
new
Group
();
group
.
setGroupName
(
groupName
);
when
(
groupRepository
.
findByGroupName
(
groupName
)).
thenReturn
(
Optional
.
empty
());
when
(
groupRepository
.
findAllLinkCode
()).
thenReturn
(
Collections
.
emptyList
());
when
(
groupRepository
.
save
(
any
(
Group
.
class
))).
thenAnswer
(
invocation
->
invocation
.
getArgument
(
0
));
Group
createdGroup
=
groupService
.
createGroup
(
group
);
assertEquals
(
groupName
,
createdGroup
.
getGroupName
());
assertNotNull
(
createdGroup
.
getLinkCode
());
}
@Test
void
testGetLevelByGroupId
()
{
long
groupId
=
1L
;
long
level
=
3L
;
Group
group
=
new
Group
();
group
.
setGroupId
(
groupId
);
group
.
setLevel
(
level
);
when
(
groupRepository
.
findById
(
groupId
)).
thenReturn
(
Optional
.
of
(
group
));
when
(
groupRepository
.
getLevelByGroupId
(
groupId
)).
thenReturn
(
Optional
.
of
(
level
));
Optional
<
Long
>
result
=
groupService
.
getLevelByGroupId
(
groupId
);
assertTrue
(
result
.
isPresent
());
assertEquals
(
level
,
result
.
get
());
}
}
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