Java Tutorial/JUnit/assert
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 assertEquals([String message],expected,actual)
- 2 assertEquals([String message],expected,actual,tolerance)
- 3 assertFalse([String message], boolean condition)
- 4 assertNotSame([String message], expected, actual)
- 5 assertNull([String message], java.lang.Object object)
- 6 assertSame([String message], expected, actual)
- 7 assertTrue([String message], boolean condition)
- 8 Ensure that they are equal
assertEquals([String message],expected,actual)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testOrder2() {
int[] arr = new int[3];
arr[0] = 8;
arr[1] = 9;
arr[2] = 7;
assertEquals(9, Largest.largest(arr));
}
}
class Largest {
/**
* Return the largest element in a list.
*
* @param list
* A list of integers
* @return The largest number in the given list
*/
public static int largest(int[] list) {
int index, max = Integer.MAX_VALUE;
for (index = 0; index < list.length - 1; index++) {
if (list[index] > max) {
max = list[index];
}
}
return max;
}
}
assertEquals([String message],expected,actual,tolerance)
check that the actual result is equal to 3.33,
but only look at the first two decimal places:
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertEquals("Should be 3 1/3", 3.33, 10.0/3.0, 0.01);
}
}
assertFalse([String message], boolean condition)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertFalse("should be same", true);
}
}
assertNotSame([String message], expected, actual)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertNotSame("should be same", "expected", "actual");
}
}
assertNull([String message], java.lang.Object object)
assertNotNull([String message], java.lang.Object object)
Asserts that the given object is null (or not null), failing otherwise. The message is optional.
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertNull("it should be null", null);
}
}
assertSame([String message], expected, actual)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertSame("should be same", "expected", "actual");
}
}
assertTrue([String message], boolean condition)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertTrue("should be same", true);
}
}
Ensure that they are equal
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testOrder() {
assertEquals(9, Largest.largest(new int[] { 8, 9, 7 }));
}
}
class Largest {
/**
* Return the largest element in a list.
*
* @param list
* A list of integers
* @return The largest number in the given list
*/
public static int largest(int[] list) {
int index, max = Integer.MAX_VALUE;
for (index = 0; index < list.length - 1; index++) {
if (list[index] > max) {
max = list[index];
}
}
return max;
}
}