Q.1.5 You are required to write unit tests for the application. Create a test package within the
application you created, which will contain the necessary unit tests.
You are required to write the following unit tests:
Test Name Test Purpose
CalculateTotalSales_ReturnsTotalSales() To supply the property sales to the
estate agent sales method. The test
will determine that the correct total
sales value is returned from the estate
agent sales method.
CalculateTotalCommission_ReturnsCommission() To supply a total property sales
amount to the estate agent
commission method. The test will
determine that the correct estate
agent commission has been
calculated.
TopAgent_ReturnsTopPosition() To supply total sales for each estate
agent to the top estate agent method.
The test will determine the top selling
total property sales for the estate
agent..include import method.In Java
1 answer
Step 1: Create a new package in your application. Right-click on the src/main/java folder and select New -> Package. Name the package "test" or any other suitable name.
Step 2: Create a new Java class for each unit test. Right-click on the test package, select New -> Class. Name the class according to the test purpose. For example, you can name the first test class CalculateTotalSales_ReturnsTotalSalesTest.
Step 3: Write the unit test method inside the test class. Here's an example of how you can write the CalculateTotalSales_ReturnsTotalSales test:
```java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalculateTotalSales_ReturnsTotalSalesTest {
@Test
public void testTotalSalesCalculation() {
// Create an instance of the EstateAgent class
EstateAgent agent = new EstateAgent();
// Call the sales method on the agent with some test data
agent.sales(1000);
agent.sales(2000);
agent.sales(3000);
// Call the getTotalSales method and assert the expected result
assertEquals(6000, agent.getTotalSales());
}
}
```
Step 4: Repeat Step 3 for the remaining unit tests (CalculateTotalCommission_ReturnsCommissionTest and TopAgent_ReturnsTopPositionTest). Adjust the test logic and expected results accordingly.
Step 5: Run the tests. Right-click on the test package, select Run As -> JUnit Test. The test results will be displayed in the JUnit panel.
Note: Make sure to include the necessary import statements at the top of each test class, such as import org.junit.jupiter.api.Test; and import static org.junit.jupiter.api.Assertions.assertEquals;.