單元測試之Junit

2021-07-14 00:22:11 字數 1118 閱讀 3311

junit是sun公司提供的一款專業做單元測試的框架。

我們通常對乙個類進行測試需要自己寫個main函式,但是可以右鍵包名選中junit test case.

cat類

package cn.itcast.junit;

public class cat

public void count(int num)

system.out.println("結果是"+result);

}}

測試cat類

package cn.itcast.junit;

import static org.junit.assert.*;

import org.junit.test;

public class testjunit1

@test

public void testcount()

}

建立junit測試時可以選擇setupbeforeclass()/teardownafterclass以及setup()/teardown()

前者只執行以此,後者測試幾個函式執行幾次

package cn.itcast.junit;

import static org.junit.assert.*;

import org.junit.after;

import org.junit.before;

import org.junit.test;

public class testjunit2

//teardown函式用於**資源

@after

public void teardown() throws exception

@test

public void testcry()

@test

public void testcount()

}

執行結果

setup ...

小貓 咪咪叫!

teardwon

setup ...

結果是55

teardwon

Junit 單元測試

測試類package com.lin.music item import android.content.contentresolver import android.content.context import android.database.cursor import android.net....

Junit單元測試

最近在寫一模組的 不能再像原來一樣不認真考慮測試了,因為看看junit如何寫單元測試,這裡作一些筆記。2.關於使用junit的目的。主要有四種 摘自某參考資料。對此,我覺得我是想測試某個類中的某幾個方法,因為覺得這幾個方法比較容易出問題,不過這樣是顯得有些不嚴謹了。其實往往測關鍵方法的時候,其中也都...

Junit單元測試

書寫規範 包 寫在.test包下或者.junit包下 類命名規範 xxtest 測試方法規範 命名規範 test xx 其他規範 返回值為void 空參如何使用junit單元測試?在需要測試的方法上加上 test註解,ctrl 1導包 test public void testgetclient j...