如何使我的jUnit测试更有用

时间:2018-03-25 21:40:33

标签: java model-view-controller junit

我有以下模型类(我正在使用MVC框架),下面我已经包含了我的测试类。我想创建一个实用的测试类,虽然测试我的主要方法测试类并不觉得有用(可能是因为我在模型类之后创建了它)。任何人都可以对我可以定位的模型类的其他部分提出建议,或者如何以更有用的方式更改我的测试类。

public class GateInfoDatabase extends Observable {

    /**
     *  A constant: the number of aircraft gates at the airport.
     */
    public int maxGate = 3;
    private Gate[] gates = new Gate[maxGate];

    public GateInfoDatabase(){
        Gate gate0 = new Gate(0);
        Gate gate1 = new Gate(1);
        Gate gate2 = new Gate(2);

        gates[0] = gate0;
        gates[1] = gate1;
        gates[2] = gate2;
    }

    /**
     * Obtain and return the status of the given gate identified by the gateNumber parameter.
     * @return 
     */
    public Gate getGate(int gateNumber){
        Gate result = gates[gateNumber];
        return result;
    }
    /**
     * Obtain and return the status of the given gate identified by the gateNumber parameter.
     * @return 
     */
    public Gate getGateByMCode(int mCode){
        Gate result = null;
        for(int i = 0; i < gates.length; i++) {
            if(mCode == gates[i].getmCode()) {
                result = gates[i];
                System.out.print(result);
            }
        }
        return result;
    }
    /**
     * Obtain and return the status of the given gate identified by the gateNumber parameter.
     * @return 
     */
    public int getStatus(int gateNumber){

        return gates[gateNumber].getStatus();
    }
    /**
     * Obtain and return the status of the given gate identified by the gateNumber parameter.
     * @return 
     */
    public int getStatusByMCode(int mCode){
        int nStatus = ManagementRecord.FREE;
        Gate result = getGateByMCode(mCode);
        if(result!=null) {
            nStatus = result.getStatus();
        }
        return nStatus;
    }
    /**
     * Returns an array containing the status of all gates.
     * For data collection by the GOC.
     */
    public int[] getStatuses(){

        int[] statuses = new int[maxGate];

        for(int i = 0; i < maxGate; i++){

            statuses[i] = gates[i].getStatus();
        }
        return statuses;
    }

    /**
     * Forward a status change request to the given gate identified by the gateNumber parameter. Called to allocate a free gate to the aircraft identified by mCode.
     */
    public void allocate(int gateNumber, int mCode){

        gates[gateNumber].allocate(mCode);
        setChanged();
        notifyObservers(); 

    }

    /**
     * Forward a status change request to the given gate identified by the gateNumber parameter. Called to indicate that the expected aircraft has arrived at the gate.
     */
    public void docked(int gateNumber){
        gates[gateNumber].docked();
        setChanged();
        notifyObservers(); 
    }

    /**
     * Forward a status change request to the given gate identified by the gateNumber parameter. Called to indicate that the aircraft has departed and that the gate is now free.
     */
    public void departed(int gateNumber){
        gates[gateNumber].departed();
        setChanged();
        notifyObservers(); 
    }

    public int getmCode(int gateNumber){

        return gates[gateNumber].getmCode();
    }

}

测试课

public class GateTest {


    GateInfoDatabase model = new GateInfoDatabase();
    ManagementRecord mrc= new ManagementRecord();

    @Before
    public void setUp() throws Exception {
        model = new GateInfoDatabase();
    }

    @After
    public void tearDown() throws Exception {
        model = null;
    }

    @Test
    public void testCreate() {
        assertNotNull("Model not created properly", model);
        assertEquals("Initial gate setup is wrong", model.maxGate, model.getStatuses().length);
    }

    @Test
    public void testGetGate() {
        Gate gate = model.getGate(0);
        assertNotNull("Gate should not be null", gate);
        assertEquals("Should be return right gate", new Gate(0).getStatus(), gate.getStatus());
        assertEquals("Should be return right gate", new Gate(0).getGateNumber(), gate.getmCode());
    }

    @Test
    public void testGetGateByMCode() {
        Gate gate = model.getGateByMCode(0);
        assertNotNull("Gate should not be null", gate);
        assertEquals("Should be return right gate", new Gate(0).getStatus(), gate.getStatus());
    }

    @Test
    public void testGetStatuses() {
        int[] statuses = model.getStatuses();
        assertNotNull("Gate should not be null", statuses);
        assertEquals("Should be equal number of gate", 3, statuses.length);
        assertEquals("Should be return right gate", 0, statuses[0]);
    }
    @Test
    public void testAllocate() {
        model.allocate(1,2);
        Gate gate = model.getGateByMCode(2);
        assertNotNull("Gate should not be null", gate);
        assertEquals("Should be equal number of gate", 1, gate.getGateNumber());
    }

    @Test
    public void testDocked() {
        model.docked(1);
        Gate gate = model.getGate(1);
        assertNotNull("Gate should not be null", gate);
        assertEquals("Should be right status after departure", 2, gate.getStatus());
    }

    @Test
    public void testDeparted() {
        model.docked(1);
        model.departed(1);
        Gate gate = model.getGate(1);
        assertNotNull("Gate should not be null", gate);
        assertEquals("Should be right status after departure", 0, gate.getStatus());
    }





}

0 个答案:

没有答案