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

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 customerTag = null;
	int custId = 0;
	String userName = "";
	Solution solution = new Solution();
    
  @Before
	public void setUp() throws Exception {
		custId = 10511;
		userName = "India";
		customerTag = new HashMap();
	}
    
	//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 customerTag = new HashMap();

   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

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.....

إرسال تعليق

Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.