0% found this document useful (0 votes)
8 views12 pages

Java Defect Fixes and Test Cases Guide

The document outlines a series of defects in a Java application, detailing their IDs, names, descriptions, and proposed solutions for fixing them. Additionally, it provides a set of test cases for a hotel reservation system, covering various scenarios including successful reservations and error conditions. Lastly, it includes a Java method for calculating employee bonuses, along with JUnit test cases designed to ensure comprehensive coverage and validation of the method's functionality.

Uploaded by

Huy Lê
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Java Defect Fixes and Test Cases Guide

The document outlines a series of defects in a Java application, detailing their IDs, names, descriptions, and proposed solutions for fixing them. Additionally, it provides a set of test cases for a hotel reservation system, covering various scenarios including successful reservations and error conditions. Lastly, it includes a Java method for calculating employee bonuses, along with JUnit test cases designed to ensure comprehensive coverage and validation of the method's functionality.

Uploaded by

Huy Lê
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Defect ID: DF001

Defect Name: Naming convention violation


Line Number: 2
Defect Description: The variable EmployeeNames does not follow Java's MixedCase naming
convention.
Fixing Solution: Rename EmployeeNames to employeeNames to follow Java naming
conventions.

2. Defect ID: DF002

Defect Name: Incorrect null check for database connection


Line Number: 7
Defect Description: The condition if (dbConnection != null || [Link]()) is incorrect
because it should check both conditions separately.
Fixing Solution: Change the condition to if (dbConnection != null && ![Link]()).

3. Defect ID: DF003

Defect Name: SQL Injection Risk

Line Number: 30

Defect Description: Using Statement to execute SQL queries makes the code vulnerable to
SQL injection.

Fixing Solution: Use PreparedStatement instead of Statement to prevent SQL injection


attacks.

4. Defect ID: DF004

Defect Name: Missing exception handling in closeConnection()


Line Number: 42
Defect Description: [Link]() can throw a SQLException which is not handled.
Fixing Solution: Wrap [Link]() in a try-catch block to handle the exception
properly.

5. Defect ID: DF005

Defect Name: Incorrect string comparison


Line Number: 15
Defect Description: [Link]() > 0 is not the best practice for checking if a string is
empty.
Fixing Solution: Use ![Link]() instead of [Link]() > 0.

6. Defect ID: DF006

Defect Name: Missing null check before closing connection


Line Number: 42
Defect Description: There’s no check for dbConnection being null before calling close(),
which can cause a NullPointerException.
Fixing Solution: Add a null check before closing: if (dbConnection != null)
[Link]();

Q3:

Test Case 1: Successful Hotel Reservation (Normal Flow)

Test Case ID: TC001


Test Case Description: Test successful reservation creation for available rooms.
Preconditions:

● Customer has an account on the hotel booking system.

● Rooms are available for the selected dates.

Test Steps:

1. Log into the system with valid credentials.

2. Select valid check-in and check-out dates (e.g., Check-in: 2025-06-10, Check-out:
2025-06-20).

3. System displays available rooms for the selected dates.

4. Select a room type (e.g., Double).

5. Enter valid personal details (name, contact) and payment information (valid credit
card).

6. Confirm payment.

Expected Results:

● System verifies payment details.

● Reservation is confirmed.

● Confirmation email is sent to the customer.

● Room availability is updated.

Notes: NF (Normal Flow)

Test Case 2: Invalid Check-out Date (Alternative Flow A1)


Test Case ID: TC002
Test Case Description: Test behavior when check-out date is before check-in date.
Preconditions:

● Customer has an account on the hotel booking system.

Test Steps:

1. Log into the system with valid credentials.

2. Select check-in date as 2025-06-15 and check-out date as 2025-06-10.

Expected Results:

● System displays an error message: "Check-out date cannot be before check-in date."

● System prompts the user to enter valid dates.

Notes: AL (Alternative Flow)

Test Case 3: No Rooms Available (Alternative Flow A2)

Test Case ID: TC003


Test Case Description: Test behavior when no rooms are available for the selected dates.
Preconditions:

● Customer has an account on the hotel booking system.

● No rooms available for the selected dates.

Test Steps:

1. Log into the system with valid credentials.

2. Select dates for check-in and check-out (e.g., 2025-07-01 to 2025-07-05).

Expected Results:

● System displays a message: "No rooms available for selected dates."

● System offers alternative dates or room types.

