Difference between revisions of "Tópicos sobre escrita de testes com JUnit/Testing the blobbiness of blobs"

From Wiki**3

< Tópicos sobre escrita de testes com JUnit
m (Como compilar?)
Line 1: Line 1:
public class Blob {
+
public class Blob {
 
   int _radius;
 
   int _radius;
 
   int _weight;
 
   int _weight;
Line 83: Line 83:
  
 
   javac Blob.java
 
   javac Blob.java
   javac -cp path/to/file/junit.jar BlobTest.java
+
   javac -cp path/to/file/junit.jar:. BlobTest.java
  
 
== Como executar os testes? ==
 
== Como executar os testes? ==
  
 
   java -cp path/to/file/junit.jar junit.textui.TestRunner BlobTest
 
   java -cp path/to/file/junit.jar junit.textui.TestRunner BlobTest

Revision as of 17:51, 22 October 2007

public class Blob {

 int _radius;
 int _weight;

 public Blob(int r, int w) {
   _radius = r;
   _weight = w;
 }

 public int getRadius() { return _radius; }
 public int getWeight() { return _weight; }

 public boolean equals(Blob b) {
   if (b == null) return false;
   return _radius == b.getRadius() && _weight == b.getWeight();
 }

 public boolean equals(Object o) {
   return o instanceof Blob && equals((Blob)o);
 }

 public Blob blob(Blob b) {
   if (_radius < b.getRadius() || _weight < b.getWeight()) return this;
   return new Blob(_radius + b.getRadius(), _weight + b.getWeight());
 }

}

The test class:

import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class BlobTest extends TestCase {

 private Blob _smallBlob;
 private Blob _largeBlob;

 public BlobTest() {}
 public BlobTest(String s) { super(s); }

 protected void setUp() {
   _smallBlob = new Blob(1, 2);
   _largeBlob = new Blob(100, 200);
 }

 public void testEquals() {
   Assert.assertTrue(!_smallBlob.equals(null));
   Assert.assertTrue(!_largeBlob.equals(null));
   Assert.assertEquals(_smallBlob, new Blob(1, 2));
   Assert.assertEquals(_largeBlob, new Blob(100, 200));
   Assert.assertFalse(_smallBlob.equals(_largeBlob));
   Assert.assertFalse(_largeBlob.equals(_smallBlob));
 }

 public void testBlobbiness() {
   Blob b1 = new Blob(101, 202);
   Blob b2 = _largeBlob.blob(_smallBlob);
   Blob b3 = _smallBlob.blob(_largeBlob);
   Assert.assertEquals(b1, b2);
   Assert.assertEquals(_smallBlob, b3);
 }

 public static Test suite() {
   TestSuite suite = new TestSuite();
   suite.addTest(new BlobTest("test equalities") {
                   protected void runTest() { testEquals(); }
                 });
   suite.addTest(new BlobTest("test blobbiness") {
                   protected void runTest() { testBlobbiness(); }
                 });
   suite.addTest(new BlobTest("testBlobbiness"));
   // Failure: method "smallTest" does not exist
   // suite.addTest(new BlobTest("smallTest"));  
   return suite;
 }

}  // end of BlobTest

Como compilar?

 javac Blob.java
 javac -cp path/to/file/junit.jar:. BlobTest.java

Como executar os testes?

 java -cp path/to/file/junit.jar junit.textui.TestRunner BlobTest