Java by API/junit.framework/TestCase — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
- 1 Assert: assertEquals(int expected, int actual)
- 2 Assert: assertEquals(Object expected, Object actual)
- 3 Assert: assertEquals(String expected, String actual)
- 4 Assert: assertEquals(String message, double expected, double actual, double precision)
- 5 Assert: assertFalse(String message, boolean value)
- 6 Assert: assertNotSame(String message, Object expected, Object actual)
- 7 Assert: assertNull(String message, Object value)
- 8 Assert: assertSame(String message, Object expected, Object actual)
- 9 Assert: assertTrue(String message, boolean value)
- 10 Assert.fail(String message)
- 11 extends TestCase
Assert: assertEquals(int expected, int actual)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testAdds() {
assertEquals(2, 1 + 1);
assertEquals(4, 2 + 2);
assertEquals(-8, -12 + 4);
}
}
Assert: assertEquals(Object expected, Object actual)
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;
import org.junit.Test;
public class Main{
public static void main (String... args) {
junit.textui.TestRunner.run (suite());
}
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(Main.class);
}
@Test public void testCopy() {
assertEquals(12, 12);
assertEquals(12L, 12L);
assertEquals(new Long(12), new Long(12));
}
}
Assert: assertEquals(String expected, String actual)
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class Main extends TestCase {
public static void main (String... args) {
junit.textui.TestRunner.run (suite());
}
public Main(String name) {
super(name);
}
public void testPassNullsToConstructor() {
try {
Person p = new Person(null, null);
fail("Expected IllegalArgumentException because both args are null");
} catch (IllegalArgumentException expected) {
}
}
public void testNullsInName() {
fail("sample failure");
Person p = new Person(null, "lastName");
assertEquals("lastName", p.getFullName());
p = new Person("Tanner", null);
assertEquals("Tanner ?", p.getFullName());
}
public static void oneTimeSetup() {
System.out.println("oneTimeSetUp");
}
public static void oneTimeTearDown() {
System.out.println("oneTimeTearDown");
}
public static Test suite() {
TestSetup setup = new TestSetup(new TestSuite(Main.class)) {
protected void setUp() throws Exception {
oneTimeSetup();
}
protected void tearDown() throws Exception {
oneTimeTearDown();
}
};
return setup;
}
}
class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
if (firstName == null && lastName == null) {
throw new IllegalArgumentException("Both names cannot be null");
}
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
String first = (this.firstName != null) ? this.firstName : "?";
String last = (this.lastName != null) ? this.lastName : "?";
return first + " " + last;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
Assert: assertEquals(String message, double expected, double actual, double precision)
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;
import org.junit.Test;
public class Main{
public static void main (String... args) {
junit.textui.TestRunner.run (suite());
}
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(Main.class);
}
@Test public void testCopy() {
assertEquals("Capacity", 11.991, 11.99, 0.1);
}
}
Assert: assertFalse(String message, boolean value)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertFalse("should be same", true);
}
}
Assert: assertNotSame(String message, Object expected, Object 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");
}
}
Assert: assertNull(String message, Object value)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertNull("it should be null", null);
}
}
Assert: assertSame(String message, Object expected, Object 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");
}
}
Assert: assertTrue(String message, boolean value)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
assertTrue("should be same", true);
}
}
Assert.fail(String message)
import junit.framework.TestCase;
public class TestLargest extends TestCase {
public TestLargest(String name) {
super(name);
}
public void testEmpty() {
try {
Largest.largest(new int[] {});
fail("Should have thrown an exception");
} catch (RuntimeException e) {
assertTrue(true);
}
}
}
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;
}
}
extends TestCase
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class Main extends TestCase {
public static void main (String... args) {
junit.textui.TestRunner.run (suite());
}
public Main(String name) {
super(name);
}
public void testPassNullsToConstructor() {
try {
Person p = new Person(null, null);
fail("Expected IllegalArgumentException because both args are null");
} catch (IllegalArgumentException expected) {
}
}
public void testNullsInName() {
fail("sample failure");
Person p = new Person(null, "lastName");
assertEquals("lastName", p.getFullName());
p = new Person("Tanner", null);
assertEquals("Tanner ?", p.getFullName());
}
public static void oneTimeSetup() {
System.out.println("oneTimeSetUp");
}
public static void oneTimeTearDown() {
System.out.println("oneTimeTearDown");
}
public static Test suite() {
TestSetup setup = new TestSetup(new TestSuite(Main.class)) {
protected void setUp() throws Exception {
oneTimeSetup();
}
protected void tearDown() throws Exception {
oneTimeTearDown();
}
};
return setup;
}
}
class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
if (firstName == null && lastName == null) {
throw new IllegalArgumentException("Both names cannot be null");
}
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
String first = (this.firstName != null) ? this.firstName : "?";
String last = (this.lastName != null) ? this.lastName : "?";
return first + " " + last;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}