SWT301_-_PE_-_SAMPLE_-_FA_2024_2909.webp
Minthep

SWT301_-_PE_-_SAMPLE_-_FA_2024_2909.webp

  • Media owner Minthep
  • Ngày thêm
Question 2 (5 points):
The following Java method processes an employee's salary, calculating bonuses and deductions based on years of service, basic salary, and performance rating. Your task is to design test cases using JUnit to validate this method. Apply equivalence partitioning and boundary value analysis for the input parameters. Additionally, ensure that your test cases cover all four branches of the code for 100% code coverage.
1234567 6
7
8
9
10
11

12
13
14
15
16
17
18
19
20
21
22
public class EmployeeSalary {
public double calculateSalary(int yearsOfService, double basicSalary, double performanceRating) { if (yearsOfService < 0 || basicSalary <= 0 || performanceRating < 0 || performanceRating > 5) { throw new IllegalArgumentException("Invalid input values");
double bonus = 0.0;
double deduction 0.0;
if (yearsOfService > 10) {
bonus 0.10 * basicSalary:
} else if (yearsOfService >= 5) { bonus 0.05 basicSalary:
if (performanceRating < 3) {
deduction 0.05 basicSalary:
return basicSalary + bonus deduction;
Sample JUnit Test Class for Completion
The following is a partially written JUnit test class for the EmployeeSalary Test method. Students are tasked with completing this class by adding the required test cases to achieve full equivalence partitioning, boundary value analysis, and branch coverage for the calculateSalary method.
24
import static org.junit.Assert.*;
25
import org.junit.Test;
26
27
public class EmployeeSalaryTest {
28
29
@Test (expectedIllegalArgumentException.class)public void testInvalidYearsOfService() {
30
31
32
EmployeeSalary salaryCalculator = new EmployeeSalary();
salaryCalculator.calculateSalary (-1, 50000, 4); // Invalid years of service
33
}
34
35
@Test
36 白
37
38
39
public void testYearsOfServiceGreaterThanTen () {
Employeesalary salaryCalculator new Employeesalary();
double result salaryCalculator.calculateSalary (11, 50000, 4); // Branch 1
assertEquals (55000.0, result, 0.01); // 10% bonus
40
}
41
// Need add more test code...
42
43
}

Thông tin

Category
SWT301
Thêm bởi
Minthep
Ngày thêm
Lượt xem
6,780
Lượt bình luận
5
Rating
0.00 star(s) 0 đánh giá

Share this media

Back
Bên trên Bottom