有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在MVC中为模型创建junit测试

我第一次使用MVC框架,我想了解一下jUnit测试,我已经将我的主项目添加到了一个新的包中,我正在尝试将一个新的测试包实现到我的jUnit测试所在的项目中

我的模型类是最多类的类,所以我认为最好先做一个junit测试。那么,我该如何进行简单的测试和构建呢

模范班

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();
    }

}

测试类,我想返回3,因为有3个门

public class GateTest {



            GateInfoDatabase model = new GateInfoDatabase();

            @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);
            }


    }

共 (1) 个答案

  1. # 1 楼答案

    单元测试并不是一个简单的话题,你们应该找到一些教程或者阅读其他的书籍来理解如何正确地进行单元测试

    解释单元测试的最简单方法是,它用于测试其他系统中的类和方法,以查看它们是否正常工作

    在创建测试时,如果您想遵循约定,您应该将test class命名为您正在测试的类,并在末尾添加测试,因此类Example应该有名为ExampleTest的测试类

    现在在ExampleTest中,你可以通过测试方法测试行为,一种方法可以应该进行更多测试。说到命名test methods有很多不同的方法,下面是几种流行的方法:

    • [tested behavior]_[tested input or state]_[expected result]-我更喜欢这个项目
    • [behavior being tested]-当测试简单的东西时
    • test[behavior being tested]-我不喜欢测试单词,因为它有点明显

    现在,您测试的内容可能会有所不同,每个用例的测试内容也会有所不同,但应该检查的总是一些应该始终通过(以确保方法首先工作)的标准案例和边缘案例,例如当采用null0Integer.[MAX/MIN]_VALUE、空字符串/集合/数组时方法的行为,负指数和其他“临界”值

    例如,让我们看看getGate,如果您编写了上面提到的几个测试用例,您将看到它失败了,-1、3或整数。MAX_VALUE是该方法可以接受的所有有效值,但对于其中任何一个,它都将失败,所以现在您可以选择在other中改进方法来处理这些值,添加少量if来检查值,如果它有效,您可以返回Gate,否则您应该抛出异常

    是的,也许你知道你只有3个门,不会尝试用更大的数字来称呼它,但如果你选择在6个月内更改代码呢?那你还记得吗

    所以,一定要始终处理大多数明显的“失败案例”,当你面对新的案例时,也一定要解决它们。这就是我们进行单元测试的主要原因,换句话说,就是为了确保单个代码单元能够正常工作,因为如果这些小代码块没有完成它们应该做的事情,那么整个程序如何能够正常工作