Question: JUnit testing quesiton: Statement: The process of test driven development involves first writing a suite of tests and then writing the implementation to pass those

JUnit testing quesiton:

Statement:

The process of test driven development involves first writing a suite of tests and then writing the implementation to pass those tests. It is known as "Red, Green, Refactor" (the Refactor step will be covered in topic 4).

Red Step

First, execute the tests to acknowledge the initialization error:

JUnit testing quesiton: Statement: The process of test driven development involves first

Next, create a new class: Uber

Run the tests gain and acknowledge the six failing tests:

writing a suite of tests and then writing the implementation to pass

Green Step

Pass one unit test at a time by implementing the Uber. The comments and code in the unit tests should be a sufficient guide for this. But the following context may be helpful:

Your Uber class will be used by some server-side logic of an arbitrary ride-sharing application to instantiate and manage driver objects. Job fares are calculated by multiplying a fare rate by the number of seconds elapsed between when the passenger is picked up and set down. In some situations, there may be a surge multiplier involved for when there is high demand for drivers.

This Oracle documentation on Period and Duration may be helpful.

Finally, upload and submit your 1 class:

Uber Source code (UberTests.java):

package junit.Uber;

import static org.junit.Assert.assertEquals;

import java.util.Random;

import org.junit.Before;

import org.junit.Test;

public class UberTests

{

// Set up testing helpers

private static Random random = new Random();

private static String[] names = new String[] {

"Brittney Threet",

"Ludivina Hensley",

"Charlsie Beckstead",

"Khadijah Geter",

"Candelaria Truax",

"Denice Lucero",

"Abdul Creegan",

"Jess Krebbs",

"Eladia Duty",

"Leanna Kyles",

"Lester Anchondo",

};

private static String[] carModels = new String[] {

"Dodge",

"Audi",

"Holden",

"Suzuki",

"BMW",

"Aston Martin",

};

private static String randName() {

return names[random.nextInt(names.length)];

}

private static String randCarModel() {

return carModels[random.nextInt(carModels.length)];

}

private static long randWaitTime() {

long scale = 500;

return (long) (1000 + random.nextDouble() * scale);

}

/*

* Test 0: Declaring Uber objects.

* [This test obliges you to create a class called "Uber".]

*/

Uber uber;

// Clear the uber object before every test.

@Before

public void setUpUber() {

uber = null;

}

/*

* Test 1: Constructing a basic Uber object.

* [This test obliges you to add a constructor with two parameters]

*

*/

@Test

public void testConstruction() {

String driverName = randName();

String carModel = randCarModel();

uber = new Uber(driverName, carModel);

}

/*

* Test 2: Get the driver's name

* [This test obliges you to add a getter method for the driver's name]

*/

@Test

public void tesDriverName() {

String driverName = randName();

String carModel = randCarModel();

uber = new Uber(driverName, carModel);

assertEquals(driverName, uber.getDriverName());

}

/*

* Test 3: Get the driver's car model.

* [This test obliges you to aadd a getter method for the driver's car model]

*/

@Test

public void testCarModel() {

String driverName = randName();

String carModel = randCarModel();

uber = new Uber(driverName, carModel);

assertEquals(carModel, uber.getCarModel());

}

/*

* Test 4: Update the global fare rate.

* [This test obliges you to add a static field, static setter, and static getter for the fare rate]

*/

@Test

public void testSetFareRate() {

double newFareRate = random.nextDouble() * 10;

Uber.setFareRate(newFareRate);

assertEquals(newFareRate, Uber.getFareRate(), 0.0);

}

/*

* Test 5: A simple job.

* [This test obliges you to add two methods for controlling jobs. The "setdownPassenger" method should

* return the cost of the job based on the fareRate and the time between pickup and setdown.]

*/

@Test

public void testSimpleJob() throws InterruptedException {

double fareRate = random.nextDouble() * 10;

long waitTime = randWaitTime();

double expectedFare = fareRate * ((double) waitTime / 1000);

// Run test

Uber.setFareRate(fareRate);

uber = new Uber(randName(), randCarModel());

uber.pickupPassenger();

Thread.sleep(waitTime);

double actualFare = uber.setdownPassenger();

// Check result

assertEquals(expectedFare, actualFare, 0.2);

}

/*

* Test 6: A complex job.

* [This test obliges you to also consider a surge multiplier]

*/

@Test

public void testComplexJob() throws InterruptedException {

double surgeMultiplier = 1 + (random.nextDouble() * 10);

double fareRate = random.nextDouble() * 10;

long waitTime = randWaitTime();

double expectedFare = fareRate * ((double) waitTime / 1000) * surgeMultiplier;

// Run test

Uber.setFareRate(fareRate);

uber = new Uber(randName(), randCarModel());

uber.setSurgeMultiplier(surgeMultiplier);

uber.pickupPassenger();

Thread.sleep(waitTime);

double actualFare = uber.setdownPassenger();

// Check result

assertEquals(expectedFare, actualFare, 0.2);

}

}

Runs: 1/1 Errors: Failures: 0 unitUber.UberTests [Runner: JUnit 4] (0.000 s) initializationError (0.000 s)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!