1.Maven Dependency
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.fool.junit</groupId> <artifactId>junit3</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>junit3</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.2</version> <scope>test</scope> </dependency> </dependencies> </project>
2.Project Directory
3.src/main/java
Calculator.java
package org.fool.junit;
public class Calculator
{
public int add(int a, int b)
{
return a + b;
}
public int subtract(int a, int b)
{
return a - b;
}
public int multiply(int a, int b)
{
return a * b;
}
public int divide(int a, int b) throws Exception
{
if (0 == b)
{
throw new Exception("除数不能为0");
}
return a / b;
}
}
Calculator2.java
package org.fool.junit;
public class Calculator2
{
@SuppressWarnings("unused")
private int add(int a, int b)
{
return a + b;
}
}
Largest.java
package org.fool.junit;
public class Largest
{
public int getLargest(int[] array) throws Exception
{
if (null == array || 0 == array.length)
{
throw new Exception("数组不能为空!");
}
int result = array[0];
for (int i = 0; i < array.length; i++)
{
if (result < array[i])
{
result = array[i];
}
}
return result;
}
}
DeleteAll.java
package org.fool.junit;
import java.io.File;
public class DeleteAll
{
public static void deleteAll(File file)
{
if (file.isFile() || file.list().length == 0)
{
file.delete();
}
else
{
File[] files = file.listFiles();
for (File f : files)
{
deleteAll(f);
f.delete();
}
}
}
public static void main(String[] args)
{
deleteAll(new File("c:\\temp1"));
}
}
MyStack.java
package org.fool.junit;
public class MyStack
{
private String[] elements;
private int nextIndex;
public MyStack()
{
elements = new String[100];
nextIndex = 0;
}
public void push(String element) throws Exception
{
if (100 == nextIndex)
{
throw new Exception("数组越界异常!");
}
elements[nextIndex++] = element;
}
public String pop() throws Exception
{
if (0 == nextIndex)
{
throw new Exception("数组越界异常!");
}
return elements[--nextIndex];
}
public void delete(int n) throws Exception
{
if (nextIndex - n < 0)
{
throw new Exception("数组越界异常!");
}
nextIndex -= n;
}
public String top() throws Exception
{
if (0 == nextIndex)
{
throw new Exception("数组越界异常!");
}
return elements[nextIndex - 1];
}
}
4.src/test/java
CalculatorTest.java
package org.fool.junit;
import junit.framework.Assert;
import junit.framework.TestCase;
/**
* 测试类必须要继承于TestCase父类
*
* 在JUnit3.8中,测试方法需要满足如下原则:
* 1. public的
* 2. void的
* 3. 无方法参数
* 4. 方法名称必须以test开头
*/
public class CalculatorTest extends TestCase
{
private Calculator cal;
public CalculatorTest(String name)
{
super(name);
}
@Override
public void setUp() throws Exception
{
cal = new Calculator();
}
@Override
public void tearDown() throws Exception
{
}
public void testAdd()
{
int result = cal.add(1, 2);
Assert.assertEquals(3, result);
}
public void testSubtract()
{
int result = cal.subtract(1, 2);
Assert.assertEquals(-1, result);
}
public void testMultiply()
{
int result = cal.multiply(2, 3);
Assert.assertEquals(6, result);
}
public void testDivide()
{
int result = 0;
try
{
result = cal.divide(6, 2);
}
catch (Exception e)
{
e.printStackTrace();
Assert.fail();
}
Assert.assertEquals(3, result);
}
public void testDivideByZero()
{
Throwable tx = null;
try
{
cal.divide(6, 0);
Assert.fail("测试失败");
}
catch (Exception e)
{
tx = e;
}
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("除数不能为0", tx.getMessage());
}
public static void main(String[] args)
{
junit.awtui.TestRunner.run(CalculatorTest.class);
// junit.textui.TestRunner.run(CalculatorTest.class);
}
}
Calculator2Test.java
package org.fool.junit;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import junit.framework.Assert;
import junit.framework.TestCase;
public class Calculator2Test extends TestCase
{
// 使用反射在测试类中调用目标类的私有方法(推荐)
public void testAdd()
{
try
{
Calculator2 cal2 = new Calculator2();
Class<Calculator2> clazz = Calculator2.class;
Method method = clazz.getDeclaredMethod("add", new Class[] {
Integer.TYPE, Integer.TYPE });
method.setAccessible(true);
Object result = method.invoke(cal2, new Object[] { 2, 3 });
Assert.assertEquals(5, result);
}
catch (NoSuchMethodException | SecurityException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException e)
{
e.printStackTrace();
}
}
}
LargestTest.java
package org.fool.junit;
import junit.framework.Assert;
import junit.framework.TestCase;
public class LargestTest extends TestCase
{
private Largest largest;
@Override
public void setUp() throws Exception
{
largest = new Largest();
}
@Override
public void tearDown() throws Exception
{
}
public void testGetLargest()
{
int[] array = { 1, 9, -10, -20, 23, 34 };
int result = 0;
try
{
result = largest.getLargest(array);
}
catch (Exception e)
{
Assert.fail("测试失败");
}
Assert.assertEquals(34, result);
}
public void testGetLargest2()
{
Throwable throwable = null;
int[] array = {};
try
{
largest.getLargest(array);
Assert.fail("测试失败");
}
catch (Exception e)
{
throwable = e;
}
Assert.assertNotNull(throwable);
Assert.assertEquals(Exception.class, throwable.getClass());
Assert.assertEquals("数组不能为空!", throwable.getMessage());
}
public void testGetLargest3()
{
Throwable throwable = null;
int[] array = null;
try
{
largest.getLargest(array);
Assert.fail();
}
catch (Exception e)
{
throwable = e;
}
Assert.assertNotNull(throwable);
Assert.assertEquals(Exception.class, throwable.getClass());
Assert.assertEquals("数组不能为空!", throwable.getMessage());
}
}
DeleteAllTest.java
package org.fool.junit;
import java.io.File;
import java.io.IOException;
import junit.framework.Assert;
import junit.framework.TestCase;
public class DeleteAllTest extends TestCase
{
public void testDeleteAll()
{
File file = null;
try
{
file = new File("test.txt");
file.createNewFile();
DeleteAll.deleteAll(file);
}
catch (IOException e)
{
Assert.fail();
}
boolean isExist = file.exists();
Assert.assertFalse(isExist);
}
/**
* 构造的是一个目录结构,其结构表示如下
*
* d
* / \
* / \
* d1 d2
* / \
* / \
*s1 s2
*/
public void testDeleteAll2()
{
File directory = null;
try
{
directory = new File("dir");
directory.mkdir();
File file1 = new File(directory, "file1.txt");
File file2 = new File(directory, "file2.txt");
file1.createNewFile();
file2.createNewFile();
File d1 = new File(directory, "d1");
File d2 = new File(directory, "d2");
d1.mkdir();
d2.mkdir();
File subFile1 = new File(d1, "subFile1.txt");
File subFile2 = new File(d2, "subFile2.txt");
subFile1.createNewFile();
subFile2.createNewFile();
DeleteAll.deleteAll(directory);
}
catch (IOException e)
{
e.printStackTrace();
}
Assert.assertNotNull(directory);
String[] names = directory.list();
Assert.assertEquals(0, names.length);
directory.delete();
}
}
MyStackTest.java
package org.fool.junit;
import junit.framework.Assert;
import junit.framework.TestCase;
public class MyStackTest extends TestCase
{
private MyStack myStack;
@Override
public void setUp() throws Exception
{
myStack = new MyStack();
}
public void testPush()
{
try
{
myStack.push("hello world");
}
catch (Exception e)
{
Assert.fail();
}
String result = null;
try
{
result = myStack.pop();
}
catch (Exception e)
{
}
Assert.assertEquals("hello world", result);
}
public void testPush2()
{
for (int i = 0; i < 100; i++)
{
try
{
myStack.push(i + "");
}
catch (Exception e)
{
Assert.fail();
}
}
for (int i = 0; i < 100; i++)
{
String result = null;
try
{
result = myStack.pop();
}
catch (Exception e)
{
}
Assert.assertEquals((99 - i) + "", result);
}
}
public void testPush3()
{
Throwable tx = null;
try
{
for (int i = 0; i <= 100; i++)
{
myStack.push(i + "");
}
Assert.fail();
}
catch (Exception ex)
{
tx = ex;
}
assertData(tx);
}
public void testPop()
{
try
{
myStack.push("hello world");
}
catch (Exception e)
{
}
String result = null;
try
{
result = myStack.pop();
}
catch (Exception e)
{
Assert.fail();
}
Assert.assertEquals("hello world", result);
}
public void testPop2()
{
Throwable tx = null;
try
{
myStack.pop();
Assert.fail();
}
catch (Exception e)
{
tx = e;
}
assertData(tx);
}
public void testPop3()
{
Throwable tx = null;
try
{
myStack.push("hello");
}
catch (Exception e)
{
}
try
{
myStack.pop();
myStack.pop();
Assert.fail();
}
catch (Exception e)
{
tx = e;
}
assertData(tx);
}
public void testTop()
{
try
{
myStack.push("hello");
}
catch (Exception e)
{
}
String result = null;
try
{
result = myStack.top();
}
catch (Exception e)
{
Assert.fail();
}
Assert.assertEquals("hello", result);
}
public void testTop2()
{
Throwable tx = null;
try
{
myStack.top();
Assert.fail();
}
catch (Exception e)
{
tx = e;
}
assertData(tx);
}
public void testDelete()
{
try
{
for (int i = 0; i < 10; i++)
{
myStack.push(i + "");
}
myStack.delete(10);
}
catch (Exception e)
{
Assert.fail();
}
}
public void testDelete2()
{
Throwable tx = null;
try
{
for (int i = 0; i < 10; i++)
{
myStack.push(i + "");
}
myStack.delete(11);
Assert.fail();
}
catch (Exception e)
{
tx = e;
}
assertData(tx);
}
private void assertData(Throwable tx)
{
Assert.assertNotNull(tx);
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("数组越界异常!", tx.getMessage());
}
}
TestAll.java
package org.fool.junit;
import junit.extensions.RepeatedTest;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestAll extends TestCase
{
public static Test suite()
{
TestSuite suite = new TestSuite();
suite.addTestSuite(CalculatorTest.class);
suite.addTestSuite(Calculator2Test.class);
suite.addTestSuite(DeleteAllTest.class);
suite.addTestSuite(LargestTest.class);
suite.addTestSuite(MyStackTest.class);
suite.addTest(new RepeatedTest(new CalculatorTest("testSubtract"), 20));
return suite;
}
}