JUnit 4 Tutorials (junit 4 tutorial), JUnit Introduction and JUnit Eclipse Tutorial

Source: Internet
Author: User
Tags arithmetic assert



Introduction to the Junit test framework


The test framework is the most popular Java unit testing framework. JUnit is used to develop unit tests on Java classes. It is a class package that provides a variety of ways to test methods in a Java class.


Junit 4 Features
    • Simple annotations that provide the basic features of writing JUnit tests
    • Assert method (assert Methods) Compare test method Execution result value and expectation
    • @Ignore annotations, ignoring test methods or testing the execution of a class
    • Expected exception test
    • Timeout test, test method execution Time
    • Test components, run some test classes together
    • Parametric testing with different input parameter values test methods




Junit Eclipse Tutorials

Describes the relevant features of JUnit in the Eclipse IDE and how to use them. A sample of writing and running unit test code in the Eclipse IDE.

  • The JUnit view window in eclipse
  • Write and run JUnit test code in Eclipse
The JUnit view window in Eclipse


1. Open the JUnit view window in eclipse:



Window→show View→other






Java→junit→ok






The JUnit view window looks like this:






JUnit View Window Properties






Junit Window has the following characteristics:


  • ①runs: shows the total number of junit test cases (methods) executed
  • ②errors: Total number of test cases showing the result as Error/exception
  • ③failures: shows the total number of test case execution failures
  • ④failure trace: show error/exception or Failure trace
  • ⑤show Failures only: display of failed use case information only
  • ⑥scroll Lock:Scroll Lock
  • ⑦rerun test: Rerun testing case
  • ⑧rerun test–failure First: rerun the test case to execute the failed test case first
  • ⑨stop Junit Test Run: Stop unit Tests
  • ⑩test Run history: Displaying test case run histories
writing and running junit test cases in Eclipse


Writing and running JUnit test cases in eclipse will take the following three steps:


    • Create a Java Project
    • To create a JUnit test class
    • Executing the JUnit test class


1. Create a Java project



Create a Java project and create a Java class Arithmetic.java, and we'll write the JUnit test case to test it. Arithmetic.java:


package in.co.javatutorials;
 
/**
* @author javatutorials.co.in
*/
public class Arithmetic {
 
    public int add(int i, int j) {
        return i + j;
    }
}
2. Creating a JUnit test class


Right click Arithmetic.java→new→other ...






Java→junit→junit Test Case→next






Select the appropriate value in the New Junit Test Case window:


    • Select New Junit 4 test
    • Source folder Select Test Directory


Next






Select the method to test, in this case we select the Add (int, int) method and click Finish









Arithmetictest.java


package in.co.javatutorials;
 
import static org.junit.Assert.*;
 
import org.junit.Test;
 
public class ArithmeticTest {
 
    @Test
    public void testAdd() {
         Arithmetic arithmetic = new Arithmetic();
            int actualResult = arithmetic.add(1, 2);
            // example of test case success
            int expectedResult = 3;
            assertEquals(expectedResult, actualResult);
    }
}

3. Run the JUnit test class


To run the Arithmetictest.java test class in this example, the user needs:



Right-click Arithmetictest.java→run as→junit Test






As shown below, we can see the results of the run in the JUnit window in the left column of eclipse:






Source Download

Click I download the source code

Tutorial Directory Navigation




    • Junit Test Framework Introduction
    • Junit Eclipse Tutorials
    • Junit 4 Annotations
    • Junit 4 Assertion Method (Assert methods)
    • Junit 4 Parametric testing
    • Junit 4 Testing Suite (test suite)
    • Junit 4 Ignore testing (Ignoretest)
    • Junit 4 timeout tests (timeouttest)






--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------



The source of this article is Http://blog.csdn.net/luanlouis, reproduced please indicate the source, thank you!







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.