2014-03-13 49 views
-2

我无法弄清楚如何从测试文件中编写这段代码。我该如何覆盖字符串

public class MineNeighbourTest 
{ 
    private MineWorld world; 
    private Position position; 
    private MineNeighbour neighbour; 
    private MineNeighbour bigNeighbour; 

    /** 
    * Default constructor for the test class. 
    */ 
    public MineNeighbourTest() 
    { 

    } 

    /** 
    * Sets up the test fixture. 
    * 
    * Called before every test case method. 
    */ 
    @Before 
    public void setUp() 
    { 
     world = new MineWorld(6, 6); 
     position = new Position(world, 3, 1); 
     // create a field that has 4 mines in the neighbourhood 
     neighbour = new MineNeighbour(position, 4); 
     // create a field that has 8 mines in the neighbourhood 
     bigNeighbour = new MineNeighbour(position, 8); 
    } 

    /** 
    * Tears down the test fixture. 
    * 
    * Called after every test case method. 
    */ 
    @After 
    public void tearDown() 
    { 
     world  = null; 
     position  = null;  
     neighbour = null; 
     bigNeighbour = null; 
    } 


    /** 
    * Test of getNeighbouringMines method, of class MineNeighbour. 
    */ 
    @Test 
    public void testGetNeighbouringMines() 
    { 
     assertEquals(4, neighbour.getNumNeighbouringMines()); 
     assertEquals(8, bigNeighbour.getNumNeighbouringMines()); 
    } 


    /** 
    * Test of getStringRepresentation method, of class MineNeighbour. 
    */ 
    @Test  
    public void testGetStringRepresentation() 
    { 
     assertEquals("4", neighbour.getStringRepresentation()); 
     assertEquals("8", bigNeighbour.getStringRepresentation()); 
    }  

} 

这里是我写的:

public class MineNeighbour extends Occupant 
{ 
    private Position pos; 

    public MineNeighbour(Position neighbour, Position bigNeighbour) { 
     super(neighbour); 
    } 
    public Position getNeighbouringMines() { 
     return neighbour; 
     return bigNeighbour; 
    } 
    @Override 
    public String getStringRepresenation() { 
     return getNeighbouringMines(); 
    } 
} 

我需要覆盖 8的neighbourbigNeighbour附加字段,我不太明白的给我的信息MineNeighbourTest

我的乘员类:

public class Occupant 
{ 

    private Position pos; 

    public Occupant(Position iniPos) { 
     this.pos = iniPos; 
    } 

    public Position getPosition() { 
     return pos; 
    } 

    public void setPosition(Position newPos) { 
     this.pos = newPos; 
    } 

    public String getStringRepresentation() { 
     return " "; 
    } 

}

+0

这是否代码甚至编译?你有一个有两个回报的方法,这根本不可能。你能澄清你的问题吗?这令人困惑,至少对我而言。 –

+0

我不明白这一点,你的'getNeighborouringMines()'方法有两个返回语句一个接一个? – Tristan

+0

不,我不知道我需要如何声明我的方法并重写getStringRepresenation。我认为测试文件要求我做的是创建两个附加字段neighbor和bigNeighbour。 – user3413917

回答

1
public class MineNeighbour extends Occupant 
{ 
    private int mines; 

    public MineNeighbour(Position neighbour, int mines) { 
    super(neighbour); 
    this.mines = mines; 
    } 

    public Position getNeighbouringMines() { 
    return mines; 
    } 

    @Override 
    public String getStringRepresenation() { 
    return Integer.toString(mines); 
    } 
}