Software testing (Junit3 JUNIT4)

Source: Internet
Author: User

1. Overview

2. Software Testing

3. The JUnit framework

4. Assert

5, TestCase class

6. Create the Tested class

 package com.ljb.junit;/** *  tested Class (import Junit.jar)  *  @author  ljb * @ version 2015 March 13  */public class Calculator { //  Results  private  static int result;  //   public void add  (int n )   {  result += n; }  //  minus  public void minus  (int  n)  {  result -= n; }  //  by  public void mult   (int n)  {  result *= n; }  //  except  public void  div  (int n)  {  if  (n == 0)  {    SYSTEM.OUT.PRINTLN ("Divisor cannot be 0!");   } else {   result /= n;  }  //  zeroing  public void clear  ()  {  result = 0; }  //  Obtain the result of the Operation &NBsp;public int getresult  ()  {  return result; }} 

7, creating test classes

package com.ljb.junit;import junit.framework.assert;import junit.framework.testcase;/** *   Create tested Classes  *  @author  LJB *  @version  2015 March 13  */public class  calculatortest extends testcase { //  creating a tested class object  Calculator calc =  New calculator ();  protected void setup ()  throws Exception {   Super.setup ();   system.out.println ("Pre-Test ...");   calc.clear (); } protected  Void teardown ()  throws exception {  super.teardown ();   System.out.println ("After test ...");  } public void testadd ()  {  calc.add (3);   calc.add (2);   int result = calc.getresult ();   assert.assertequals (5,  result);  } public void testminus ()  {  calc.minus (1);   Calc.minus (2);   int resulT = calc.getresult ();   assert.assertequals ( -3, result);  } public void  testmult ()  {  calc.add (1);   calc.mult (2);   int result =  calc.getresult ();   assert.assertequals (2, result); } public void  Testdiv ()  {  calc.add (5);   calc.div (2);  int result =  Calc.getresult ();   system.out.println (result);   assert.assertequals (2, result);  }}

Note: Removing an assert is also possible because TestCase inherits the Assert class

Operation Result:

Before the test ...
After the test ...
Before the test ...
After the test ...
Before the test ...
After the test ...
Before the test ...
2
After the test ...
8, Junit4 commonly used annotations

9. Abnormal test

10. Time Test

Software testing (Junit3 JUNIT4)

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.