Thursday, October 14, 2021

Fresco-Play-Java8-Qualis-Hands-On

 1. JUnit


package com.tcs.fresco;


import static junit. framework.Assert.*;


import java.util.HashMap;

import java.util.Map;


import org.junit.After;

import org.junit.Before;


import junit.framework.Test;

import junit.framework.TestCase;

import junit.framework.TestSuite;


public class SolutionTest extends TestCase{

    Map<Integer, String> customerTag = null;

int custId = 0;

Solution solution = new Solution();

    

    @Before

public void setUp() throws Exception {

custId = 10511;

customerTag = new HashMap<Integer, String>();

}

    

    //Write your test cases here

    

    public void test() {

    assertTrue( true );

    }


    @org.junit.Test

    public void testValidUser(){

      solution.saveCustomer(this.custId, "Tej");

      assertTrue(solution.isCustomerExist("Tej"));

    }

     @org.junit.Test

    public void testinValidUser(){

      solution.saveCustomer(this.custId, "Tej");

      assertFalse(solution.isCustomerExist("Tej1"));

    }

    

    

    @After

public void tearDown() throws Exception {


customerTag.clear();

}

}

2. Mockito


package com.tcs.fresco;



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


import static org.junit.Assert.*;

import static org.mockito.Mockito.doThrow;

//Write import statements for Mockito classes.

import static org.mockito.Mockito.mock;

import static org.mockito.Mockito.when;

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() {

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

      //Write your code here

       authenticatorMock = mock(UserAuthenticatorInterface.class);

        

}


@Before

public void setUpAuthenticator() {

authenticator.setUserAuthenticator(authenticatorMock);

}


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


      // Write your code here

  @Test(expected = FailedToAuthenticateException.class)

public void testAuthenticate_InvalidCredentials() throws FailedToAuthenticateException {


String username = "User1";

String password = "wrong password";

String errorMessage = "Invalid credentials .Authentication Failed";

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

        // Write your code here

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

authenticator.authenticateUser(username, password);


}


@Test

public void testAuthenticate_ValidCredentials() throws FailedToAuthenticateException {


String username = "User1";

String password = "Password";

    /*Configure Returning True with when...thenReturn configuration on mock Object - Q5*/

        //Write your code here

    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";

        /*Configure Throwing exception using when...thenThrow configuration on mock Object - Q6*/

        //Write your code here

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

     authenticator.authenticateUser(username, password);

        authenticator.authenticateUser(username, password);


}

    @Test()

    public void dummytest(){

        assertEquals(1,1);

    }

}

3. MocMVC


No Need to Write anything Only Submit it.


4. CheckStyle


No Need to Write anything Only Submit it.


5. FindBugs 


No Need to Write anything Only Submit it.


6. JoCoCo 


No Need to Write anything Only Submit it.