搜档网
当前位置:搜档网 › Linux下用intellij idea使用JUnit进行代码单元测试

Linux下用intellij idea使用JUnit进行代码单元测试

Linux下用Intellij idea使用JUnit进行代码单元测试

今天看了一下《Java开放源码编程》一书,在intellij idea上进行了平生首个JUnit代码单元测试,步骤如下:

1建立两个包,com.test下建立三个测试类,xcl文件加下面建立两个类并被用于测试,其中TestParser是CsvPaser的测试类,TestTelephoneBook是TelephoneBook的测试类,使用MyTestSuite测试类可以一次执行所有的测试。

2编写代码如下:

com.xcl.CsvPaser代码如下:

package com.xcl;

import java.util.StringTokenizer;

/**

* Created by xcl on 15-12-24.

*/

public class CsvPaser {

private String[] currentLine;

public void parse(String input){

StringTokenizer tokenizer=new StringTokenizer(input,","); currentLine=new String[tokenizer.countTokens()];

for(int i=0;i

currentLine[i]=tokenizer.nextToken();

}

}

public String get(int columnIndex){

return currentLine[columnIndex];

}

}

com.xcl.TelephoneBool类代码如下:

package com.xcl;

import java.util.HashMap;

import java.util.Map;

/**

* Created by xcl on 15-12-24.

*/

public class TelephoneBook {

public Mapmap=new HashMap(); public TelephoneBook(Mapmap){

this.map=map;

}

public String getTelephone(String name){

return map.get(name);

}

}

com.test.TestCsvParser类如下:

package com.test;

import com.xcl.CsvPaser;

import junit.framework.Assert;

import junit.framework.TestCase;

import org.junit.Test;

/**

* Created by xcl on 15-12-24.

*/

public class TestParser extends TestCase{

@Test

public void testParseSimpleLine(){

CsvPaser paser=new CsvPaser();

paser.parse("Bill, Gates, 555-1234");

Assert.assertEquals("Bill", paser.get(0));

}

@Test

public void testMultiLines(){

try {

CsvPaser paser=new CsvPaser();

paser.parse("hello, world");

Assert.assertEquals("hello", paser.get(0));

paser.parse("xcl,你大爷");

Assert.assertEquals("xcl ", paser.get(0));

}catch (Exception e){

e.printStackTrace();

}

}

}

com.test.TestTelephoneBook类的代码如下:

package com.test;

import junit.framework.Assert;

import junit.framework.TestCase;

import org.junit.Test;

import java.util.HashMap;

import com.xcl.TelephoneBook;

/**

* Created by xcl on 15-12-24.

*/

public class TestTelephoneBook extends TestCase{//测试用类必须继承自junit.framework.TetstCase类

@Test//测试方法必须以test***的形式,并且此处可以加上该annotation

public void testTelephoneBook(){

HashMapmap=new HashMap();

map.put("xcl","189********");

map.put("ljc","189********");

TelephoneBook tb=new TelephoneBook(map);

//断言方法是assertEquals(Object expected,Object input);如果expected.equals(input)则表示断言成功,否则断言失败。

Assert.assertEquals("189********", tb.getTelephone("xcl"));

Assert.assertEquals("189********", tb.getTelephone("ljc"));

}

}

com.test.MyTestSuite类如下:

package com.test;

import junit.framework.Test;

import junit.framework.TestSuite;

/**

* Created by xcl on 15-12-24.

*/

public class MyTestSuite {

//请注意该方法是静态方法,并且其返回值是junit.framework.Test接口

public static Test suite(){

TestSuite su=new TestSuite();//为了使用TestSuite的实例方法addTestSuite必须创建一个TestSuite对象

su.addTestSuite(TestParser.class);

su.addTestSuite(TestTelephoneBook.class);

return su;

}

}

3测试结果如下:

一个测试用例导致了测试失败。

相关主题