Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 9a34337c authored by riorio805's avatar riorio805
Browse files

[REFACTOR] Mock EmailService in AuthenticationServiceTest and refactor mocks...

[REFACTOR] Mock EmailService in AuthenticationServiceTest and refactor mocks to be more specific in implementation (any string instead of object)

Maybe a better implementation is to mock otpService instead so that less mocked classes are required, could be future work
parent 0758159f
No related branches found
No related tags found
No related merge requests found
......@@ -34,6 +34,9 @@ class AuthenticationServiceTest {
@Mock
private OTPService otpService;
@Mock
private EmailService emailService;
@InjectMocks
private AuthenticationService authenticationService;
......@@ -44,6 +47,9 @@ class AuthenticationServiceTest {
@Test
void testRegisterUser_UnderAge() {
// Assume email service works
when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(true);
RegistrationRequest request = new RegistrationRequest();
request.setEmail("test@example.com");
request.setPassword("password");
......@@ -59,6 +65,8 @@ class AuthenticationServiceTest {
@Test
void testRegisterUser_DuplicateEmail() {
when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(true);
RegistrationRequest request = new RegistrationRequest();
request.setEmail("test@example.com");
request.setPassword("password");
......@@ -75,6 +83,8 @@ class AuthenticationServiceTest {
@Test
void testRegisterUser_Success() {
when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(true);
RegistrationRequest request = new RegistrationRequest();
request.setEmail("test@example.com");
request.setPassword("password");
......
......@@ -24,7 +24,7 @@ class OTPServiceTest {
@Test
void testGenerateOTP() {
// Assume email service works
when(emailService.sendOTPMail(any(), any())).thenReturn(true);
when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(true);
String email = "user@example.com";
String otp = otpService.generateOTP(email);
......@@ -38,7 +38,7 @@ class OTPServiceTest {
@Test
void testVerifyOTPSuccess() {
when(emailService.sendOTPMail(any(), any())).thenReturn(true);
when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(true);
String email = "user@example.com";
String otp = otpService.generateOTP(email);
......@@ -50,7 +50,7 @@ class OTPServiceTest {
// TODO: Test has a 1/1,000,000 chance to fail because OTP can generate all 0's.
@Test
void testVerifyOTPWrongOtp() {
when(emailService.sendOTPMail(any(), any())).thenReturn(true);
when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(true);
String email = "user@example.com";
otpService.generateOTP(email);
......@@ -61,7 +61,7 @@ class OTPServiceTest {
@Test
void testVerifyOTPExpired() throws Exception {
when(emailService.sendOTPMail(any(), any())).thenReturn(true);
when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(true);
String email = "user@example.com";
String otp = otpService.generateOTP(email);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment