Unit Testing with Eclipse

Source: Internet
Author: User
Tags assert

    • Install Eclipse
      • Download eclipse

This tutorial takes Windows7 as an example, using the Green install-free version of Eclipse, Link: http://pan.baidu.com/s/1kVNhUKZ Password: sbak

      • Installation
    1. Right-click eclipse.rar--> "Unzip to current folder". (note, do not unzip to the Chinese path!) )
    2. Install Jdk-8u73-windows-x64.exe, no special settings, select the path and always click "Next" until the installation is complete. (Note, do not install to the Chinese path!) Make note of the installation path and use it later! )
      • Configure environment Variables 1, right-click "My Computer" and "Properties", in the pop-up window select "Advanced System Settings"--"Environment variables"--click on "new"-Variable name: Classpath variable value under the system variables:.;D: \ Program Files\java\jdk1.8.0_73\lib; (Note that the variable value is ".; The path to the Bin folder in the JDK ", do not forget the front of the".; "). As shown in the following:

2, the system variable "new"--Variable name: Java_home variable value: D:\Program files\java\jdk1.8.0_73; (The variable value is the path to the JDK)

As shown in the following:

3. Click "Path" in the system variable--"edit"--Variable value: D:\Program files\java\jdk1.8.0_73\bin; (in the top-most English-speaking ";", and then add the path to the Bin folder in the JDK before the semicolon).

At this point, the Eclipse installation is complete.

  • Unit Test
    • Source
      /*
      The following code implements the maximum number of sub-arrays and algorithms
      Coding.net Address: Https://git.coding.net/Aaric/MaximumSubarray.git
      */

      Packagesoftwareengineering; Public classMaximumsubarray { Public intFindmaximumsubarray (intNint[] s_subarray) { intS_sum, s_maxsum = s_subarray[0]; for(inti = 0; I < n; i++) {s_sum= 0; for(intK = i; K < n; k++) {s_sum+=S_subarray[k]; if(S_sum >s_maxsum) {S_maxsum=s_sum; } } } returns_maxsum; }} Packagesoftwareengineering; Public classMaximumsubarray { Public intFindmaximumsubarray (intNint[] s_subarray) { intS_sum,
      S_maxsum = 0; Note that there is a bug and should be changed to S_maxsum = S_subarray[0] for(inti = 0; I < n; i++) {s_sum= 0; for(intK = i; K < n; k++) {s_sum+=S_subarray[k]; if(S_sum >s_maxsum) {S_maxsum=s_sum; } } } returns_maxsum; }}
      • Add JUnit to a project

Right-click the project name to select "Properties", select "Java Build Path" in the pop-up form and select "Libraries" tab--"Add Library" and select "Junit"--"in the popup window. Next ", as shown in:

Select the "Junit" and version number that we need to add, and add a successful confirmation.

      • New JUnit

Select the project right-click "New"-"Others"-"JUnit" (in the Java directory)--"JUnit Test case".

Fill in the name of the unit test, tick the "setUp" method, select the class to be tested and the method being tested,

    • Test
      • With the above steps, JUnit is created and you can start writing your own test code.
      •  Packagesoftwareengineering;Import Staticorg.junit.assert.*;ImportOrg.junit.Before;Importorg.junit.Test; Public classTestUnit01 {@Before Public voidSetUp ()throwsException {/** Before you complete the test preparation work **/} @Test Public voidTestfindmaximumsubarray () {/** Write the test code here **/    }}

        The following is the complete test code

        Package softwareengineering;

        Import static org.junit.assert.*;

        Import Java.util.Scanner;

        Import Org.junit.Before;
        Import Org.junit.Test;


        public class TestUnit01 {

        Private Maximumsubarray t_maxsum;
        Private int[] T_VC;
        private static final int n=5; Number of test data
        Private Scanner SC;

        @Before
        public void SetUp () throws Exception {
        T_maxsum = new Maximumsubarray ();
        T_VC = new Int[n];
        sc = new Scanner (system.in);
        for (int i = 0; i < n; i++)
        {
        T_vc[i] = (Integer) sc.nextint (); Enter test data
        }
        }

        @Test
        public void Testfindmaximumsubarray () {
        int res = T_maxsum.findmaximumsubarray (n, T_VC); Test
        Assertequals ( -1,res); Compare test results with expected values
        }

        }

        After modifying the code as described above, right-click on "Runas-->1 JUnit test" to run the unit test and enter the data to be tested in the console. As shown in the following:

      • After adjusting the bug indicated in the source code, the test results are as follows:


      • Here are the results of my unit tests.

      • Use Case number Use case description Input data Expected output value Actual output value Pass/No Pass Evaluation
        1 statement overrides 1,2,3,4,5 15 15 through
        2 criteria override -1,-2,-3,-4,-5 -1 0 not through

        need to modify

        3 Decision condition Override -1,-2,-3,-4,-5 -1 -1 Pass

        Source Code Modification

        After that, the test succeeds

    • End
      • Original blog, reproduced please indicate the source!

Unit testing with Eclipse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.