Bicycle.java
/*
* A sample testable class
* http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html
*
*/
package vehicles;
public class Bicycle {
// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void setSpeed(int newValue) {
speed = newValue;
}
public int getGear() {
return gear;
}
public int getCadence() {
return cadence;
}
public int getSpeed() {
return speed;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
Car.java
package vehicles;
public class Car {
public int cadence;
public int gear;
public int speed;
public Car(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void setSpeed(int newValue) {
speed = newValue;
}
public int getGear() {
return gear;
}
public int getCadence() {
return cadence;
}
public int getSpeed() {
return speed;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
BicycleTest.java
package tests;
import vehicles.*;
import org.junit.* ;
import static org.junit.Assert.* ;
public class BicycleTest {
@Test
public void test_applyBrake() {
System.out.println("Test applyBrake method ...");
Bicycle Bike = new Bicycle(10, 20, 1);
assertEquals(Bike.getGear(), 1);
assertEquals(Bike.getSpeed(), 20);
assertEquals(Bike.getCadence(), 10);
// testing setter
Bike.setGear(2);
Bike.setSpeed(30);
Bike.setCadence(5);
assertEquals(Bike.getGear(), 2);
assertEquals(Bike.getSpeed(), 30);
assertEquals(Bike.getCadence(), 5);
Bike.applyBrake(10);
assertEquals(Bike.getSpeed(), 20);
}
@Test
public void test_speedUp() {
System.out.println("Test speedUp method ...") ;
Bicycle Bike = new Bicycle(10, 20, 1);
assertEquals(Bike.getGear(), 1);
assertEquals(Bike.getSpeed(), 20);
assertEquals(Bike.getCadence(), 10);
// testing setter
Bike.setGear(2);
Bike.setSpeed(30);
Bike.setCadence(5);
assertEquals(Bike.getGear(), 2);
assertEquals(Bike.getSpeed(), 30);
assertEquals(Bike.getCadence(), 5);
Bike.speedUp(5);
assertEquals(Bike.getSpeed(), 35);
}
}
CarTest.java
package tests;
import vehicles.*;
import org.junit.*;
import static org.junit.Assert.*;
public class CarTest {
@Test
public void test_ugik() {
System.out.println("Test applyBrake method ...");
Car Bike = new Car(10, 20, 1);
assertEquals(Bike.getGear(), 1);
assertEquals(Bike.getSpeed(), 20);
assertEquals(Bike.getCadence(), 10);
// testing setter
Bike.setGear(2);
Bike.setSpeed(30);
Bike.setCadence(5);
assertEquals(Bike.getGear(), 2);
assertEquals(Bike.getSpeed(), 30);
assertEquals(Bike.getCadence(), 5);
Bike.applyBrake(10);
assertEquals(Bike.getSpeed(), 20);
}
@Test
public void test_115610092() {
System.out.println("Test speedUp method ...") ;
Car Bike = new Car(10, 20, 1);
assertEquals(Bike.getGear(), 1);
assertEquals(Bike.getSpeed(), 20);
assertEquals(Bike.getCadence(), 10);
// testing setter
Bike.setGear(2);
Bike.setSpeed(30);
Bike.setCadence(5);
assertEquals(Bike.getGear(), 2);
assertEquals(Bike.getSpeed(), 30);
assertEquals(Bike.getCadence(), 5);
Bike.speedUp(5);
assertEquals(Bike.getSpeed(), 35);
}
}
0 komentar:
Posting Komentar