Java 8 Qualis Fresco Play handson Solution Hacker Rank

Learn important topics of JAVA programming such as JUnit, Mockito, MockMvc, and JaCoCo. Java 8 Qualis Fresco Play
Java 8 Qualis Fresco Play handson Solution Hacker Rank - www.pdfcup.com

LAB 1: Welcome to Hands On - JUnit

JUnit - Hands-On

Solution:

 

// FileName: SolutionTest.java
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;
	String userName = "";
	Solution solution = new Solution();
    
  @Before
	public void setUp() throws Exception {
		custId = 10511;
		userName = "India";
		customerTag = new HashMap<Integer, String>();
	}
    
	//Write your test cases here // testPositiveCase  testPositiveCase
	@org.junit.Test
	public void testPositiveCase(){
		// String 
		solution.saveCustomer(this.custId, this.userName);
		assertTrue(solution.isCustomerExist(this.userName));
	}

	@org.junit.Test
	public void testNegativeCase(){
		solution.saveCustomer(this.custId, this.userName);
		assertFalse(solution.isCustomerExist("xyz"));

	}
	public void test() {
		assertTrue( true );
	}
    
    
  @After
	public void tearDown() throws Exception {
		customerTag.clear();
	}
}

 

//fileName: Solution.java 

package com.tcs.fresco;
import java.util.HashMap;
import java.util.Map;
public class Solution {
    
   public Map<Integer, String> customerTag = new HashMap<Integer, String>();

   public void saveCustomer(int custId, String custName) {
		customerTag.put(custId, custName);
   }
   public boolean isCustomerExist(String customerName) {
		boolean isExist = customerTag.containsValue(customerName);
		return isExist;
   }
  
}
    
    

LAB 2: Welcome to Hands-On-Mockito

Hands-on Mockito

Solution:

 

package com.tcs.fresco;

// Task 1: Write static mocks for Assert and Mockito classes. -Q1

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


public class UserAuthenticatorTest {
    UserAuthenticator authenticator = new UserAuthenticator();
    public static UserAuthenticatorInterface authenticatorMock;

    @BeforeClass
    public static void setUp() {
        //Task 2: Create mock object using static mock configuration  -Q2
        //Write your code here
        authenticatorMock = mock(UserAuthenticatorInterface.class);
    }



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

    // Task 3: 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";


        // Task 4: 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";

        // Task 5: 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";

        // Task 6: 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);
    }

    @Test()
    public void dummytest() {
        assertEquals(1, 1);
    }

} 

LAB 3: Welcome to MockMvc Hands-On

MockMvc: Hands-on

Solution: Just run and Pass

 

This handson will be cleared automatically, just run the test-case and submit.

However, if you have tried writing code in this handson and facing any difficulty then you can share your errors in comments.

LAB 4: Welcome to Hands-On-JaCoCo, Configure JaCoCo with Maven.

JUnit - JaCoCo: Hands-on

Solution:

 

This handson will be cleared automatically, just run the test-case and submit.

However, if you have tried writing code in this handson and facing any difficulty then you can share your errors in comments.

LAB 5: Welcome to Hands-On-FindBugs

FindBugs: Hands-on

Solution:

 

This handson will be cleared automatically, just run the test-case and submit.

However, if you have tried writing code in this handson and facing any difficulty then you can share your errors in comments.

LAB 6: Welcome to Hands-On-Checkstyle

Checkstyle: Hands-on

Solution:

 

This handson will be cleared automatically, just run the test-case and submit.

However, if you have tried writing code in this handson and facing any difficulty then you can share your errors in comments.

About the author

D Shwari
I'm a professor at National University's Department of Computer Science. My main streams are data science and data analysis. Project management for many computer science-related sectors. Next working project on Al with deep Learning.....

Post a Comment