Question

How can I write a unit test for the following java method? (I am using intelliJ)...

How can I write a unit test for the following java method? (I am using intelliJ)

public class ReviewEmployee{

public void EmployeeReview(Employee currentEmployee){

System.out.println("Your recorded hours for the week are: ") + (currentEmployee.getHours());

System.out.println("Your satisfactory rating is: ") + (currentlyEmployee.getRating());

System.out.println("Your bonus is: ");

if(currentEmployee.getRating() < 3){

System.out.println(currentlyEmployee.getBonus());

}

else{

System.out.println(currentlyEmployee.getBonus() + 500);

}

}

}

The Employee class is as follows:

Public class Employee{

               Private double hours;

               Private double rating;

               Private double bonus;

               Public Employee(double hours, double rating, double bonus){

              

               This.hours=hours;

               This.rating=rating;

               This.bonus=bonus;

               }

               Public double getHours(){

               Return hours;

               }

               Public double getRating(){

               Return rating;

               }

               Public double getBonus(){

               Return bonus;

               }

This is what I have so far:

import org.junit.Test;



import static org.junit.Assert.*;

public class EmployeeReviewTest {

    @Test

    public void EmployeeReview() {

        EmpReviewTest x = new EmpReviewTest();

    }
}

Please be detailed so I can understand, thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Employee.java

public class Employee {

    private double hours;

    private double rating;

    private double bonus;

    public Employee(double hours, double rating, double bonus) {
        this.hours = hours;
        this.rating = rating;
        this.bonus = bonus;
    }

    public double getHours() {
        return hours;
    }

    public double getRating() {
        return rating;
    }

    public double getBonus() {
        return bonus;
    }
}
ReviewEmployee.java
//import org.junit.Test;
//import static org.junit.Assert.*;

public class ReviewEmployee {
    public double EmployeeReview(Employee currentEmployee) {
        System.out.println("Your recorded hours for the week are: " + currentEmployee.getHours());
        System.out.println("Your satisfactory rating is: " + currentEmployee.getRating());
        System.out.println("Your bonus is: ");

        /*RETURN  TYPE WILL REMAIN VOID*/
//        if (currentEmployee.getRating() < 3) {
//            System.out.println(currentEmployee.getBonus());
//        } else {
//            System.out.println(currentEmployee.getBonus() + 500);
//        }
//        assertEquals(900, currentEmployee.getBonus(), 0);
//        assertEquals(40, currentEmployee.getHours(), 0);
//        assertEquals(4, currentEmployee.getRating(), 0);

        /*RETURN  TYPE WILL BE DOUBLE */
        if (currentEmployee.getRating() < 3) {
            return currentEmployee.getBonus();
        }
        return currentEmployee.getBonus() + 500;
    }
}

// comment one section to check the result accordingly
EmployeeReviewTest.java
import org.junit.Test;
import static org.junit.Assert.*;

public class EmployeeReviewTest {
    @Test
    public void EmployeeReview() {
        Employee employee = new Employee(40, 4, 400);
        ReviewEmployee reviewEmployee = new ReviewEmployee();
        assertEquals(900, reviewEmployee.EmployeeReview(employee), 0);
        assertEquals(40, employee.getHours(), 0);
        assertEquals(4, employee.getRating(), 0);
        assertEquals(400, employee.getBonus(), 0);

        employee = new Employee(40, 2, 400);
        reviewEmployee = new ReviewEmployee();
        assertEquals(400, reviewEmployee.EmployeeReview(employee), 0);
        assertEquals(40, employee.getHours(), 0);
        assertEquals(2, employee.getRating(), 0);
        assertEquals(400, employee.getBonus(), 0);
    }
}
// thus @Test method will test the given hour,rating and bonus and the bonus after rating calculation

// assert equals method check whether the expected value and the value returned from the method are equal or not
// and delta is to maximum allowable difference which can be expected in result

// see i have make one change in ReviewEmployee class that is it will return double instead of void
// if you want to remain it void than we have to implement
// assertEquals in ReviewEmployee class only because we have to pass three parameters which cannot be void
Output

// COMMENT IN CASE OF ANY QUERY

Add a comment
Know the answer?
Add Answer to:
How can I write a unit test for the following java method? (I am using intelliJ)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • How do I test this with JUnit? I watched a video on YouTube about how to...

    How do I test this with JUnit? I watched a video on YouTube about how to create and run a Simple JUnit test in Eclipse IDE but I still don't understand. Here's what I need to test: public class ReverseDomainName {    public static void main(String[] args)    {        String domain = "cs.princeton.edu";        System.out.println("domain is " + domain);        System.out.println("reverse of domain is " + rev_domain(domain));    }       public static String rev_domain(String domain)...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • I was wondering if I could get some help with a Java Program that I am...

    I was wondering if I could get some help with a Java Program that I am currently working on for homework. When I run the program in Eclipse nothing shows up in the console can you help me out and tell me if I am missing something in my code or what's going on? My Code: public class Payroll { public static void main(String[] args) { } // TODO Auto-generated method stub private int[] employeeId = { 5658845, 4520125, 7895122,...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • Hi so I am currently working on a project for a java class and I am...

    Hi so I am currently working on a project for a java class and I am having trouble with a unit testing portion of the lab. This portion wants us to add three additional tests for three valid arguments and one test for an invalid argument. I tried coding it by myself but I am not well versed in unit testing so I am not sure if it is correct or if the invalid unit test will provide the desired...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT