• ST Lab1 junit test


    代码地址:  https://github.com/newff/st-lab1

    Tasks:

    1. Install Junit(4.12), Hamcrest(1.3) with Eclipse
    2. Install Eclemma with Eclipse
    3. Write a java program for the triangle problem and test the program with Junit.

    a)       Description of triangle problem:

    Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral(等边), isosceles(等腰), or scalene(斜角体). 

    1. Install Junit(4.12), Hamcrest(1.3) with Eclipse

      To install jar package into Eclipse, we can use Maven or download the jar package the add to Eclipse. In my earlier blogs I have telled how to add jar package through Maven, in this passage I tell how to download and add directly.

      Online websites:

        Junit:         http://junit.org/junit4/

        hamcrest:  http://search.maven.org/#search|ga|1|g%3Aorg.hamcrest

      hamcrest-all.* is recommended.

      Choose the proper version and download it.

    Open Eclipse, file->new->Java Project, name it "javaPro", it finished to new a new project. Then, on your "javaPro", right click->properties->Java Build Path->Libraries->Add External JARs, select the directory of your jar packages. click Apply and OK. You can see the two packages in your Referenced Library.

    2. Install Eclemma with Eclipse

    website:      http://www.eclemma.org/installation.html#marketplace

    There are three options for us to install the Eclemma on this website, you can choose anyone of them. I chose the first one and then restart my eclipse, it was already installed.

    In your "javaPro", help->Eclipse MarketPlace, search "Eclemma ",and then install. It finished to install the Eclemma.

    3.Write a java program for the triangle problem and test the program with Junit. 

    The java program is on the github. To test the program with junit, we have to right click the "Triangle.java", and then new->Junit Test Case, 

    Browse the functions that we  want to test, then finish.

     Write the test function for the Triangle function, then, add some test cases, click the button to run and we will see the result.

    4.Result of the test

    When I test all of the four conditions, the coverage is 100%.

     

    One error I have made in my lab is that when I judge whether it is a riangle, I wrote this"if((a+b>c && a-b<c) || (a+c>b && a-c<b) || (b+c>a && b-c<a))",this program get the wrong answer. Then I fixed the error and the program become right.

     5.Prolongation

    What's more, I tried to use Parameters in my junit test.

    These are my codes:

     1 package javaPro;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Arrays;
     6 import java.util.Collection;
     7 
     8 import org.junit.Before;
     9 import org.junit.Test;
    10 import org.junit.runner.RunWith;
    11 import org.junit.runners.Parameterized;
    12 import org.junit.runners.Parameterized.Parameters;
    13 
    14 @RunWith(Parameterized.class)
    15 public class TriangleTest {
    16     private int a;
    17     private int b;
    18     private int c;
    19     private int expected;
    20     private Triangle triangle;
    21 
    22     public TriangleTest(int a, int b, int c, int expected){
    23                  this.a = a;
    24                  this.b = b;
    25                  this.c = c;
    26                  this.expected = expected;
    27              }
    28     @Before
    29     public void setUp() throws Exception {
    30         System.out.println("Before test");
    31         triangle = new Triangle();
    32     }
    33     
    34     @Parameters
    35     public static Collection<Object[]>getData(){
    36         return Arrays.asList(new Object[][]{
    37             {1, 2, 3, 3},
    38             {1, 1, 1, 0},
    39             {2, 2, 3, 1},
    40             {3, 4, 5, 2}
    41         });
    42     }
    43     @Test
    44     public void testTriangles() {
    45         assertEquals (this.expected, triangle.triangles(a, b, c));
    46 //        assertEquals (0, triangle.triangles (1,1,1));
    47 //        assertEquals (1, triangle.triangles (2,2,3));
    48 //        assertEquals (2, triangle.triangles (3,4,5));
    49     }
    50 
    51 }

    And this is the result, I have also put my code on the Github.

    I made a mistake that the name of the constructed function must be the same with the name of the class. It worked after I have fixed this mistake.

  • 相关阅读:
    CentOS7下搭建hadoop2.7.3完全分布式
    在linux命令行利用SecureCRT上传下载文件
    SPDY和HTTP
    哈希表的工作原理
    LVS(Linux Virtual Server)
    Discuz x3 UCenter实现同步登陆原理
    Goroutine(协程)为何能处理大并发?
    缓存与DB数据一致性问题解决的几个思路
    固态硬盘SSD与闪存(Flash Memory)
    堆和栈的区别(转过无数次的文章)
  • 原文地址:https://www.cnblogs.com/newff/p/6523453.html
Copyright © 2020-2023  润新知