Java8 Qualis Hands-on Solution | TCS Fresco Play

0
Java8 Qualis Hands-on Solution  |  TCS Fresco Play

Disclaimer: The primary purpose of providing this solution is to assist and support anyone who are unable to complete these courses due to a technical issue or a lack of expertise. This website's information or data are solely for the purpose of knowledge and education.

Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).

Course Path: Microservices/Construction/Java8 Qualis

Suggestion: Just Copy whole code from below and replace with existing code on hackerrank.


1. Welcome to Hands On – Junit

 

File Name – SolutionTest.java (under src->test folder)

 

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.Hands-on Mockito

 

File Name- UserAuthenticatorTest.java 

 

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.MockMvc: Hands-on


No need to write code just submit and it will clear.


4.Checkstyle: Hands-on


No need to write code just submit and it will clear.


5.FindBugs: Hands-on


No need to write code just submit and it will clear.


6.JaCoCo: Hands-on


No need to write code just submit and it will clear.



If you have any queries, please feel free to ask on the comment section.

If you want MCQs and Hands-On solutions for any courses, Please feel free to ask on the comment section too.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !

Join Telegram Channel

Join MNCAnswers To Get Instant Updates

Join Telegram Channel