Arrays和String单元测试 20175301

要求

在IDEA中以TDD的方式对String类和Arrays类进行学习

一、String类相关方法的单元测试

1.ChatAt的测试

代码:
import org.junit.Test;
import junit.framework.TestCase;
public class Char extends TestCase{
String s = new String("aaaazdxzds");
@Test
public void testcharAt() throws Exception {
assertEquals('a',s.charAt(0));//正常
assertEquals('下', new String("天下").charAt(1));//一个汉字占两个字节一个字符
assertEquals('e', new String("abcd e").charAt(5));//正常
assertEquals(' ', new String("abcd e").charAt(4));//边界
//assertEquals(' ', new String("abcd e").charAt(6));//error
//assertEquals('下', new String("天下").charAt(1));//error
} }

结果截图:

Arrays和String单元测试 20175301

一共测试了四种通过情况,两种错误情况。

2. split方法测试

测试代码(包括整个String类)

import org.junit.Assert;
import org.junit.Test;
import junit.framework.TestCase;
public class CharTest {
String s = new String("aaaazdxzds");
String s1= new String("my guiding-moon-light");
@Test
public void testcharAt() throws Exception {
Assert.assertEquals('a', s.charAt(0));//正常
Assert.assertEquals('下', new String("天下").charAt(1));//一个汉字占两个字节一个字符
Assert.assertEquals('e', new String("abcd e").charAt(5));//正常
Assert.assertEquals(' ', new String("abcd e").charAt(4));//边界
//assertEquals(' ', new String("abcd e").charAt(6));//error
//assertEquals('下', new String("天下").charAt(1));//error
} @Test
public void TestCase() throws Exception{
Assert.assertEquals("my", s1.split(" ")[0]);//正常情况
Assert.assertEquals("", s1.split("[a-z]+")[0]);//边界情况
Assert.assertEquals("guiding-moon-light", s1.split(" ")[1]);//边界情况
//assertEquals(" ",s1.split(" ")[-1]);//error
//assertEquals(" ",s1.split(" ")[100]);//error
}
}

测试了两种正常情况两种异常情况一个超出界限,一个小于0

实验截图:

Arrays和String单元测试 20175301

一、Arrays类相关方法的单元测试

1,sort方法

代码:

import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
import junit.framework.TestCase;
import java.util.Arrays; public class ArraysTest extends TestCase {
String[] s = {"w", "d", "n", "m", "d"};
int[] i = {2, 1, 4, 5, 3};
char[] p = {'a', 'b', 'c'}; @Test
public void testSort() {
Arrays.sort(i);
assertEquals(1, i[0]);
assertEquals(2, i[1]);
assertEquals(3, i[2]);
assertEquals(4, i[3]);
assertEquals(5, i[4]);
//assertEquals(5,i[3]); //
Arrays.sort(p);
assertEquals("[a, b, c]",Arrays.toString(p));
Arrays.sort(s);
assertEquals("[d, d, m, n, w]",Arrays.toString(s)); }
}

Arrays和String单元测试 20175301

2,binarySearch方法

代码:

import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
import junit.framework.TestCase;
import java.util.Arrays;
import static java.util.Arrays.binarySearch; public class ArraysTest extends TestCase {
String[] s = {"w", "d", "n", "m", "d"};
int[] i = {2, 1, 4, 5, 3};
char[] p = {'a', 'b', 'c'}; @Test
public void testSort() {
Arrays.sort(i);
assertEquals(1, i[0]);
assertEquals(2, i[1]);
assertEquals(3, i[2]);
assertEquals(4, i[3]);
assertEquals(5, i[4]);
//assertEquals(5,i[3]); //
Arrays.sort(p);
assertEquals("[a, b, c]",Arrays.toString(p));
Arrays.sort(s);
assertEquals("[d, d, m, n, w]",Arrays.toString(s));
}
public void testBinarySearch(){
Arrays.sort(i);
assertEquals(0 ,Arrays.binarySearch(i,1));//正常情况
//assertEquals(-6,Arrays.binarySearch(i,100));//error
//assertEquals(-1,Arrays.binarySearch(i,-1));//error
}
}

Arrays和String单元测试 20175301

上一篇:Python文件基础操作(IO入门1)


下一篇:VS2012+LUA环境搭建