Professional Documents
Culture Documents
This Study Resource Was
This Study Resource Was
This Study Resource Was
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
@Test(expected = FailedToAuthenticateException.class)
public void testAuthenticate_InvalidCredentials() throws
FailedToAuthenticateException {
String username = "User1";
ed d
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)