2016-04-18 12 views
-2

Ich habe eine Klasse, die verwendet wird, um Listen zu erstellen. Dies ist das Ergebnis des Tests:Wie weisen Sie einer Aufgabenaufgabe eine ID-Nummer zu?

----------------------------------------- 
TESTS - CONSTRUCTOR 1 
----------------------------------------- 
Construction todo ("description 1")... ➤   [⍡] - 1) description 1() **(expected result in the test)** 
id: 1 (System.out.println of the id in the test class) 
➤   [⍡] - 1) description 1 **(result)** 
OK **(the test is ok)** 
Construction todo ("")... OK 
Construction todo (null)... OK 

------------------------------------------ 
TESTS - CONSTRUCTOR 2 
------------------------------------------ 
Construction todo (23/08/2016, "description 2")... id : 2 **(here id is 2)** 
➤ 23/08/2016 [⍡] - 2) description 2 
OK 
Construction todo (null, "description 3")... id : 3 **(here id is 3)** 
OK 
Construction todo (01/01/2016, "xxx")... OK 
Construction todo (10/09/2016, "")... OK 
Construction todo (10/09/2016, null)... OK 

------------------------------------------ 
TESTS - CONSTRUCTOR 3 
------------------------------------------ 
Construction todo (15/07/2016, "description 4", Todo.HIGH_IMPORTANCE)... **expected** : ➤ 15/07/2016 [⍥] - 4) description 4 
**but i got** : ➤ 15/07/2016 [⍥] - 5) description 4 **(on this line the id is 5 instead of 4)** 
id: 5 **(System.out.println of the id in test class)** 
ERROR **(thats why i have here error)** 
Construction todo (null, "description 5", Todo.MEDIUM_IMPORTANCE... id : 6 **(System.out.println in the test class)** 
ERROR 
Construction todo (01/01/2016, "xxx", Todo.HIGH_IMPORTANCE)... OK 
Construction todo (10/09/2016, "", Todo.HIGH_IMPORTANCE)... OK 
Construction todo (10/09/2016, null, Todo.HIGH_IMPORTANCE)... OK 

------------------------ 
TESTS - GETTERS 
------------------------ 
GET ID... OK 
GET Date creation... OK 
GET Dead line... OK 
GET Description... OK 
GET Description... OK 
IS COMPLETED... OK 

------------------------ 
TESTS - SETTERS 
------------------------ 
SET Dead line... OK 
SET Completed... OK 
SET Level importance... OK 
SET Level importance invalid... OK 

Checking if the level of importance has not changed... **we expect** : ➤   [⍨] - 5) description 5 
**printed result**: ➤   [⍨] - 6) description 5 **(here id is not good, need to be 5)** 
ERROR 
SET Description... OK 
SET Description empty... OK 
Checking that the description has not been changed... **we expect** : ➤ 15/07/2016 [⍥] - 4) description 4 
**printed result**: ➤ 15/07/2016 [⍥] - 5) description 4 
ERROR **(THAT'S WHY ERROR)** 
SET Description null... OK 
Checking that the description has not been changed... **we expect** : ➤ 15/07/2016 [⍥] - 4) description 4 
**printed result**: ➤ 15/07/2016 [⍥] - 5) description 4 
ERROR 

---------------------------------------------------- 
TESTS - METHOD OBTENIR GETING LAST ID ASSUMED 
---------------------------------------------------- 
Last id assumed = 5... **id printed is 7**... ERROR 

--------------------------------------- 
TESTS - METHOD HAS PRIORITY OVER 
--------------------------------------- 
Test 1... OK 
Test 2... OK 
Test 3... OK 
Test 4... OK 
Test 5... OK 
Test 6... OK 
Test 7... OK 
Test 8... OK 
Test 9... OK 
Test 10... OK 

Und das ist meine Klasse:

