Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Fullstack
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
Madeleine Stenberg Jonassen
Fullstack
Commits
19c62abf
Commit
19c62abf
authored
1 year ago
by
Torbjørn Antonsen
Browse files
Options
Downloads
Patches
Plain Diff
Added method testVerifyAppUser?ExistingUser and ?NonExistingUser to UserServiceTest.java
parent
1d8a096a
No related branches found
No related tags found
No related merge requests found
Pipeline
#267950
passed
1 year ago
Stage: build
Stage: test
Stage: package
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
FullstackProsjekt/src/backend/test/java/edu/ntnu/idatt2105/UserServiceTest.java
+43
-2
43 additions, 2 deletions
...backend/test/java/edu/ntnu/idatt2105/UserServiceTest.java
with
43 additions
and
2 deletions
FullstackProsjekt/src/backend/test/java/edu/ntnu/idatt2105/UserServiceTest.java
+
43
−
2
View file @
19c62abf
...
...
@@ -11,8 +11,7 @@ import org.springframework.security.core.userdetails.UserDetails;
import
org.springframework.security.core.userdetails.UsernameNotFoundException
;
import
java.util.Optional
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
static
org
.
mockito
.
Mockito
.*;
@ExtendWith
(
MockitoExtension
.
class
)
...
...
@@ -68,5 +67,47 @@ class UserServiceTest {
assertThrows
(
UsernameNotFoundException
.
class
,
()
->
userService
.
loadUserByUsername
(
username
));
}
@Test
void
testVerifyAppUser_ExistingUser
()
{
// Mocking UserRepository
UserRepository
userRepository
=
mock
(
UserRepository
.
class
);
// Creating a sample user
User
user
=
new
User
();
user
.
setUsername
(
"existingUser"
);
user
.
setPassword
(
"password123"
);
// Mocking UserRepository's behavior to return an existing user when queried by username or password
when
(
userRepository
.
findByUsername
(
user
.
getUsername
())).
thenReturn
(
Optional
.
of
(
user
));
when
(
userRepository
.
findByPassword
(
user
.
getPassword
())).
thenReturn
(
Optional
.
of
(
user
));
// Creating an instance of UserService
UserService
userService
=
new
UserService
(
userRepository
);
// Verifying that the method returns true for an existing user
assertTrue
(
userService
.
verifyAppUser
(
user
));
}
@Test
void
testVerifyAppUser_NonExistingUser
()
{
// Mocking UserRepository
UserRepository
userRepository
=
mock
(
UserRepository
.
class
);
// Creating a sample user
User
user
=
new
User
();
user
.
setUsername
(
"nonExistingUser"
);
user
.
setPassword
(
"password123"
);
// Mocking UserRepository's behavior to return empty Optional when queried by username or password
when
(
userRepository
.
findByUsername
(
user
.
getUsername
())).
thenReturn
(
Optional
.
empty
());
when
(
userRepository
.
findByPassword
(
user
.
getPassword
())).
thenReturn
(
Optional
.
empty
());
// Creating an instance of UserService
UserService
userService
=
new
UserService
(
userRepository
);
// Verifying that the method returns false for a non-existing user
assertFalse
(
userService
.
verifyAppUser
(
user
));
}
}
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