Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/main/java/michaelmoss/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package michaelmoss;

public class Cat {

// 5 private field attributes/modifiers
private String name;
private int age;
private String breed;
private String color;
private double weight;


// constructor; taking all fields as parameters
public Cat(String name, int age, String breed, String color, double weight) {

if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}

if (weight <= 0) {
throw new IllegalArgumentException("Weight must be positive");

}


this.name = name;
this.age = age;
this.breed = breed;
this.color = color;
this.weight = weight;


}
// Getters
public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getBreed () {
return breed;
}

public String getColor () {
return color;
}

public double getWeight () {
return weight;
}

// Setters
public void setName (String name){
this.name = name;
}

public void setAge ( int age){
this.age = age;
}

public void setBreed (String breed){
this.breed = breed;
}

public void setColor (String color){
this.color = color;
}

public void setWeight ( double weight){
this.weight = weight;
}
// 2 random methods
public void meow() {
System.out.println(name + " says: Meow!");
}

public void eat2(double foodWeight) {
weight += foodWeight;
}
// IllegalArgumentException; validated weight
public void eat(double foodWeight) {


if (foodWeight < 0) {
throw new IllegalArgumentException("Food amount cannot be negative");
}

this.weight += foodWeight;
}

}









125 changes: 125 additions & 0 deletions src/test/java/michaelmoss/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package michaelmoss;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class CatTest {

@Test
void constructorHappyPath() {

// Arrange
String name = "Whiskers";
int age = 3;
String breed = "Siamese";
String color = "Black";
double weight = 4.5;

// Act
Cat cat = new Cat(name, age, breed, color, weight);

// Assert
assertEquals(name, cat.getName());
assertEquals(age, cat.getAge());
assertEquals(breed, cat.getBreed());
assertEquals(color, cat.getColor());
assertEquals(weight, cat.getWeight());
}

@Test
void constructorValidationThrowsException() {

// Arrange
int invalidAge = -1;

// Act & Assert
assertThrows(IllegalArgumentException.class, () -> {
new Cat("BadCat", invalidAge, "Siamese", "Black", 4.0);
});
}

@Test
void gettersAndSettersWork() {

// Arrange
Cat cat = new Cat("Kitty", 2, "Tabby", "Orange", 3.5);

// Act
cat.setName("Mittens");
cat.setAge(4);
cat.setBreed("Persian");
cat.setColor("White");
cat.setWeight(5.0);

// Assert
assertEquals("Mittens", cat.getName());
assertEquals(4, cat.getAge());
assertEquals("Persian", cat.getBreed());
assertEquals("White", cat.getColor());
assertEquals(5.0, cat.getWeight());
}

@Test
void eatHappyPath() {

// Arrange
Cat cat = new Cat("Leo", 2, "Maine Coon", "Gray", 5.0);

// Act
cat.eat(1.0);

// Assert
assertEquals(6.0, cat.getWeight());
}

@Test
void eatEdgeCaseZeroFood() {

// Arrange
Cat cat = new Cat("Leo", 2, "Maine Coon", "Gray", 5.0);

// Act
cat.eat(0);

// Assert
assertEquals(5.0, cat.getWeight());
}

@Test
void eatValidationThrowsException() {

// Arrange
Cat cat = new Cat("Leo", 2, "Maine Coon", "Gray", 5.0);

// Act & Assert
assertThrows(IllegalArgumentException.class, () -> {
cat.eat(-1);
});
}

@Test
void meowMethodRuns() {

// Arrange
Cat cat = new Cat("Whiskers", 3, "Siamese", "Black", 4.5);

// Act
cat.meow();

// Assert
assertNotNull(cat);
}
}

//✔ Constructor happy path tested
//✔ Constructor validation tested
//✔ Getters and setters tested
//✔ Custom methods tested
//✔ Edge case included (eat(0))
//✔ Validation test with assertThrows
//✔ AAA pattern comments in every test