Skip to content
Snippets Groups Projects
Commit e12ed2b0 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

test for equals and hash code in group entity

parent fd551f52
No related branches found
No related tags found
No related merge requests found
......@@ -82,4 +82,29 @@ public class Group {
@JsonIgnoreProperties({"groups"})
private List<Achievement> achievements;
/**
* Checks if two objects are equal
* @param o the object to compare to
* @return true if the objects are equal, false otherwise
*/
@Override
public boolean equals(Object o){
if(o == this){
return true;
}
if(!(o instanceof Group group)){
return false;
}
return group.getGroupId() == this.getGroupId();
}
/**
* Gets the hashcode of the object
* @return the hashcode of the object
*/
@Override
public int hashCode(){
return Long.hashCode(this.getGroupId());
}
}
......@@ -282,7 +282,7 @@ public class GroupControllerTest {
assertSame(HttpStatus.FORBIDDEN, groupResponseEntity.getStatusCode());
assertNull(groupResponseEntity.getBody());
}
}
}
\ No newline at end of file
package ntnu.idatt2016.v233.SmartMat.entity.group;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GroupTest {
@Test
void testEquals() {
Group group = Group.builder()
.groupName("Test Group")
.groupId(1L)
.build();
Group group2 = Group.builder()
.groupId(2L)
.groupName("Test Group2")
.build();
assertEquals(group, group);
assertNotEquals(group, group2);
assertNotEquals("hello", group);
}
@Test
void testHashCode() {
Group group = Group.builder()
.groupName("Test Group")
.groupId(1L)
.build();
Group group2 = Group.builder()
.groupId(2L)
.groupName("Test Group2")
.build();
assertEquals(group.hashCode(), group.hashCode());
assertNotEquals(group.hashCode(), group2.hashCode());
}
@Test
void testToString() {
Group group = Group.builder()
.groupName("Test Group")
.groupId(1L)
.build();
assertEquals("Group(groupId=1, level=0, points=0, groupName=Test Group, linkCode=null, open=null, shoppingList=null, user=null, fridge=null, achievements=null)",
group.toString());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment