Closed RalfBarkow closed 5 months ago
EntryID
ClassLet's create a set of TestNG test cases to verify the behavior of the EntryID
class. These tests will cover various scenarios, including construction, method functionality, and equality checks.
EntryIDTest
package de.danielluedecke.zettelkasten;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class EntryIDTest {
@Test
public void testEntryIDIntConstructor() {
EntryID entryID = new EntryID(123);
assertEquals(entryID.asInt(), 123, "The entry number should be 123");
}
@Test
public void testEntryIDStringConstructor() {
EntryID entryID = new EntryID("456");
assertEquals(entryID.asInt(), 456, "The entry number should be 456");
}
@Test(expectedExceptions = NumberFormatException.class)
public void testEntryIDStringConstructorWithInvalidNumber() {
new EntryID("invalid");
}
@Test
public void testAsInt() {
EntryID entryID = new EntryID(789);
assertEquals(entryID.asInt(), 789, "The entry number should be 789");
}
@Test
public void testAsString() {
EntryID entryID = new EntryID(1011);
assertEquals(entryID.asString(), "1011", "The entry number string should be '1011'");
}
@Test
public void testEqualsSameObject() {
EntryID entryID = new EntryID(1213);
assertTrue(entryID.equals(entryID), "An EntryID should be equal to itself");
}
@Test
public void testEqualsNull() {
EntryID entryID = new EntryID(1415);
assertFalse(entryID.equals(null), "An EntryID should not be equal to null");
}
@Test
public void testEqualsDifferentClass() {
EntryID entryID = new EntryID(1617);
String notAnEntryID = "notAnEntryID";
assertFalse(entryID.equals(notAnEntryID), "An EntryID should not be equal to an object of a different class");
}
@Test
public void testEqualsSameEntryNumber() {
EntryID entryID1 = new EntryID(1819);
EntryID entryID2 = new EntryID(1819);
assertTrue(entryID1.equals(entryID2), "EntryIDs with the same entry number should be equal");
}
@Test
public void testEqualsDifferentEntryNumber() {
EntryID entryID1 = new EntryID(2021);
EntryID entryID2 = new EntryID(2223);
assertFalse(entryID1.equals(entryID2), "EntryIDs with different entry numbers should not be equal");
}
@Test
public void testHashCode() {
EntryID entryID1 = new EntryID(2425);
EntryID entryID2 = new EntryID(2425);
assertEquals(entryID1.hashCode(), entryID2.hashCode(), "Equal EntryIDs should have the same hash code");
}
@Test
public void testHashCodeDifferent() {
EntryID entryID1 = new EntryID(2627);
EntryID entryID2 = new EntryID(2829);
assertNotEquals(entryID1.hashCode(), entryID2.hashCode(), "Different EntryIDs should have different hash codes");
}
}
Constructor Tests:
testEntryIDIntConstructor
: Tests the integer constructor.testEntryIDStringConstructor
: Tests the string constructor with a valid number.testEntryIDStringConstructorWithInvalidNumber
: Tests the string constructor with an invalid number, expecting a NumberFormatException
.Method Tests:
testAsInt
: Verifies the asInt
method.testAsString
: Verifies the asString
method.Equality Tests:
testEqualsSameObject
: Checks if an object is equal to itself.testEqualsNull
: Checks if an object is not equal to null
.testEqualsDifferentClass
: Checks if an object is not equal to an object of a different class.testEqualsSameEntryNumber
: Checks if two objects with the same entry number are equal.testEqualsDifferentEntryNumber
: Checks if two objects with different entry numbers are not equal.Hash Code Tests:
testHashCode
: Verifies that equal objects have the same hash code.testHashCodeDifferent
: Verifies that different objects have different hash codes.
The
EntryID
class requires improvements to its documentation and equality logic. The current implementation uses assertions in theequals
method, which is not appropriate for production code. Additionally, the class and its methods lack proper Javadoc comments.Tasks:
Add Javadoc Comments:
EntryID
class.Refactor
equals
Method:equals
method with proper null checks and type checks usinginstanceof
.Add TestNG Tests:
asInt
andasString
methods.Proposed Solution: Refactor the
EntryID
class as follows:Add the corresponding TestNG test cases to
EntryIDTest
class.