Question

In: Computer Science

Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To...

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:

  1. Valid types - You will receive credit for submitting a test set for a correct implementation of the triangleType method from the class provided (i.e. an equilateral, isosceles or scalene triangle). The JUnit test run tells you which of your test numbers is failing.
  2. Invalid types/error messages – You will need to design your test set for some bugs that we have intentionally placed into the triangleType method (i.e. error message). This part measures the quality of your test set. Have too few or too similar tests, and you won't be able to find all the bugs.

***************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

}

Solutions

Expert Solution

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

}


Related Solutions

Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
I'm really confused Junit test case Here is my code. How can I do Junit test...
I'm really confused Junit test case Here is my code. How can I do Junit test with this code? package pieces; import java.util.ArrayList; import board.Board; public class Knight extends Piece {    public Knight(int positionX, int positionY, boolean isWhite) {        super("N", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               // check if squares where knight can go are available for it       ...
Testing a Claim about a Mean Test the Claim: The mean time to take this exam...
Testing a Claim about a Mean Test the Claim: The mean time to take this exam is greater than 30 minutes. (Note: There will be NO credit given for wrong answers even if they are correct based on previous wrong answers – so check your work) Sample data: n = 25, x = 32 minutes, s = 5 minutes. α = 0.05 6. What is the value of the Test Statistic? (1 Point) 7. What is/are the Critical Value(s)? (1...
1. For the standardized test statistic approach to hypothesis testing, calculate the test statistic for testing...
1. For the standardized test statistic approach to hypothesis testing, calculate the test statistic for testing the null hypothesis that the population mean is less than or equal to 9.29, given a sample mean of 13.90, a sample size of 35, and a population standard deviation of 3.92. Round to two decimals. 2. Using the traditional hypothesis testing approach, calculate the critical value for testing the null hypothesis that the population mean is greater than or equal to 13, given...
Testing Claims You wish to test the following claim ( H a ) at a significance...
Testing Claims You wish to test the following claim ( H a ) at a significance level of α = 0.001 . H o : p = 0.71 H a : p < 0.71 You obtain a sample of size n = 636 in which there are 420 successful observations. What is the test statistic for this sample? (Report answer accurate to three decimal places.) test statistic = 0.573 Incorrect What is the p-value for this sample? (Report answer accurate...
You are interested in enhancing learning. You decide to test the hypothesis that continual self-testing as...
You are interested in enhancing learning. You decide to test the hypothesis that continual self-testing as part of the learning process can benefit the long-term retention of information. In this case, testing refers to learning the material to a 100% accurate level and then continuing to be tested on the material during later learning trials. You decide to compare participants in the self-test condition to participants who learned the material to the 100% level and continued to study the material...
You are testing the change in test scores following an intensive tutoring session. You have the...
You are testing the change in test scores following an intensive tutoring session. You have the following data from a small group of students each student is tested before and after the tutoring session. Each row represents one student. |Time 1 |Time 2| |---------|------| |65 |77 | |87 |100 | |77 |75 | |90 |89 | |70 |80 | |84 |81 | |92 |91 | |83 |96 | |85 |84 | |91 |89 | |68 |88 | |72 |100 |...
What are impurities and their testing method of metoclopramide ?
What are impurities and their testing method of metoclopramide ?
In your own words, describe what you have learned about hypothesis testing and hypothesis testing for...
In your own words, describe what you have learned about hypothesis testing and hypothesis testing for the mean. Give one or more examples of how you could use this information to test a problem. Be specific on what the problem would be about.
// TESTING //------------------------------------------------------------------------------ // TEST CASE 1 // // DESCRIPTION // Performs an acceptance test of...
// TESTING //------------------------------------------------------------------------------ // TEST CASE 1 // // DESCRIPTION // Performs an acceptance test of the entire program. // // INPUT DATA (Note that the input data is read from a file named payroll.txt) // Simpson Homer // 15.25 84 // 2 // // EXPECTED OUTPUT // ----------------------------- // EMPLOYEE: Simpson, Homer // // PAY RATE: $ 15.25 // HOURS: 84.00 // GROSS PAY: $ 1311.50 // MED INS DEDUCT: $ 110.13 // 401K DEDUCT: $ 78.69 // FED...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT