Difference between revisions of "Tópicos sobre escrita de testes com JUnit/Toy Example (JUnit 3.8.x)"

From Wiki**3

< Tópicos sobre escrita de testes com JUnit
 
(2 intermediate revisions by the same user not shown)
Line 37: Line 37:
 
  }
 
  }
 
</java5>
 
</java5>
 +
 +
[[category:Ensino]]
 +
[[category:PO]]
 +
[[category:PO Exemplos]]
 +
[[category:Java]]

Latest revision as of 20:38, 4 November 2013

The following toy example tests a few situations (some fail, to illustrate all cases). <java5>

import junit.framework.*;
import java.util.ArrayList;

public class HelloWorld3 extends TestCase {

 public void testMultiplication() {
   // Testing if 3*2=6:
   assertEquals("Multiplication", 6, 3*2);
 }

 public void testCreativeMultiplication() {
   // Testing if 3*2=6:
   assertEquals("Creative multiplication", 7, 3*2);
 }

 public void testException1() {
   try {
     ArrayList<Object> emptyList = new ArrayList<Object>();
     Object o = emptyList.get(0);   // throws IndexOutOfBoundsException
     fail("no exception");
   }
   catch (Exception e) {
   }
 }

 public void testException2() {
   try {
     int i = 0;
   }
   catch (Exception e) {
     fail("exception");
   }
 }

}

</java5>