Skip to content

ravalkevin/Lab-8_202001170

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

Lab-8_202001170

Name:KEVIN RAVAL

ID: 202001170

Unit Testing with JUnit


Creating Java Package and Boa Class

i created a new eclipse project with name Lab8_202001170 and then create package,create class for Boa, the code is as follow

package tests;

public class Boa {
	private String name; 
	private int length; // the length of the boa, in feet 
	private String favoriteFood; 
	public Boa (String name, int length, String favoriteFood){
		this.name = name; 
		this.length = length; 
		this.favoriteFood = favoriteFood; 
	} 
	 // returns true if this boa constrictor is healthy 
	public boolean isHealthy(){ 
		return this.favoriteFood.equals("granola bars");
	} 
	// returns true if the length of this boa constrictor is 
	// less than the given cage length 
	public boolean fitsInCage(int cageLength){
		return this.length < cageLength; 
	}
}

Creating a JUnit Test Class in Eclipse

i created testcase file tests.java for Boa.java

After configuring the test case file, i define the setUp() function as follows

package tests;
import org.junit.Before;
import org.junit.Test;
public class tests {
	Boa jen,ken;
	
	@Before
	public void setUp() throws Exception { 
		jen = new Boa("Jennifer", 2, "grapes");
		ken = new Boa ("Kenneth", 3, "granola bars");
	} 
}

Testing Boa class methods

isHealthy() method

It is important to note that the isHealthy() function does not take any parameters. Due to this, the only test cases possible are to call the method on Boa objects "jen" and "ken".
Thus, the test cases defined in the testIsHealthy() test function is as follows:

  @Test
  public void testIsHealthy() {
      assertEquals("Error in isHealthy()",false, jen.isHealthy());
      assertEquals("Error in isHealthy()",true, ken.isHealthy());
  }

FitsInCage() method

The FitsInCage() function takes an integer parameter as input and hence can have a range of test-cases possible.
Although jen and ken both will be executing the same function definition for FitsInCage(<cage_length>) function call, it is better to call methods from both the instances to ensure independency of the function definition from the instance.

The test-cases defined can thus go as follows:

@Test
public void testFitsInCage() {
    assertEquals("Error in fitsInCage()",true,jen.fitsInCage(5));  // For length of cage greater than jen's boa
    assertEquals("Error in fitsInCage()",false,jen.fitsInCage(2)); // For length of cage equal to jen's boa
    assertEquals("Error in fitsInCage()",false,jen.fitsInCage(1)); // For length of cage less than jen's boa
    assertEquals("Error in fitsInCage()",false,ken.fitsInCage(2)); // For length of cage less than ken's boa
    assertEquals("Error in fitsInCage()",false,ken.fitsInCage(3)); // For length of cage equal to ken's boa
    assertEquals("Error in fitsInCage()",true,ken.fitsInCage(5));  // For length of cage greater than ken's boa
}

Running the Test Cases

To run the test cases, i simply right-click on the file(BoaTest.java here), click on Coverage As, and then 1 JUnit Test.

on the first run 2 out of 2 test cases are passed

image

Adding new Method

i add a new method lengthInches() to return the length of the Boa in inches.

// produces the length of the Boa in inches 
public int lengthInInches(){ 
    return this.length*12;
}

The new test cases defined for the same are as follows:

@Test
public void testlengthInInches() {
    assertEquals("Error in fitsInCage()",24,jen.lengthInInches());
    assertEquals("Error in fitsInCage()",36,ken.lengthInInches());
}

The test case pass successfully as shown below

WhatsApp Image 2023-04-20 at 15 26 19

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published