public class Todo { 


public static final int HIGH_IMPORTANCE = 1; 
public static final int MEDIUM_IMPORTANCE = 2; 
public static final int LOW_IMPORTANCE = 3; 


private static int lastIdAssumed = 0; 


private Date dateCreation; 
private Date deadline; 
private String description; 
private int levelImportance; 
private boolean completed; 
private int id; 


public Todo(String description) throws TodoInvalideException{ 
    if (description != null && description.length() != 0) { 
     this.description = description; 
    } else { 
     throw new TodoInvalideException(); 
    } 

    this.dateCreation = Date.todayDate(); 
    this.deadline = null; 
    this.levelImportance = LOW_IMPORTANCE; 
    this.completed = false; 
    lastIdAssumed = lastIdAssumed + 1; 
    this.id = lastIdAssumed; 

} 

public Todo(Date deadline, String description)throws TodoInvalideException, DateInvalideException { 
    this(description); 
    if (deadline != null && dateCreation.estPlusRecente(deadline)){ 
     throw new TodoInvalideException(); 
    } 

    this.deadline = deadline; 

} 

public Todo(Date deadline, String description, int levelImportance)throws TodoInvalideException, DateInvalideException{ 
    this(deadline, description); 


    if (levelImportance == HIGH_IMPORTANCE || levelImportance == MEDIUM_IMPORTANCE 
    || levelImportance == LOW_IMPORTANCE) { 
     this.levelImportance = levelImportance; 
    } else { 
     throw new TodoInvalideException(); 
    } 

} 


//GETTERS (6) 
public Date getDateCreation(){ 
    return dateCreation; 
} 

public Date getDeadline(){ 
    return deadline; 
} 

public String getDescription(){ 
    return description; 
} 

public boolean isCompleted(){ 
    return completed; 
} 

public int getLevelImportance(){ 
    return levelImportance; 
} 

public int getId(){ 
    return id; 
} 

//SETTERS (4) 

public void setDeadline(Date deadline){ 
    this.deadline = deadline; 
} 

public void setDescription(String description) throws TodoInvalideException { 

    if (description != null && description.length() != 0) { 
     this.description = description; 
    } else { 
     throw new TodoInvalideException(); 
    } 

} 

public void setCompleted(boolean completed){ 
    this.completed = completed; 
} 

public void setLevelImportance(int levelImportance) throws TodoInvalideException { 

    if (levelImportance == HIGH_IMPORTANCE || levelImportance == MEDIUM_IMPORTANCE 
    || levelImportance == LOW_IMPORTANCE) { 
     this.levelImportance = levelImportance; 
    } else { 
     throw new TodoInvalideException(); 
    } 

} 


public boolean hasPriorityOver (Todo anotherTodo) throws TodoInvalideException, DateInvalideException { 
    boolean priorityTodo = false; 

    if (this.deadline == null && anotherTodo.deadline == null) { 
     if (this.levelImportance < anotherTodo.levelImportance) { 
      priorityTodo = true; 
     } 

    } else if (this.deadline == null && anotherTodo.deadline != null) { 
     priorityTodo = false; 

    } else if (this.deadline != null && anotherTodo.deadline == null) { 
     priorityTodo = true; 
    } 

    if(this.deadline != null && anotherTodo.deadline != null) { 

     if (anotherTodo.deadline.isMoreRecent(this.deadline)) { 
      priorityTodo = true; 

     }else{ 
      priorityTodo = false; 
     } 

     if (this.deadline.isEquals(anotherTodo.deadline)) { 
      if (this.levelImportance < anotherTodo.levelImportance) { 
       priorityTodo = true; 
      }else{ 
       priorityTodo = false; 
      } 

     } 
    } 

    return priorityTodo; 
} 


public String toString() { 
    String [] tab = {"", "[\u2365]", "[\u2368]", "[\u2361]"}; 
    String s = ""; 

    if (completed) { 
     s = s + "\u2714"; 
    } else { 
     s = s + "\u27A4"; 
    } 
    if (deadline == null) { 
     s = s + "   "; 
    } else { 
     s = s + " " + deadline; 
    } 

    s = s + " " + tab[levelImportance]; 
    s = s + " - " + id + ") " + description; 

    return s; 
} 


public static int getingLastIdAssumed() { 


    return lastIdAssumed; 
} 

} 

Die Methoden isMoreRecent(), isEquals() und Date.todayDate() in dem anderen Klasse mit dem Namen Datum sind . Vielen Dank!

+5

wo die Frage ist? –

+0

in meinem Test bekomme ich falsche ID –

+0

Mehr als wahrscheinlich, gibt es eine unerwartete ToDo-Klasse irgendwo, vielleicht im Test-Treiber erstellt. Die Tatsache, dass "id" von der "lastIdAssumed" (die inkrementiert wird) zugewiesen wird, ist suggestiv. Ich gehe davon aus, dass das Problem das ist, was in der Ausgabe gezeigt wird, aber wie @RamanShrivastava vorgeschlagen hat, wäre es einfacher, wenn die Frage direkter formuliert wäre. – KevinO

Antwort

1

Siehe meine Erklärung nach den Pfeilen

Construction todo (01/01/2016, "xxx")... OK ---> invokes Todo(String description), your static variable increased to 4 

„xxx“ nicht null ist und die Länge ist größer als 0, daher ist es in Ordnung ist Todo (String Beschreibung) aufzurufen. Leider sind die folgenden beiden Instanziierungen aufgrund der Beschreibung fehlgeschlagen: entweder leere Zeichenfolge oder Nullwert, statische Variable lastIdAssumed änderte ihren Wert nicht.

Construction todo (10/09/2016, "")... OK ---> throws TodoInvalideException, static variable didn't increase 
Construction todo (10/09/2016, null)... OK ---> throws TodoInvalideException, static variable didn't increase 

Wenn Sie also unter Konstruktor aufrufen, Ihre statische Variable 5.

Construction todo (15/07/2016, "description 4", Todo.HIGH_IMPORTANCE) 
+0

und wie das zu beheben? –

+0

es sollte 5 sein. Sie können Ihre Testfälle Bestellungen – haifzhan

+0

Entschuldigung, aber ich verstehe nicht ganz, was zu ändern ist. :( –

Verwandte Themen