Junit4 Quick Start

1 What is xUnit

xUnit is a test framework based on test-driven development
pythonUnit for python testing
CppUnit for C++ testing
JUnit for java testing
 
2. What is JUnit
Junit is a framework for testing Java code.
 
3 Why use JUnit
Purposeful testing helps us avoid bugs in our code.
 
Four junit official website
jar package download address:
 
Five examples
1. Create the junit4Demo project with the eclipse project.
2. Create the caculate.java program
  1. package com.imooc.util;
  2. publicclassCaculate{
  3. publicint add(int a,int b){
  4. return a+b;
  5. }
  6. publicint subtract(int a,int b){
  7. return a-b;
  8. }
  9. publicint multiply(int a ,int b){
  10. return a*b;
  11. }
  12. publicint divide(int a,int b){
  13. return a/b;
  14. }
  15. }
3. Import the jar package of Junit
选择 junit4Demo,右键【Build Path】->【Add library】->【JUnit】->【Next】->【JUnit4】->【Finish】
4. Create test code
Select the com.imooc.util package, right-click [new] -> [Junit Test Case].
Name it CaculateTest.
code show as below
  1. package com.imooc.util;
  2. importstatic org.junit.Assert.*;
  3. import org.junit.Test;
  4. publicclassCaculateTest{
  5. @Test
  6. publicvoid add(){
  7. assertEquals(6,newCaculate().add(3,3));
  8. }
  9. }
5. Execute the test
Right click【 CaculateTest.java 】->【Run as】->【Junit test】
6. Execution results

 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327057648&siteId=291194637
Recommended