簡單了解junit的使用

2022-09-06 16:06:12 字數 4111 閱讀 1161

普通使用:

在沒有用測試框架之前,我們要用乙個main方法來跑**,而有了像junit這樣的測試框架後,就可以不用次次寫個main方法了。

直接上**,注釋裡有說明:

package

com.stupayment;

import

static

org.junit.assert.assertequals;

import

org.junit.after;

import

org.junit.afterclass;

import

org.junit.before;

import

org.junit.beforeclass;

import

org.junit.ignore;

import

org.junit.test;

/*** 簡單地演示了junit的基本用法

* @author

85060 *

*/public

class

mytest

public

void

get2(numbertest num)

@ignore

//這個注釋下的方法是會被忽略暫時不會執行的

public

void

ignoremethod()

@before

//這個注釋是在每個測試方法之前都會跑的,也就是每個@test的方法跑之前

public

void

saybefore()

@after

//這個注釋是在每個測試方法之後都會跑的,也就是每個@test的方法跑之後

public

void

sayafter()

@beforeclass

//這個注釋是在整個測試開始前會跑的

public

static

void

befc()

@afterclass

//這個注釋是在整個測試結束後會跑的

public

static

void

aftc()

@test

public

void

test1()

@test

public

void

test2() else if(numbertest.a == 9)

*/get3(numbertest);

assertequals(729, numbertest.a);

}}class

numbertest

}

執行的方法就直接run as junit test,執行結果:

在springboot中使用junit:

這裡只是提供乙個簡單的測試例子,可以用來單獨測試service或者是你的dao介面。

關於@runwiht:

@runwith

在junit中有很多個runner,他們負責呼叫你的測試**,每乙個runner都有各自的特殊功能,你要根據需要選擇不同的runner來執行你的測試**。

如果我們只是簡單的做普通j**a測試,不涉及spring web專案,你可以省略@runwith註解,這樣系統會自動使用預設runner來執行你的**。

也是直接上**,注釋裡面有說明:

package

com.stupayment;

import

static

org.junit.assert.assertarrayequals;

import

j**a.util.hashmap;

import

j**a.util.list;

import

j**a.util.map;

import

org.junit.test;

import

org.junit.runner.runwith;

import

org.springframework.beans.factory.annotation.autowired;

import

org.springframework.boot.test.context.springboottest;

import

org.springframework.test.context.junit4.springrunner;

import

import

com.stupayment.entiy.user;

/*** 演示了springboot環境下的junit測試

* @author

85060 *

*/@runwith(springrunner.

class) //

springjunit支援,由此引入spring-test框架支援!

@springboottest //

提供spring環境

public

class

mytest2 ,

newobject);}

}

junit的引數化測試:其實就是,當有乙個方法,要用多組引數來驗證其正確性,這個時候你就可以考慮用這個@runwith(parameterized.class)註解

上**,**裡面有註解解釋:

package

com.stupayment;

import

static

org.junit.assert.asserttrue;

import

j**a.util.arrays;

import

j**a.util.collection;

import

org.junit.test;

import

org.junit.runner.runwith;

import

org.junit.runners.parameterized;

import

org.junit.runners.parameterized.parameters;

/*** 演示一下引數測試

* @author

85060 *

*/@runwith(parameterized.

class) //

一定要用這個註解

public

class

mytest3

/*** 必須提供@parameters方法,方法簽名必須是public static collection,不能有引數,

* 並且collection元素必須是相同長度的陣列。

* 同事陣列的長度必須與唯一的公共建構函式的引數數量相匹配。

*/@parameters

public

static collection>data() ,

,});

//意思其實就是拿這些資料去用mytest3(string name, boolean result)一直new

}

@test

//真正測試也就是跑的那個方法,用@parameters方法返回的那些引數每組都用mytest3(string name, bolean result)

//然後再跑下面這個方法。

public

void

test()

}

測試結果的效果:

可以看到,如果有錯的話會有藍色的叉叉,然後點選這個錯誤,在failure trace那可以看到簡單的錯誤說明。

Junit工具的簡單使用

windows平台,工具為intellij 使用junit完成一次向 helloworld 那樣基礎的測試 導包 需要兩個類 需要測試的類和進行測試的類 import org.junit.test import static org.junit.assert.assertequals public ...

關於JUnit測試的最簡單使用

只是寫乙個小入門,簡單輸出文字,沒啥?首先,建立乙個messageutil類,用來測試 package com.test.jiao public class messageutil public string printmessage 然後,建立testcase 類,命名為testjunit,測試時...

Junit的簡單例子

第一步 被測試類 helloworld 類 public class helloworld calculator 類 public class calculator public int minus int a,int b public int multiply int a,int b public...