? 今天學習了JUNIT的知識跟大家分享一下。
? 首先是JUNIT的配置過程。
? 1.首先要下載eclipse,junit和junit_src網上都能找到對應的軟件。
? 2.安裝
? 第一步:打開Eclipse->Window->preferences.
? 第二步:Java->Build Path->Class Variable,新建兩個變量JUNIT和JUNIT_SRC,將junit和junit_src的路徑指定到這兩個變量中。如下圖所示:
?
? 第三步:創建工程,右擊工程屬性->java bulid path->libraries->add variable
將新創建的變量JUNIT加載進去。
? 第四步:JUNIT->Sourse attachment,將創建的變量JUNIT_SRC加載進去。
? 3.測試
? 被測代碼:
? public class Hello {
public int number(){
return 4;
}
public boolean yesOrNo(){
return true;
}
}
? 測試代碼:
package com.mini;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class HelloTest extends TestCase{
?
int _expectedNumber;
boolean yesorno;
Hello _hello;
@Before
public void setUp() throws Exception {//測試前要調用的方法
_expectedNumber = 5;
yesorno = false;
_hello = new Hello();
}
@After
public void tearDown() throws Exception {//測試后要調用的方法
}
@Test
public void testMain() {//測試執行的方法
assertEquals(_expectedNumber, _hello.number());
//assertEquals(yesorno,_hello.yesOrNo());
}
}
?
? 首先是JUNIT的配置過程。
? 1.首先要下載eclipse,junit和junit_src網上都能找到對應的軟件。
? 2.安裝
? 第一步:打開Eclipse->Window->preferences.
? 第二步:Java->Build Path->Class Variable,新建兩個變量JUNIT和JUNIT_SRC,將junit和junit_src的路徑指定到這兩個變量中。如下圖所示:
?
? 第三步:創建工程,右擊工程屬性->java bulid path->libraries->add variable
將新創建的變量JUNIT加載進去。
? 第四步:JUNIT->Sourse attachment,將創建的變量JUNIT_SRC加載進去。
? 3.測試
? 被測代碼:
? public class Hello {
public int number(){
return 4;
}
public boolean yesOrNo(){
return true;
}
}
? 測試代碼:
package com.mini;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class HelloTest extends TestCase{
?
int _expectedNumber;
boolean yesorno;
Hello _hello;
@Before
public void setUp() throws Exception {//測試前要調用的方法
_expectedNumber = 5;
yesorno = false;
_hello = new Hello();
}
@After
public void tearDown() throws Exception {//測試后要調用的方法
}
@Test
public void testMain() {//測試執行的方法
assertEquals(_expectedNumber, _hello.number());
//assertEquals(yesorno,_hello.yesOrNo());
}
}
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
