Identifier test sets implemented with JUnit

De Software testing
Ir para: navegação, pesquisa
package identifier;

import org.junit.Test;
import org.junit.Assert;

public class IdentifierTestCase
{
  protected Identifier id;

  @Test 
  public void validate1() {
    id = new Identifier();
    boolean result = id.validateIdentifier("Abcd5");
    Assert.assertEquals(true, result);
  }

  @Test
  public void validate2() {
    id = new Identifier();
    boolean result = id.validateIdentifier("x12345");
    Assert.assertEquals(true, result);
  }
}


Now using fixtures:

package identifier;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Assert;

public class IdentifierTestCase
{
  protected Identifier id;

  @Before
  public void setUp() {
    id = new Identifier();
  }

  @Test 
  public void validate1() {
    boolean result = id.validateIdentifier("Abcd5");
    Assert.assertEquals(true, result);
  }

  @Test
  public void validate2() {
    boolean result = id.validateIdentifier("x12345");
    Assert.assertEquals(true, result);
  }

  @Test
  public void validate3() {
    boolean result = id.validateIdentifier("&123");
    Assert.assertFalse(result);
  }

  @Test
  public void validate4() {
    boolean result = id.validateIdentifier("Inv@lido");
    Assert.assertFalse(result);
  }

  @Test
  public void validate5() {
    Assert.assertNotNull(id);
  }

  @Test(expected=IndexOutOfBoundsException.class)
  public void stringException() {
    String str = new String("JUnit Example");
    str.substring(30);
  }

  @Test(timeout=2000)
  public void looping() {
    boolean result = id.validateIdentifier("Abcd5");
    Assert.assertEquals(true, result);
  }

  @Ignore("Out of the program scope")
  @Test(expected=IndexOutOfBoundsException.class)
  public void stringException2() {
    String str = new String("JUnit Example");
    str.substring(30);
  }
}


And with a parameterized JUnit test case:

package identifier;

import org.junit.Assert;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.*;

import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class IdentifierParameterizedTestCase {

  private Identifier id;

  private String param;
  private boolean expected;

  @Before 
  public void initialize() {
    id = new Identifier();
  }

  @Parameters
  public static Collection data() {
    return Arrays.asList(new Object[][]{
      {"Abcd5",     true},
      {"x12345",    true},
      {"&123",      false},
      {"Inv@lido",  false}	
      });
  }

  public ParameterizedTestCase2(
      String param, boolean expected) {
    this.param = param;
    this.result = expected;
  }

  @Test
  public void validate() {
    boolean obtained = id.validateIdentifier(param);
    Assert.assertEquals(result, obtained);
  }
}

Finally, a JUnit test suite to run them all:

package identifier;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    IdentifierTestCase.class,
    IdentifierParameterizedTestCase.class
})

public class AllTests  {
}