【问题标题】:Simple Junit testing with OOP java使用 OOP java 进行简单的 Junit 测试
【发布时间】:2015-09-04 19:26:35
【问题描述】:

下面是一个用小时和分钟表示时间的类(不表示秒)。

public class ClassTime {

    public static int hour;
    public static int minute;
    public static String amPm;

    //Initializes the object using specified parameter values.
    public ClassTime(int hour, int minute, String amPm){

        //hour value between 1 and 12, both inclusive.
        boolean validHour = false;
        if (hour >= 1 && hour <= 12){
            validHour = true;
            this.hour = hour;
        } else {
            throw new IllegalArgumentException("Invalid hour value");
        }

        //minute value between 0 and 59, both inclusive.
        boolean validMinute = false;
        if (minute >= 0 && minute <= 59){
            validMinute = true;
            this.minute = minute;
        } else {
            throw new IllegalArgumentException("Invalid minutes value");
        }

        //amPm is either "am" or "pm".
        if (amPm.equalsIgnoreCase("am")){
            this.amPm = amPm;
        } else if (amPm.equalsIgnoreCase("pm")){
            this.amPm = amPm;
        } else {
            throw new IllegalArgumentException("Invalid am/pm value");
        }

    }

    /*
     * Returns a string using the format "hour:minutes am" or "hour:minutes pm".
     * A single space is used in between minutes and am/pm. The minutes always
     * appear with two digits (e.g., 0 minutes will be "00").
     */
    public String toString(){
        String toBeReturned = "hour:" + String.format("%02d", this.minute) + 
                " " + amPm;
        return toBeReturned;
    }

    /*
     * Compares two time objects. Two time objects are considered equal if
     * they represent the same time.
     */
    public boolean equals(ClassTime obj){

        boolean equal = false;

        if (obj.minute == this.minute && obj.hour == this.hour &&
                obj.amPm.equalsIgnoreCase(this.amPm)){
            equal = true;
        } 

        return equal;
    }

    /*
     * Compares two time objects. Returns -1 if the current object is a time
     * that precedes the time parameter, 0 if the current object and the time
     * parameter represent the same time, and 1 if the current object represents 
     * a time that is after the time parameter.
     */
    public int compareTo(ClassTime obj){

        int returnNum = 2;

        if(this.amPm.equalsIgnoreCase("am") && obj.amPm.equalsIgnoreCase("pm")){
            returnNum = -1;
        } else if (this.amPm.equalsIgnoreCase("pm") && 
                obj.amPm.equalsIgnoreCase("am")){
            returnNum = 1;
        } else if (this.amPm.equalsIgnoreCase(obj.amPm)){
            if (this.hour < obj.hour){
                returnNum = -1;
            } else if (this.hour > obj.hour){
                returnNum = 1;
            } else if (this.hour == obj.hour){
                if (this.minute < obj.minute){
                    returnNum = -1;
                } else if (this.minute > obj.minute){
                    returnNum = 1;
                } else if (this.minute == obj.minute){
                    returnNum = 0;
                }
            }
        }
        return returnNum;
    }

    /*
     * Returns a new time object corresponding to the time we will have after 
     * increasing the time parameter by the specified number of minutes.
     */
    public static ClassTime increaseByMinutes(ClassTime obj, int minutes){

        //create variables that monitor changes in minutes, hours, and amPm.
        int minValue = obj.minute + minutes;
        int hourValue = obj.hour;
        String amPmValue = obj.amPm;

        /*
         * increments hour and minutes if the total minutes is below two hours, 
         * but greater tan or equal to 1 hour.
         */
        if(minValue > 59 && minValue < 120){
            minValue = minValue % 60; 
            hourValue = hourValue + 1;
            if (hourValue > 12){
                hourValue = hourValue % 12;
            } else if(hourValue == 12 && amPmValue.equalsIgnoreCase("am")){
                amPmValue = "pm";
            } else if (hourValue == 12 && amPmValue.equalsIgnoreCase("pm")){
                amPmValue = "am";
            }

            /*
             * Increment for when the total amount of minutes is greater
             * or equal to 2 hours.
             */
        } else if (minValue > 119){
            for(int i = 0; i <= (minValue/60); i++){
                hourValue++;
                if(hourValue > 12){
                    hourValue = hourValue % 12;
                } else if (hourValue == 12 && amPmValue.equalsIgnoreCase("am")){
                    amPmValue = "pm";
                } else if (hourValue == 12 && amPmValue.equalsIgnoreCase("pm")){
                    amPmValue = "am";
                }
            }
        }

        /*
         * Create a new ClassTime object with the found values of minutes, hours,
         * and amPm Value. This is what will be returned.
         */
        ClassTime newObject = new ClassTime(hour, minute, amPm);

        newObject.minute = minValue;
        newObject.hour = hourValue;
        newObject.amPm = amPmValue;

        return newObject;

    }


    public static void main(String[] args) {

    }

}

我不知道如何测试 JUnit 测试用例中的构造函数或方法。到目前为止,我唯一能想到的构造函数是:

import static org.junit.Assert.*;

import org.junit.Test;

public class JunitTests {

    @Test
    public void testClassTime() {
        ClassTime object1 = new ClassTime(3, 30, "pm");



    }

}

我将如何完成对构造函数的测试,也许只有 1 个方法。请不要全部做。

【问题讨论】:

  • 下一步是断言一些东西。阅读 JUnit 文档了解更多信息。

标签: java unit-testing oop testing junit


【解决方案1】:

我建议你阅读单元测试,你的问题有点太宽泛了。

基本上这个想法是给定某些情况,某件事发生时,那么某些条件应该为真。 (我个人将 cmets 添加到我的测试中,以帮助我以这些术语进行思考。)

这是一个抽象的例子:

  1. 给了面包和烤面包机
  2. 当我把面包放在烤面包机里,烤好后把它拿出来
  3. 那就烤好了

查找 JUnit 的断言库以帮助找到断言条件的好方法。在查找 JUnit 示例时,您通常会看到 assertTrue(...)assertNotNull(...) 之类的东西,这些是来自 Assert 类的静态方法,我提到这一点是因为您看起来像个新手,我不想让您感到困惑通过例子。通常所有这些方法在示例中都是静态导入的。

您目前在测试用例中拥有的是 givenwhen 可能正在执行toString() (String actual = object1.toString();)。您的 then 子句将断言返回的 String 等于它应该返回的值,这可能是 "3:30 PM" (assertEquals("3:30 PM", actual);)。我没有设计你的代码,所以我不知道这是否正确,你可能期望它返回“Hello,World!”无论如何,我会先运行这个 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 2017-05-12
    相关资源
    最近更新 更多