Can you please help me write JAVA @Test code cover each single scenario for the
ID: 3781119 • Letter: C
Question
Can you please help me write JAVA @Test code cover each single scenario for the program below ?
At least 12 test cases. Remember that a test should be small, and cover a single scenario.
Your tests should test both "good" dates to ensure that they are correctly identified as "good," and "bad" dates to ensure that they are correctly identified as "bad."
The Date class specification stated that Spring started on March 21, Summer on June 21, Fall on September 23 and Winter on December 21.
Please make a new program call something like Tester.java for testing Date.java and Month.java programs. I gave DistanceTest.java and Distance.java program as an example for Tester program.
--- Please don't change Date.java program ---
/**
*
* This class represents a date given the month and the day of the month. For the
* given date, this class provides a method for determining the season in the northern hemisphere for the date.
*
* For example, the given code fragment the output to the console should be WINTER.
*
* Date jan1 = new Date(1, 1);
* String season = jan1.getSeason();
* System.out.println(season);
*
* @author parks
*
*/
public class Date
{
private static final Month year[] = {
new Month("January", 1, 31),
new Month("February", 2, 29),
new Month("March", 3, 31),
new Month("April", 4, 30),
new Month("May", 5, 31),
new Month("June", 6, 30),
new Month("July", 7, 31),
new Month("August", 8, 31),
new Month("September", 9, 30),
new Month("October", 10, 31),
new Month("November", 11, 30),
new Month("December", 12, 31),
};
private int month;
private int day;
/**
* Constructs a new Date object. The month should be a value
* from 1 -12 and the day from 1 - 31.
*
* @param theMonth the month
* @param theDay the day
*/
public Date(int theMonth, int theDay)
{
month = theMonth;
day = theDay;
}
/**
* This method returns the String representation for the month. For example:
* "January" for month == 1, "February" for month == 2, etc
*
* @return string representation of the month
*/
public String getMonth()
{
String result = "UNKNOWN";
if (isValidDay()) {
result = year[month - 1].getMonthName();
}
return result;
}
/**
* Returns the season that this days falls in based on the following chart:
*
* SEASON RETURNED FROM TO
* SPRING March 21 June 20
* SUMMER June 21 September 22
* FALL September 23 December 20
* WINTER December 21 March 20
*
* If the day does not represent a valid day it will return UNKNOWN
*
* @return the string representation for the season:
* WINTER | SPRING | SUMMER | FALL | UNKNOWN
*/
public String getSeason()
{
String result = "UNKNOWN";
if (!isValidDay()) {
return result;
}
if (((month == 12) && day >= 21) || (month == 1 ) || (month == 2) || (month == 3 && day <= 20)) {
result = "Winter";
} else if (((month == 3) && day >= 21) || (month == 4 ) || (month == 5) || (month == 6 && day <= 20)) {
result = "Spring";
} else if (((month == 6) && day >= 21) || (month == 7 ) || (month == 8) || (month == 9 && day <= 22)) {
result = "Summer";
} else {
result = "Fall";
}
return result;
}
/**
*
* Returns true if the day is a valid day of the month. For example if the
* day were 31 and the month was 11 (November) this method would return
* false since November only has 30 days. Here is a (shortened version)
* of a Mother Goose rhyme for the number of days in each month:
*
* Thirty days hath September,
* April, June, and November.
* All the rest have thirty-one,
* Excepting February (which we will say has 29 days)
*
* @return true if the day is a valid day of the month otherwise false
*/
public boolean isValidDay()
{
boolean rc = false;
if (isValidMonth() && day > 0 && day <= year[month - 1].getMonthDays()) {
rc = true;
}
return rc;
}
/**
* Returns true if the day is a valid month. Valid months have values from 1 - 12 (inclusive)
*
* @return true if the day is a valid day of the month otherwise false
*/
public boolean isValidMonth()
{
return month > 0 && month <= year.length;
}
}
------------
--- Please don't change Month.java ---
/**
*
* Simple store for a month's number (1-origin), name
* and number of days in the month.
*
* @author parks
*
*/
public class Month {
private int monthNumber;
private String monthName;
private int monthDays;
public Month(String name, int number, int days) {
setMonthName(name);
setMonthDays(days);
setMonthNumber(number);
}
public boolean isDayValid(int d) {
return d <= monthDays;
}
public String getMonthName() {
return monthName;
}
public void setMonthName(String monthName) {
this.monthName = monthName;
}
public int getMonthDays() {
return monthDays;
}
public void setMonthDays(int monthDays) {
this.monthDays = monthDays;
}
public int getMonthNumber() {
return monthNumber;
}
public void setMonthNumber(int monthNumber) {
this.monthNumber = monthNumber;
}
}
========================================================================
Here example how @Test code should look like:
Distance.java
public class Distance {
private double currX;
private double currY;
private double currDist;
public Distance() {
currX = 0.0;
currY = 0.0;
currDist = 0.0;
}
public Distance(double nx, double ny) {
currX = nx;
currY = ny;
currDist = 0;
}
public void addPoint(double newX, double newY) {
double dis = Math.sqrt(Math.pow(currX - newX, 2) +
Math.pow(currY - newY, 2));
currDist += dis;
currX = newX;
currY = newY;
}
public double findDistance() {
return currDist;
}
}
----------------------
DistanceTest.java
import org.junit.Test;
import static org.junit.Assert.*;
public class DistanceTest {
@Test
public void Test0() {
Distance dist = new Distance();
double d = dist.findDistance();
assertEquals(.0, d, .00001);
}
@Test
public void Test1() {
Distance dist = new Distance();
dist.addPoint(3, 4);
double d = dist.findDistance();
assertEquals(5.0, d, .00001);
}
}
Explanation / Answer
MonthTest.java
public class MonthTest {
public MonthTest()
{
}
public void Test1()
{
//test for valid season #case 1
Date jan1 = new Date(1, 1);
String season = jan1.getSeason();
System.out.println(season);
}
public void Test2()
{
//test for invalid season with invalid month #case 2
Date d2= new Date(13,1);
String season =d2.getSeason();
System.out.println(season);
}
public void Test3()
{
//test for invalid season with invalid month #case 3
Date d3= new Date(0,1);
String season =d3.getSeason();
System.out.println(season);
}
public void Test4()
{
//test for invalid season with invalid day #case 4
Date d4= new Date(1,32);
String season =d4.getSeason();
System.out.println(season);
}
public void Test5()
{
//test for invalid season with invalid day #case 5
Date d5= new Date(1,0);
String season =d5.getSeason();
System.out.println(season);
}
public void Test6()
{
//test for invalid day for feb month #case 6
Date d6= new Date(2,30);
String season =d6.getSeason();
System.out.println(season);
}
public void Test7()
{
//test for getmonth() with valid month and invalid day #case 7
Date d7= new Date(2,30);
String monthName;
monthName = d7.getMonth();
System.out.println(monthName);
}
public void Test8()
{
//test for function getMonth with valid month and day #case 8
Date d8= new Date(2,28);
String monthName = d8.getMonth();
System.out.println(monthName);
}
public void Test9()
{
//test for valid season #case 9
Date d9 = new Date(9, 10);
String season = d9.getSeason();
System.out.println(season);}
public void Test10()
{
//test for valid season #case 10
Date d10 = new Date(10, 10);
String season = d10.getSeason();
System.out.println(season);
}
public void Test11()
{
//test for valid season #case 11
Date d11 = new Date(4, 10);
String season = d11.getSeason();
System.out.println(season);
}
public void Test12()
{
//test for invalid season with both invalid day and month #case 12
Date d12 = new Date(-1, -1);
String season = d12.getSeason();
System.out.println(season);
}
public static void main (String[] args)
{
MonthTest test= new MonthTest();
System.out.println("Testing with valid and invalid date");
test.Test1();
test.Test2();
test.Test3();
test.Test4();
test.Test5();
test.Test6();
test.Test7();
test.Test8();
test.Test9();
test.Test10();
test.Test11();
test.Test12();
}
}
---------------------------------------------------------------------------
output
Testing with valid and invalid date
Winter
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
February
Summer
Fall
Spring
UNKNOWN