0

我有一个如下所示的类 Horse 我必须使 Horse 对象不采用不是 Perso 对象的驱动程序,并且它继承了 Vehicle 类并且不能具有不是自身的引擎。在我的代码下发布的 Junit 测试中,我的 test4、test1 和 test2 失败了。我相信问题在于我如何专门处理设置引擎和设置驱动程序方法。在这种情况下,我需要设置方法吗?

马.java

public class Horse extends Vehicle implements Engine {
public Horse(Driver driver){
super(driver, null);
super.setEngine(this);
}
//@override
public void setEngine(Engine engine){
if(engine == null){
    super.setEngine(this);
}
else if(engine instanceof Horse){
    super.setEngine(engine);
}
else{
    throw new IllegalStateException();}
}

//@override
public void setDriver(Driver driver){
    if(driver instanceof Person){
        super.setDriver(this);
}
    else{
        throw new IllegalStateException();
    }
}
    //@override
    public int getForce(){
        return 746;
    }
    public boolean equals(Object other)
    {
    if(other instanceof Horse){
        if(((Horse)other).getForce() == this.getForce()){
            return true;
        }
        }
        return false;
    }

    public int compareTo(Engine o) {
        int force = this.getForce() - o.getForce();
        return force;

    }
}

车辆.java

abstract class Vehicle{
Engine engine;
Driver driver;
public Vehicle(Driver driver, Engine engine){
}
public Engine getEngine(){
    return engine;
}
public void setEngine(Engine aEngine){
    engine = aEngine;
}
public Driver getDriver(){
return driver;
}
public  void setDriver(Driver aDriver){
driver = aDriver;
}

public boolean equals(Object other)
{
if(other instanceof Vehicle){
if(((Vehicle)other).getEngine().equals(this.getEngine())){
    return true;
}
}
return false;
}
public int compareTo(Engine o) {
int force = o.getForce();
return force;

}
}

引擎.java

public interface Engine extends Comparable<Engine>{

int compareTo(Engine o);
public int getForce();

} 

junit 测试

import static org.junit.Assert.*;

import org.junit.Test;

public class HorseTest {

@Test
public void test0_HorseIsAVehicleAndAnEngine() {
    Horse horse = new Horse( new Person( 55 ) { } );
    assertTrue( "Incorrect result", horse instanceof Vehicle );
    assertTrue( "Incorrect result", horse instanceof Engine  );
}
@Test(expected=IllegalArgumentException.class)
public void test1_HorsesCannotBeRiddenByAnyDriverOtherThanPeople_NEW() {
    new Horse( new Driver() { } );
}
@Test(expected=IllegalArgumentException.class)
public void test2_HorsesCannotBeRiddenByAnyDriverOtherThanPeople_SET() {
    Horse horse = new Horse( new Person( 42 ) { } );
    horse.setDriver( new Driver() { } );
}
@Test
public void test3_AHorseIsItsOwnEngine() {
    Horse  horse  = new Horse( new Person( 99 ) { } );
    Engine engine = horse.getEngine();
    assertTrue( "Incorrect result", horse == engine );
}
@Test(expected=IllegalArgumentException.class)
public void test4_HorsesCannotHaveAnEngineThatIsNotItself() {
    Engine anEngine = new Engine() {
        @Override
        public int compareTo(Engine o) {
            return 0;
        }
        @Override
        public int getForce() {
            return 0;
        }
    };
    Horse horse = new Horse( new Person( 24 ) { } );
    horse.setEngine( anEngine );
}
@Test
public void test5_AHorseReceivingNullOrItselfAsEngineSetEngineToItself() {
    Engine engine;
    Horse  horse  = new Horse( new Person( 99 ) { } );

    horse.setEngine( null );
    engine = horse.getEngine();
    assertTrue( "Incorrect result", horse == engine );

    horse.setEngine( horse );
    engine = horse.getEngine();
    assertTrue( "Incorrect result", horse == engine );
}
@Test
public void test6_HorseHasEquals() {
    Engine  a, b;
    boolean actual;
    // equal to itself
    a      = new Horse( new Person( 21 ));
    actual = a.equals( a );
    assertTrue ( "Incorrect result", actual );
    // equal to another horse (regardless of driver)
    a      = new Horse( new Person( 42 ));
    b      = new Horse( new Person( 24 ));
    actual = a.equals( b );
    assertTrue ( "Incorrect result", actual );
    // not equal to null
    actual = a.equals( null );
    assertFalse( "Incorrect result", actual );
    // not equal to some other object
    actual = a.equals( "84" );
    assertFalse( "Incorrect result", actual );
    // not equal to some other object
    actual = a.equals(  48  );
    assertFalse( "Incorrect result", actual );
}
@Test
public void test7_HorseHasCompareTo() {
    Horse a, b;
    int   actual;
    // equal to itself
    a      = new Horse( new Person( 52 ));
    actual = a.compareTo( a );
    assertTrue( "Incorrect result", actual == 0 );
    // equal to another horse (regardless of driver)
    a      = new Horse( new Person( 9 ));
    b      = new Horse( new Person( 8 ));
    actual = a.compareTo( b );
    assertTrue( "Incorrect result", actual == 0 );
}
}
4

1 回答 1

2

test1中,您不会调用任何会检查驱动程序是否是人的东西。您调用Horse构造函数,并且使用super(driver,null)which 调用Vehicle构造函数,但在任何时候都没有调用setDriver方法,这是唯一会检查driver instanceof Person. (此外,driver刚刚被抛出。Horse构造函数和Vehicle构造函数都没有将它保存在任何地方。Vehicle构造函数可能应该有这个:

this.driver = driver;
this.engine = engine;

)

我没有尝试过您的代码,但test2失败的原因可能是setDriver实际抛出IllegalStateException但您告诉 JUnit 检查IllegalArgumentException. 同样的事情test4。先解决这些问题,看看是否能解决问题。

于 2013-10-02T19:35:32.180 回答