Fix tests

This commit is contained in:
cugurlu
2019-03-18 12:15:00 +01:00
parent a2e432d453
commit f76a6827f7
4 changed files with 48 additions and 27 deletions

View File

@@ -1,17 +1,17 @@
package greenify.server.data.model; package greenify.server.data.model;
import lombok.Data; import lombok.Data;
import javax.persistence.*;
import java.util.Objects; import java.util.Objects;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table;
@Entity @Entity
@Data @Data
@Table(name = "activities") @Table(name = "activities")
public class Activity { public class Activity {
@Id @Id
@@ -88,7 +88,7 @@ public class Activity {
* sets the description. * sets the description.
* @param description the description to be set. * @param description the description to be set.
*/ */
public void setDescription(String description){ public void setDescription(String description) {
this.description = description; this.description = description;
} }
@@ -107,23 +107,19 @@ public class Activity {
*/ */
@Override @Override
public String toString() { public String toString() {
return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")"; return "Activity(id=" + this.id + ", name=" + this.name + ", description="
+ this.description + ", score=" + this.score + ")";
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if(other instanceof Activity){ if (other instanceof Activity) {
Activity that = (Activity)other; Activity that = (Activity)other;
if(that.id != this.id) if (that.id == this.id && that.name.equals(this.name)
return false; && that.description.equals(this.description) && that.score == this.score) {
if(!that.name.equals(this.name))
return false;
if(!that.description.equals(this.description))
return false;
if(that.score != this.score)
return false;
return true; return true;
} }
}
return false; return false;
} }

View File

@@ -1,8 +1,8 @@
package greenify.server.data.model; package greenify.server.data.model;
import lombok.Data; import lombok.Data;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import java.util.Objects;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
@@ -10,7 +10,6 @@ import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@EnableAutoConfiguration
@Entity @Entity
@Data @Data
@Table(name = "users") @Table(name = "users")
@@ -92,20 +91,31 @@ public class User {
this.veganMeal = veganMeal; this.veganMeal = veganMeal;
} }
/**
* Returns a human readable object. It's in JSON.
* @return the JSON form of the object.
*/
@Override
public String toString() {
return "User(id=" + this.id + ", name=" + this.name + ", password="
+ this.password + ", veganMeal=" + this.veganMeal + ")";
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if(other instanceof User){ if (other instanceof User) {
User that = (User)other; User that = (User)other;
if(that.id != this.id) if (that.id == this.id && that.name.equals(this.name)
return false; && that.password.equals(this.password) && that.veganMeal == this.veganMeal) {
if(!that.name.equals(this.name))
return false;
if(!that.password.equals(this.password))
return false;
if(that.veganMeal != this.veganMeal)
return false;
return true; return true;
} }
}
return false; return false;
} }
@Override
public int hashCode() {
return Objects.hash(id, name, password, veganMeal);
}
} }

View File

@@ -1,6 +1,7 @@
package greenify.server.data.model; package greenify.server.data.model;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test; import org.junit.Test;
@@ -39,6 +40,13 @@ public class ActivityTest {
assertTrue(first.equals(second)); assertTrue(first.equals(second));
} }
@Test
public void notEqualsTest() {
Activity first = new Activity(1, "Solar panels", "Installed", 10000);
Activity second = new Activity(2, "Solar panels", "Installed", 10000);
assertFalse(first.equals(second));
}
@Test @Test
public void hashCodeTest() { public void hashCodeTest() {
Activity first = new Activity(1, "Solar panels", "Installed", 10000); Activity first = new Activity(1, "Solar panels", "Installed", 10000);

View File

@@ -39,6 +39,13 @@ public class UserTest {
assertTrue(first.equals(second)); assertTrue(first.equals(second));
} }
@Test
public void notEqualsTest() {
User first = new User(1L, "greenify", "password", 3);
User second = new User(1L, "greenify", "password", 7);
assertFalse(first.equals(second));
}
@Test @Test
public void instanceOfTest() { public void instanceOfTest() {
User first = new User(); User first = new User();
@@ -50,8 +57,8 @@ public class UserTest {
public void hashCodeTest() { public void hashCodeTest() {
User first = new User(1L, "greenify", "password", 3); User first = new User(1L, "greenify", "password", 3);
User second = new User(1L, "greenify", "password", 3); User second = new User(1L, "greenify", "password", 3);
assertTrue(first.equals(second) && second.equals(first));
assertEquals(first, second); assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode());
} }
} }