In: Computer Science
Triangle Testing
You are about to test the implementations of triangleType method with JUnit Testing. To test this Triangle class, you write a JUnit test class named TriangleTest.java. This test class contains multiple test cases in the format of methods. The test runner executes TriangleTest to generate a JUnit test report.
In this project, you do not need to write the code for this Triangle class. Instead, you will write JUnit test cases to make sure that the implementation of triangelType method meets the requirements. The main method will not be tested; you may use it any way you want.
Download Triangle.java and the template TriangleTests.txt. Rename the template as FirstnameLastnameTriangleTest.java (ex. JohnSmithTriangleTest.java). The template file contains 20 test cases. The first three test cases are given; you need to complete rest of the test cases. Do not remove any test cases from the template, just change the contents of the methods to test various situations. The goal is to write a comprehensive set of test cases (i.e. reasonable and non-repeated) that covers all the different possible situations. For example, (5 5 5) and (6 6 6) are considered repeated test cases with equilateral type of arguments.
Your test cases will be graded based on the output from JUnit. A Triangle class file will be provided for testing purpose. You cannot modify the Triangle class.
There are two types of test cases:
***************Triangle.java***************
// Triangle class
public class Triangle {
// Instance variables for 3 triangle sides
private String side1;
private String side2;
private String side3;
// Constructors
public Triangle() {
// a default triangle
this("0", "0", "0");
}
public Triangle(String str1, String str2, String str3) {
this.side1 = str1;
this.side2 = str2;
this.side3 = str3;
}
// Instance methods
public String triangleType() {
String type = "";
int s1 = 0, s2 = 0, s3 = 0;
String errMessage = "";
try {
s1 = Integer.parseInt(side1);
} catch (NumberFormatException e) {
errMessage += "The side 1 is not an integer number.\n\n";
}
try {
s2 = Integer.parseInt(side2);
} catch (NumberFormatException e) {
errMessage += "The side 2 is not an integer number.\n\n";
}
try {
s3 = Integer.parseInt(side3);
} catch (NumberFormatException e) {
errMessage += "The side 3 is not an integer number.\n\n";
}
// Check for a negative side
if (s1 < 0 || s2 < 0 || s3 < 0) {
errMessage += "At least one side is negative!\n";
}
// Check for vaide sides
if ((s1 + s2 <= s3) || (s1 + s3 <= s2) || (s2 + s3 <= s1))
{
errMessage += "Not a valid triangle!\n";
}
if (s1 + s2 + s3 > 1000) {
errMessage += "This triangle is too big.\n";
}
if (errMessage.length() > 0) {
type = errMessage ;
}
else {
if ((s1 == s2) && (s2 == s3)) {
type = "This is an equilateral triangle. ";
}
else if (( s1 == s2) || (s2 == s3) || (s1 == s3)) {
type = "This is an isosceles triangle. ";
}
else {
type = "This is a scalene triangle. ";
}
}
return type;
}
}
**********TriangleTest.txt***************
import static org.junit.Assert.*;
import org.junit.Test;
public class TriangleTest {
@Test
public void test1(){
Triangle triangle = new
Triangle("12","12","12");
assertEquals("This is an
equilateral triangle. ",triangle.triangleType());
}//end test
@Test
public void test2(){
Triangle triangle = new
Triangle("3","3","5");
assertEquals("This is an isosceles
triangle. ",triangle.triangleType());
}//end test
@Test
public void test3(){
Triangle triangle = new
Triangle("4","5","6");
assertEquals("This is a scalene
triangle. ", triangle.triangleType());
// expected value: "This is a
scalene triangle. "
// value returned from the method:
triangle.triangleType()
}//end test
@Test
public void test4(){
// add your code below
assertEquals(true,true);
// change above arguments(true,
true) to (expectedValue, valueReturned)
}//end test
@Test
public void test5(){
assertEquals(true,true);
}//end test
@Test
public void test6(){
assertEquals(true,true);
}//end test
@Test
public void test7(){
assertEquals(true,true);
}//end test
@Test
public void test8(){
assertEquals(true,true);
}//end test
@Test
public void test9(){
assertEquals(true,true);
}//end test
@Test
public void test10(){
assertEquals(true,true);
}//end test
@Test
public void test11(){
assertEquals(true,true);
}//end test
@Test
public void test12(){
assertEquals(true,true);
}//end test
@Test
public void test13(){
assertEquals(true,true);
}//end test
@Test
public void test14(){
assertEquals(true,true);
}//end test
@Test
public void test15(){
assertEquals(true,true);
}//end test
@Test
public void test16(){
assertEquals(true,true);
}//end test
@Test
public void test17(){
assertEquals(true,true);
}//end test
@Test
public void test18(){
assertEquals(true,true);
}//end test
@Test
public void test19(){
assertEquals(true,true);
}//end test
@Test
public void test20(){
assertEquals(true,true);
}//end test
}
Please find below code for junit test cases:
package test;
import static org.junit.Assert.*;
import org.junit.Test;
public class TriangleTest {
private final String EQUILATERAL_TRAINGLE="This is an
equilateral triangle. ";
private final String ISOSCELES_TRIANGLE="This is an
isosceles triangle. ";
private final String SCLANE_TRIANGLE="This is a
scalene triangle. ";
private final String NOT_A_VALID_TRAINGLE="Not a valid
triangle!\n";
private final String ONE_SIDE_IS_NEGATIVE="At least
one side is negative!\n";
private final String SIDE_1_IS_NOT_INTEGER="The side 1
is not an integer number.\n\n";
private final String SIDE_2_IS_NOT_INTEGER="The side 2
is not an integer number.\n\n";
private final String SIDE_3_IS_NOT_INTEGER="The side 3
is not an integer number.\n\n";
private final String TRIANGLE_IS_TOO_BIG="This
triangle is too big.\n";
@Test
public void test1(){
Triangle triangle = new Triangle("12","12","12");
assertEquals(EQUILATERAL_TRAINGLE,triangle.triangleType());
}//end test
@Test
public void test2(){
Triangle triangle = new Triangle("3","3","5");
assertEquals(ISOSCELES_TRIANGLE,triangle.triangleType());
}//end test
@Test
public void test3(){
Triangle triangle = new Triangle("4","5","6");
assertEquals(SCLANE_TRIANGLE, triangle.triangleType());
}//end test
@Test
public void test4(){
Triangle triangle = new Triangle();
assertEquals(NOT_A_VALID_TRAINGLE, triangle.triangleType());
}//end test
@Test
public void test5(){
Triangle triangle = new Triangle("-4","5","6");
assertEquals(ONE_SIDE_IS_NEGATIVE+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test6(){
Triangle triangle = new Triangle("4P","5","6");
assertEquals(SIDE_1_IS_NOT_INTEGER+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test7(){
Triangle triangle = new Triangle("4","5P","6");
assertEquals(SIDE_2_IS_NOT_INTEGER+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test8(){
Triangle triangle = new Triangle("4","5","6P");
assertEquals(SIDE_3_IS_NOT_INTEGER+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test9(){
Triangle triangle = new
Triangle("400","500","600");
assertEquals(TRIANGLE_IS_TOO_BIG, triangle.triangleType());
}//end test
@Test
public void test10(){
Triangle triangle = new Triangle("2","3","6");
assertEquals(NOT_A_VALID_TRAINGLE, triangle.triangleType());
}//end test
@Test
public void test11(){
Triangle triangle = new Triangle("4","5P","-6");
assertEquals(SIDE_2_IS_NOT_INTEGER+ONE_SIDE_IS_NEGATIVE+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test12(){
Triangle triangle = new Triangle("4P","5","-6");
assertEquals(SIDE_1_IS_NOT_INTEGER+ONE_SIDE_IS_NEGATIVE+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test13(){
Triangle triangle = new Triangle("4","-5","6P");
assertEquals(SIDE_3_IS_NOT_INTEGER+ONE_SIDE_IS_NEGATIVE+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test14(){
Triangle triangle = new Triangle("4P","5P","6");
assertEquals(SIDE_1_IS_NOT_INTEGER+SIDE_2_IS_NOT_INTEGER+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test15(){
Triangle triangle = new Triangle("4P","5","6P");
assertEquals(SIDE_1_IS_NOT_INTEGER+SIDE_3_IS_NOT_INTEGER+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test16(){
Triangle triangle = new Triangle("4","5P","6P");
assertEquals(SIDE_2_IS_NOT_INTEGER+SIDE_3_IS_NOT_INTEGER+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test17(){
Triangle triangle = new
Triangle("4P","5P","6P");
assertEquals(SIDE_1_IS_NOT_INTEGER+SIDE_2_IS_NOT_INTEGER+SIDE_3_IS_NOT_INTEGER+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test18(){
Triangle triangle = new
Triangle("4P","-5","6P");
assertEquals(SIDE_1_IS_NOT_INTEGER+SIDE_3_IS_NOT_INTEGER+ONE_SIDE_IS_NEGATIVE+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test19(){
Triangle triangle = new
Triangle("-4","5P","6P");
assertEquals(SIDE_2_IS_NOT_INTEGER+SIDE_3_IS_NOT_INTEGER+ONE_SIDE_IS_NEGATIVE+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
@Test
public void test20(){
Triangle triangle = new
Triangle("4P","5P","-6");
assertEquals(SIDE_1_IS_NOT_INTEGER+SIDE_2_IS_NOT_INTEGER+ONE_SIDE_IS_NEGATIVE+NOT_A_VALID_TRAINGLE,
triangle.triangleType());
}//end test
}