Notes: AL (Alternative Flow)


Test Case 4: Payment Declined (Alternative Flow A3)

Test Case ID: TC004


Test Case Description: Test behavior when payment is declined.
Preconditions:

● Customer has an account on the hotel booking system.

● Valid room selection.

● Payment method has insufficient funds.

Test Steps:

1. Log into the system with valid credentials.

2. Select valid check-in and check-out dates.

3. Select a room type.

4. Enter valid personal details and payment information (card with insufficient funds).

Expected Results:

● System displays error: "Payment declined. Please provide a different payment


method."

● Customer is prompted to try a different payment method.

Notes: AL (Alternative Flow)

Test Case 5: Cancel Reservation Before Payment (Alternative Flow A4)

Test Case ID: TC005


Test Case Description: Test behavior when the customer cancels the reservation process
before payment.
Preconditions:

● Customer has an account on the hotel booking system.

Test Steps:

1. Log into the system with valid credentials.

2. Select valid check-in and check-out dates.


3. Select a room type.

4. Enter personal details.

5. Cancel before payment.

Expected Results:

● System cancels the process without saving any details.

Notes: AL (Alternative Flow)

Test Case 6: System Error During Payment Processing (Exception Scenario E1)

Test Case ID: TC006


Test Case Description: Test behavior when the system experiences an error during payment
processing.
Preconditions:

● Customer has an account on the hotel booking system.

● Payment gateway is down.

Test Steps:

1. Log into the system with valid credentials.

2. Select valid check-in and check-out dates.

3. Select a room type.

4. Enter valid personal details and payment information.

5. Attempt to confirm payment.

Expected Results:

● System displays error message: "Payment processing error. Please try again later."

● System logs the user out.

Notes: EX (Exception Scenario)

Test Case 7: Reservation After Check-in Date Has Passed (Exception Scenario E2)
Test Case ID: TC007
Test Case Description: Test behavior when the customer tries to reserve a room after the
check-in date has passed.
Preconditions:

● Customer has an account on the hotel booking system.

Test Steps:

1. Log into the system with valid credentials.

2. Select check-in date in the past (e.g., 2025-01-01).

Expected Results:

● System displays error: "Check-in date cannot be in the past."

● System prompts the customer to select new dates.

Notes: EX (Exception Scenario)

Test Case 8: System Error During Payment Processing (Exception Scenario E1)

Test Case ID: TC008


Test Case Description: Test behavior when the system experiences an error during payment
processing.
Preconditions:

● Customer has an account on the hotel booking system.

● Payment gateway is down or network issues occur during processing.

Test Steps:

1. Log into the system with valid credentials.

2. Select valid check-in and check-out dates (e.g., 2025-07-10 to 2025-07-15).

3. System shows available rooms.

4. Select a room type (e.g., Suite).

5. Enter valid personal details and payment information.


6. Attempt to confirm payment (simulate payment gateway error).

Expected Results:

● System displays error message: "Payment processing error. Please try again later."

● System logs the user out.

● Reservation is not saved.

Notes: EX (Exception Scenario)

Question 2 (5 points):
The following Java method calculates the bonus for employee based on item year of service,
salary, and performance. The method involves different calculations for various scenarios.
Your task is to design test cases using JUnit for this method. You should apply equivalence
partitioning and boundary value analysis for the input parameters. Additionally, ensure that
your test cases cover all branches of the code to achieve 100% code coverage.
public class Employee BonusCalculator {
public double calculateBonus (int yearsofservice, double salary, boolean hasoutstanding
Performance) { if (yearsofService < 11 salary <<= 0) {
throw new IllegalArgumentException("Invalid input: yearsofservice and salary must be
positive.");
double bonusPercentage=0.0;
if (yearsofService > 10) ( bonusPercentage=0.10;
} else if (yearsofservice> 5) { bonus Percentage = 0.07;
} else if (yearsofService > 2) {
bonus Percentage
} else {
}
0.05;
bonusPercentage=0.02;
if (hasOutstandingPerformance) {
bonusPercentage + 0.03;
1
return salary bonus Percentage;
[Link].
Sample JUnit Test Class for Completion
The following is a partially completed JUnit test class for the Employee BonusCalculator
method. Students are required to complete this class by adding the necessary test cases to
achieve full equivalence partitioning, boundary value analysis, and branch coverage for the
Employee BonusCalculator method. After completing the tests, students should copy and
paste the test code into a Word document for evaluation.
Test Case Years of Salary Performance Expected Outcome
Service

1 -1 500 true Exception

2 5 -500 true Exception

3 5 0 true Exception

4 0 50000 false 50000 × 0.02

5 1 50000 true 50000 × (0.02 + 0.03)

6 3 50000 false 50000 × 0.05

7 5 50000 true 50000 × (0.07 + 0.03)

8 10 50000 false 50000 × 0.07

9 11 50000 true 50000 × (0.10 + 0.03)

10 0 0.01 false 0.01 × 0.02

Tham số Ý nghĩa Ví dụ

expected Giá trị kỳ vọng mà bạn mong muốn phương thức trả về 50000 *
0.05
actual Giá trị thực tế mà phương thức trả về khi thực thi bonus

delta Sai số cho phép giữa expected và actual khi so sánh (chỉ áp dụng 0.01
cho giá trị kiểu double hoặc float)

1. Test Case 1
- ID: TC1
● Test for: yearsOfService < 0
● Input parameter: -1, 500, true
● Expected result: Exception (IllegalArgumentException)

Test code:

@Test(expected = [Link])
public void testCase1() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
[Link](-1, 500, true);
}

2. Test Case 2
- ID: TC2
● Test for: salary < 0
● Input parameter: 5, -500, true
● Expected result: Exception (IllegalArgumentException)

Test code:

@Test(expected = [Link])
public void testCase2() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
[Link](5, -500, true);
}

3. Test Case 3
- ID: TC3
● Test for: salary = 0
● Input parameter: 5, 0, true
● Expected result: Exception (IllegalArgumentException)

Test code:

@Test(expected = [Link])
public void testCase3() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
[Link](5, 0, true);
}

4. Test Case 4
- ID: TC4
● Test for: yearsOfService = 0, salary = 50000, performance = false
● Input parameter: 0, 50000, false
● Expected result: 50000 × 0.02

Test code:

@Test
public void testCase4() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
double bonus = [Link](0, 50000, false);
assertEquals(50000 * 0.02, bonus, 0.01);
}

5. Test Case 5
- ID: TC5
● Test for: yearsOfService = 1, salary = 50000, performance = true
● Input parameter: 1, 50000, true
● Expected result: 50000 × (0.02 + 0.03)

Test code:

@Test
public void testCase5() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
double bonus = [Link](1, 50000, true);
assertEquals(50000 * (0.02 + 0.03), bonus, 0.01);
}

6. Test Case 6
- ID: TC6
● Test for: yearsOfService = 3, salary = 50000, performance = false
● Input parameter: 3, 50000, false
● Expected result: 50000 × 0.05

Test code:

@Test
public void testCase6() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
double bonus = [Link](3, 50000, false);
assertEquals(50000 * 0.05, bonus, 0.01);
}
7. Test Case 7
- ID: TC7
● Test for: yearsOfService = 5, salary = 50000, performance = true
● Input parameter: 5, 50000, true
● Expected result: 50000 × (0.07 + 0.03)

Test code:

@Test
public void testCase7() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
double bonus = [Link](5, 50000, true);
assertEquals(50000 * (0.07 + 0.03), bonus, 0.01);
}

8. Test Case 8
- ID: TC8
● Test for: yearsOfService = 10, salary = 50000, performance = false
● Input parameter: 10, 50000, false
● Expected result: 50000 × 0.07

Test code:

@Test
public void testCase8() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
double bonus = [Link](10, 50000, false);
assertEquals(50000 * 0.07, bonus, 0.01);
}

9. Test Case 9
- ID: TC9
● Test for: yearsOfService = 11, salary = 50000, performance = true
● Input parameter: 11, 50000, true
● Expected result: 50000 × (0.10 + 0.03)

Test code:

@Test
public void testCase9() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
double bonus = [Link](11, 50000, true);
assertEquals(50000 * (0.10 + 0.03), bonus, 0.01);
}
10. Test Case 10
- ID: TC10
● Test for: yearsOfService = 0, salary = 0.01, performance = false
● Input parameter: 0, 0.01, false
● Expected result: 0.01 × 0.02

Test code:

@Test
public void testCase10() {
EmployeeBonusCalculator calculator = new EmployeeBonusCalculator();
double bonus = [Link](0, 0.01, false);
assertEquals(0.01 * 0.02, bonus, 0.01);
}

You might also like