zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    探索SaaS产业发展新机遇|鲁班会贵安首秀圆满收官
    带你认识三种kafka消息发送模式
    常用的echo和cat,这次让我折在了特殊字符丢失问题上
    当MySQL执行XA事务时遭遇崩溃,且看华为云如何保障数据一致性
    一图解析MySQL执行查询全流程
    HDFS源码解析:教你用HDFS客户端写数据
    大型集团企业云管平台建设参考架构
    带你了解家居智能的心脏:物联网关
    带你认识7种云化测试武器
    实践解析可视化开发平台FlinkSever优势
  • 原文地址:https://www.cnblogs.com/newff/p/6523453.html
Copyright © 2011-2022 走看看