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;
import lombok.Data;
import javax.persistence.*;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Data
@Table(name = "activities")
public class Activity {
@Id
@@ -88,7 +88,7 @@ public class Activity {
* sets the description.
* @param description the description to be set.
*/
public void setDescription(String description){
public void setDescription(String description) {
this.description = description;
}
@@ -107,22 +107,18 @@ public class Activity {
*/
@Override
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
public boolean equals(Object other) {
if(other instanceof Activity){
if (other instanceof Activity) {
Activity that = (Activity)other;
if(that.id != this.id)
return false;
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;
if (that.id == this.id && that.name.equals(this.name)
&& that.description.equals(this.description) && that.score == this.score) {
return true;
}
}
return false;
}

View File

@@ -1,8 +1,8 @@
package greenify.server.data.model;
import lombok.Data;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@@ -10,7 +10,6 @@ import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@EnableAutoConfiguration
@Entity
@Data
@Table(name = "users")
@@ -92,20 +91,31 @@ public class User {
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
public boolean equals(Object other) {
if(other instanceof User){
if (other instanceof User) {
User that = (User)other;
if(that.id != this.id)
return false;
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;
if (that.id == this.id && that.name.equals(this.name)
&& that.password.equals(this.password) && that.veganMeal == this.veganMeal) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(id, name, password, veganMeal);
}
}

View File

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

View File

@@ -39,6 +39,13 @@ public class UserTest {
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
public void instanceOfTest() {
User first = new User();
@@ -50,8 +57,8 @@ public class UserTest {
public void hashCodeTest() {
User first = 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.hashCode(), second.hashCode());
}
}