You are on page 1of 2

package com.tcs.

fresco;

/* Write static mocks for Assert and Mockito classes. -Q1 */


import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doThrow;
import static org.mockito.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;

public class UserAuthenticatorTest {

UserAuthenticator authenticator = new UserAuthenticator();


public static UserAuthenticatorInterface authenticatorMock;

@BeforeClass
public static void setUp() {

authenticatorMock = mock(UserAuthenticatorInterface.class);

@Before
public void setUpAuthenticator() {
authenticator.setUserAuthenticator(authenticatorMock);
}

@Test(expected=FailedToAuthenticateException.class)
public void testAuthenticate_InvalidCredentials() throws
FailedToAuthenticateException {

String username = "User1";


String password = "wrong password";
String errorMessage = "Invalid credentials .Authentication Failed";

doThrow(new
FailedToAuthenticateException(errorMessage)).when(authenticatorMock).authenticateUs
er("User1","wrong password"));

authenticator.authenticateUser(username, password);

@Test
public void testAuthenticate_ValidCredentials() throws
FailedToAuthenticateException {

String username = "User1";


String password = "Password";

when(authenticatorMock.authenticateUser(username, password)).thenReturn(true);
assertTrue(authenticator.authenticateUser(username, password));

@Test(expected=FailedToAuthenticateException.class)
public void testAuthenticate_EmptyCredentials() throws
FailedToAuthenticateException {

String username = "";


String password = "";
String errorMessage= "Credentials cannot be empty";

when(authenticatorMock.authenticateUser(username, password)).thenThrow(new
FailedToAuthenticateException(errorMessage));

authenticator.authenticateUser(username, password);

You might also like