Java by API/junit.framework/TestCase

Материал из Java эксперт
Перейти к: навигация, поиск

Assert: assertEquals(int expected, int actual)

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Assert: assertEquals(Object expected, Object actual)

   <source lang="java">

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));
 }

}

 </source>
   
  
 
  



Assert: assertEquals(String expected, String actual)

   <source lang="java">

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;
 }

}

 </source>
   
  
 
  



Assert: assertEquals(String message, double expected, double actual, double precision)

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Assert: assertFalse(String message, boolean value)

   <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>
   
  
 
  



Assert: assertNotSame(String message, Object expected, Object 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>
   
  
 
  



Assert: assertNull(String message, Object value)

   <source lang="java">

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>
   
  
 
  



Assert: assertSame(String message, Object expected, Object 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>
   
  
 
  



Assert: assertTrue(String message, boolean value)

   <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>
   
  
 
  



Assert.fail(String message)

   <source lang="java">

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;
 }

}

 </source>
   
  
 
  



extends TestCase

   <source lang="java">

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;
 }

}

 </source>