0% found this document useful (0 votes)
103 views

This Study Resource Was

The document contains test cases for user authentication using Mockito. It defines a UserAuthenticator class and UserAuthenticatorInterface. The test cases setup mocks for the interface and inject it into the authenticator. One test case expects an exception when invalid credentials are passed. Another test verifies authentication succeeds when valid credentials are used by configuring the mock's behavior. A third test case expects an exception for empty credentials.

Uploaded by

jeetu rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

This Study Resource Was

The document contains test cases for user authentication using Mockito. It defines a UserAuthenticator class and UserAuthenticatorInterface. The test cases setup mocks for the interface and inject it into the authenticator. One test case expects an exception when invalid credentials are passed. Another test verifies authentication succeeds when valid credentials are used by configuring the mock's behavior. A third test case expects an exception for empty credentials.

Uploaded by

jeetu rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.tcs.

fresco;
/* Write static mocks for Assert and Mockito classes. -Q1 */
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.junit.Assert.*;
//Write import statements for Mockito classes.
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

@RunWith(MockitoJUnitRunner.class)
public class UserAuthenticatorTest {
@InjectMocks
UserAuthenticator authenticator = new UserAuthenticator();
@Mock
public static UserAuthenticatorInterface authenticatorMock;

@BeforeClass

m
public static void setUp() {

er as
/* Create mock object using static mock configuration -Q2 */

co
//Write your code here

eH w
authenticatorMock.authenticateUser("User1", "Password");
}

o.
@Before
rs e
ou urc
public void setUpAuthenticator() {
authenticator.setUserAuthenticator(authenticatorMock);
}
o

/*Complete the test case with the expected exception -Q3 */


aC s

// Write your code here


v i y re

@Test(expected = FailedToAuthenticateException.class)
public void testAuthenticate_InvalidCredentials() throws
FailedToAuthenticateException {
String username = "User1";
ed d

String password = "wrong password";


ar stu

String errorMessage = "Invalid credentials .Authentication Failed";

/*Throw exception using doThrow...when configuration - Q4*/


// Write your code here
sh is

when(authenticator.authenticateUser("username", "password"))
.thenThrow(new FailedToAuthenticateException("Auth failure"));
Th

authenticator.authenticateUser(username, password);
}

@Test
public void testAuthenticate_ValidCredentials() throws
FailedToAuthenticateException {
String username = "User1";
String password = "Password";

UserAuthenticatorInterface authenticatorMock;
UserAuthenticator authenticator;
authenticatorMock =Mockito.mock(UserAuthenticatorInterface.class);
authenticator = new UserAuthenticator(authenticatorMock);

when(authenticatorMock.authenticateUser(username,password)).thenReturn(false);

This study source was downloaded by 100000804759950 from CourseHero.com on 04-21-2021 12:55:24 GMT -05:00

https://www.coursehero.com/file/88649618/Mockitotxt/
boolean actual =authenticator.authenticate(username, password);
assertFalse(actual);
}

@Test(expected=FailedToAuthenticateException.class)
public void testAuthenticate_EmptyCredentials() throws
FailedToAuthenticateException {
String username = "";
String password = "";
String errorMessage= "Credentials cannot be empty";

when(authenticator.authenticateUser("username", "password")).thenThrow(new
FailedToAuthenticateException("Auth failure"));
authenticator.authenticateUser(username, password);
}

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000804759950 from CourseHero.com on 04-21-2021 12:55:24 GMT -05:00

https://www.coursehero.com/file/88649618/Mockitotxt/
Powered by TCPDF (www.tcpdf.org)

You might also like