Software Quality and Testing Experiment 5: Unit Testing with Eclipse (IDEA) Junit

1. Experimental purposes and requirements

1.1 Use Junit environment configuration in Eclipse (or IDEA) environment

1.2 Design of Junit test script

1.3 Test results

2. Experimental environment

window 10

IntelliJ IDEA 2023.1 x64

Java

Junit

3. Experiment content

  1. Configure the Junit environment in the IDEA tool
  2. Design a Junit test example script about the triangle problem
  3. Watch the output by running the test

4. Test case description and experimental steps

1. Configure Junit environment

(1) Open Eclipse (or IDEA) and open your Java project.

(2) Right-click the project and select the “Properties” (or “Build Path”) menu.

(3) In the pop-up window, select the “Java Build Path” (or “Libraries”) tab.

(4) Click the “Add Library” (or “Add JARs”) button.

(5) In the dialog box for selecting the JUnit library, browse and select the location of the JUnit library, and then click (6) “Finish” to complete the addition of the library.

import org.junit.Test;

@Test

Test method public void test(){}

2. Write test scripts

Triangle Problem Test Class

package com.company;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class test {

    private int p1;
    private int p2;
    private int p3;
    private String expected_triangle="";
    @Parameters
    public static Collection<Object[]> date(){
        //Put all test data in the object array object
        //object array object can use any data type
        Object[][] object= {
                //Statement coverage
        /*
//#Illegal (3 2 0 "illegal input")
//#Line segment (2 3 5 "a line")
//#Isosceles (3 3 5 "isosceles triangle")
//#Equilateral (3 3 3 "equilateral triangle")
//#Right angle (3 4 5 "right triangle")
//#acute angle (5 6 7 "acute triangle")
//#obtuse angle (3 6 5 "obtuse triangle")*/
                {3 ,2,0,"this is illegal input"},
                {2 ,3, 5, "this only construct a line"},
                {3 ,3 ,5 ,"this only constuct a isosceles triangle"},
                {3 ,3 ,3 ,"this only constuct a equilateral triangle"},
                {3 ,4 ,5 ,"this only constuct a right triangle"},
                {5 ,6 ,7 ,"this only constuct a acute triangle"},
                {3 ,6 ,5 ,"this only constuct a obtuse triangle"},

        };
        return Arrays.asList(object);
    }
    public test(int p1,int p2,int p3,String expect_triangle){
        this.p1=p1;
        this.p2=p2;
        this.p3=p3;
        this.expected_triangle=expect_triangle;
    }
    @Test
    public void test_istriangle(){
        Triangle triangle=new Triangle(p1,p2,p3);
        triangle.JudgeTriangle(triangle);
        String result_triangle=triangle.str;
     assertEquals(expected_triangle,result_triangle);
    }

}

Question test the next day

@RunWith(Parameterized.class)
public class DateTests {
    private String input1;
    private String input2;
    private String input3;
    private String expected;

    @Parameters
    public static Collection<?> prepareData(){
        String [][] object = {
                // Valid equivalence class
                {"2016","2","29","The next day is March 1, 2016!"},
                {"2017","1","28","The next day is January 29, 2017!"},
                {"2017","1","31","The next day is February 1, 2017!"},
                {"2017","4","30","The next day is May 1, 2017!"},
                // Invalid equivalence class
                {"1899","3","1","The value of the year is not within the specified range"},
                {"2051","3","1","The value of year is not within the specified range"},
                {"205%","3","1","Invalid input date!"},
                {"1901","-1","1","The value of month is not within the specified range"},
                {"1901","13","1","The value of month is not within the specified range"},
                {"1901","1%","1","Invalid input date!"},
                {"1901","1","-1","The value of day is not within the specified range"},
                {"2016","2","30","The value of day is not within the specified range"},
                {"2017","2","29","The value of day is not within the specified range"},
                {"2017","3","32","The value of day is not within the specified range"},
                {"2017","4","31","The value of day is not within the specified range"},
                {"2017","4","32","The value of day is not within the specified range"},
                {"2017","4","3%","Invalid input date!"}
        };
        return Arrays.asList(object);
    }
    public DateTests(String input1,String input2,String input3,String expected){
        this.input1 = input1;
        this.input2 = input2;
        this.input3 = input3;
        this.expected = expected;

    }
    @Test
    public void testDate(){
        String result = Date.nextDate(input1,input2,input3);
        Assert.assertEquals(expected,result);
    }
}

3. Execute tests

Triangle problem test passed

Question test the next day

5. Debugging process and experimental results

6. Summary

When I started using Eclipse (or IDEA) for JUnit unit testing, I ran into some problems. First, I don’t know how to configure the JUnit environment in the IDE. I’m stuck because I don’t find the JUnit library in my project. To solve this problem, I did some research and found a solution. I learned to add the JUnit library to the project build path in Eclipse (or IDEA). I right-clicked on the project, selected the “Properties” (or “Build Path”) menu, and added the JUnit library in the “Java Build Path” (or “Libraries”) tab. In this way, I successfully configured the JUnit environment.

I faced another problem, that is how to design JUnit test scripts. I’m not sure how to start writing test code. I started reading JUnit’s documentation and tutorials to understand how to write effective test scripts. Understand the basic principles of writing test scripts. I created a test class corresponding to the class to be tested and marked the test methods to be executed using the @Test annotation. I used the assertion method provided by JUnit to verify the consistency of the expected results with the actual results.

I also learned how to set up preconditions and cleanup operations. I marked a method using the @Before annotation to initialize the test environment before the test method is executed. Likewise, I marked a method using the @After annotation to perform cleanup operations after the test method is executed.

I also need to find a way to view my test results. I discovered that Eclipse (or IDEA) provides JUnit views to display test results. By running my unit tests in the IDE, I can view the execution status and detailed results of each test method in the JUnit view. By solving these issues, I successfully ran JUnit unit tests and obtained accurate test results. I have a better understanding and mastery of the entire process of unit testing using JUnit in an Eclipse (or IDEA) environment.