Java Tutorial/JUnit/assert
Содержание
- 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)
<source lang="java">
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; }
}</source>
assertEquals([String message],expected,actual,tolerance)
<source lang="java">
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); }
}</source>
assertFalse([String message], boolean condition)
<source lang="java">
import junit.framework.TestCase; public class TestLargest extends TestCase {
public TestLargest(String name) { super(name); } public void testEmpty() { assertFalse("should be same", true); }
}</source>
assertNotSame([String message], expected, actual)
<source lang="java">
import junit.framework.TestCase; public class TestLargest extends TestCase {
public TestLargest(String name) { super(name); } public void testEmpty() { assertNotSame("should be same", "expected", "actual"); }
}</source>
assertNull([String message], java.lang.Object object)
<source lang="java">
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); }
}</source>
assertSame([String message], expected, actual)
<source lang="java">
import junit.framework.TestCase; public class TestLargest extends TestCase {
public TestLargest(String name) { super(name); } public void testEmpty() { assertSame("should be same", "expected", "actual"); }
}</source>
assertTrue([String message], boolean condition)
<source lang="java">
import junit.framework.TestCase; public class TestLargest extends TestCase {
public TestLargest(String name) { super(name); } public void testEmpty() { assertTrue("should be same", true); }
}</source>
Ensure that they are equal
<source lang="java">
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; }
}</source>