Commit 2cc3cfda authored by a.guest's avatar a.guest

Initial Commit

parents
Pipeline #148 failed with stages
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>UnitTestingExampleLeapYear</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
public class DateTool {
public static final int[] DayCount = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public boolean isLeapYear(int aYear) {
if (aYear % 400 == 0) {
return true;
} else if (aYear % 100 == 0) {
return false;
} else if (aYear % 4 == 0) {
return true;
} else {
return false;
}
}
public int daysInMonth(int aMonth, int aYear) {
if ((aMonth == 2) && (this.isLeapYear(aYear))) {
return 29;
} else {
return DayCount[aMonth-1];
}
}
}
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
/**
*
*/
/**
* @author Andy
*
*/
@DisplayName("DateTool Unit Tests")
class DateToolTest {
public static DateTool tool;
public static int testCount = 0;
@BeforeAll // Gets called before any tests are run
static void setUpBeforeClass() throws Exception {
System.out.println("Preparing to test DateTool.");
tool = new DateTool();
}
@AfterAll // Gets called when all tests are complete
static void tearDownAfterClass() throws Exception {
System.out.println("Completed testing of DateTool.");
}
@BeforeEach // Gets called before each test
void setUp() throws Exception {
System.out.print("Test Number : " + testCount + " : ");
testCount++;
}
@AfterEach // Gets called after each test
void tearDown() throws Exception {
}
// Test to see if isLeapYear correctly returns that
// 1900 is not a leap year.
// Demonstrates assertFalse()
@Test
@DisplayName("Testing Not Leap Year")
void testCenturyNotLeap() {
int aYear = 1900;
System.out.println("Testing Not Leap Year " + aYear);
assertFalse(tool.isLeapYear(1900));
}
// Test to see if isLeapYear correctly returns that
// 2000 is a leap year.
// Demonstrates assertTrue()
@Test
@DisplayName("Testing Leap Year")
void testFourthCenturyLeap() {
int aYear = 2000;
System.out.println("Testing Leap Year " + aYear);
assertTrue(tool.isLeapYear(2000));
}
// Tests a selection of leap years are correctly identified
// Demonstrates using @ValueSource of ints
@ParameterizedTest
@DisplayName("Parameterized Test - Valid Leap Years")
@ValueSource(ints = {1980, 1988, 2000})
void testValidLeapYears(int aYear) {
System.out.println("Parameterized Testing Leap Year " + aYear);
assertTrue(tool.isLeapYear(aYear));
}
// Tests a selection of non-leap years are correctly identified
// Demonstrates using @ValueSource of ints
@ParameterizedTest
@DisplayName("Parameterized Test - Invalid Leap Years")
@ValueSource(ints = {1981, 1900, 1800})
void arrayTestInvalidLeapYears(int aYear) {
System.out.println("Parameterized Testing Year Non-Leap " + aYear);
assertFalse(tool.isLeapYear(aYear));
}
// Demonstrates Parameterized Test using Csv source to
// get both a parameter and an expected result
// Note you can't use AssertEquals to test for a boolean value
@ParameterizedTest
@DisplayName("Parameterized CSV Test - Leap Year")
@CsvSource({"1980,true","1981,false"})
void arrayTestLeapYears(int aYear, boolean expected) {
System.out.println("Testing " + aYear);
if (expected)
{
assertTrue(tool.isLeapYear(aYear));
}
else
{
assertFalse(tool.isLeapYear(aYear));
}
}
// Demonstrates Parameterized Test using Csv source to
// get multiple parameters and an expected result
// Since the return value is an integer we can use AssertEquals
@DisplayName("Array Test - Day Count")
@ParameterizedTest(name = "{0}, {1} should be {2}")
@CsvSource({"1,2018,31", "2,2018,28", "2,2020,29"})
void csvTestDayCount(int m, int y, int count)
{
System.out.println("CSV Testing Day Count " + m + ":" + y);
assertEquals(count, tool.daysInMonth(m, y));
}
}
public class MainHub {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment