Skip to main content
fix String class name
Source Link
David Harkness
  • 8.8k
  • 1
  • 26
  • 44

You could use a parameterized test:

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class MyParameterizedClassTest {

  private stringString promptNameOrg;

  public MyParameterizedClassTest(stringString testParameter, stringString expectedParameter) {
    this.promptNameOrg = testParameter;
    this.expected = expectedParameter;
  }

  // creates the test data
  @Parameters
  public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { "20129142\\1234", "1234" }, { "80808080\\159;20129142\\1234", "1234" } };
    return Arrays.asList(data);
  }

  @Test
  public void extractSimplePromptNameFindsLastPartOfString() {
    String simplePromptName = extractSimplePromptName(this.promptNameOrg);
    Assert.assertEquals(this.expected, simplePromptName);
  }

  // extractSimplePromptName omitted
}

There can only be one @Test in such a class, though.

More info can be found here.

You could use a parameterized test:

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class MyParameterizedClassTest {

  private string promptNameOrg;

  public MyParameterizedClassTest(string testParameter, string expectedParameter) {
    this.promptNameOrg = testParameter;
    this.expected = expectedParameter;
  }

  // creates the test data
  @Parameters
  public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { "20129142\\1234", "1234" }, { "80808080\\159;20129142\\1234", "1234" } };
    return Arrays.asList(data);
  }

  @Test
  public void extractSimplePromptNameFindsLastPartOfString() {
    String simplePromptName = extractSimplePromptName(this.promptNameOrg);
    Assert.assertEquals(this.expected, simplePromptName);
  }

  // extractSimplePromptName omitted
}

There can only be one @Test in such a class, though.

More info can be found here.

You could use a parameterized test:

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class MyParameterizedClassTest {

  private String promptNameOrg;

  public MyParameterizedClassTest(String testParameter, String expectedParameter) {
    this.promptNameOrg = testParameter;
    this.expected = expectedParameter;
  }

  // creates the test data
  @Parameters
  public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { "20129142\\1234", "1234" }, { "80808080\\159;20129142\\1234", "1234" } };
    return Arrays.asList(data);
  }

  @Test
  public void extractSimplePromptNameFindsLastPartOfString() {
    String simplePromptName = extractSimplePromptName(this.promptNameOrg);
    Assert.assertEquals(this.expected, simplePromptName);
  }

  // extractSimplePromptName omitted
}

There can only be one @Test in such a class, though.

More info can be found here.

added 12 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

You could use a parameterized test.:

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class MyParameterizedClassTest {

  private string promptNameOrg;

  public MyParameterizedClassTest(string testParameter, string expectedParameter) {
    this.promptNameOrg = testParameter;
    this.expected = expectedParameter;
  }

  // creates the test data
  @Parameters
  public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { "20129142\\1234", "1234" }, { "80808080\\159;20129142\\1234", "1234" } };
    return Arrays.asList(data);
  }

  @Test
  public void extractSimplePromptNameFindsLastPartOfString() {
    String simplePromptName = extractSimplePromptName(this.promptNameOrg);
    Assert.assertEquals(this.expected, simplePromptName);
  }

  // extractSimplePromptName omitted
}

There can only be one @Test@Test in such a class, though.

More info can be found here:
http://www.vogella.com/tutorials/JUnit/article.html#junitadvanced_parameterizedtestshere.

You could use a parameterized test.

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class MyParameterizedClassTest {

  private string promptNameOrg;

  public MyParameterizedClassTest(string testParameter, string expectedParameter) {
    this.promptNameOrg = testParameter;
    this.expected = expectedParameter;
  }

  // creates the test data
  @Parameters
  public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { "20129142\\1234", "1234" }, { "80808080\\159;20129142\\1234", "1234" } };
    return Arrays.asList(data);
  }

  @Test
  public void extractSimplePromptNameFindsLastPartOfString() {
    String simplePromptName = extractSimplePromptName(this.promptNameOrg);
    Assert.assertEquals(this.expected, simplePromptName);
  }

  // extractSimplePromptName omitted
}

There can only be one @Test in such a class, though.

More info can be found here:
http://www.vogella.com/tutorials/JUnit/article.html#junitadvanced_parameterizedtests

You could use a parameterized test:

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class MyParameterizedClassTest {

  private string promptNameOrg;

  public MyParameterizedClassTest(string testParameter, string expectedParameter) {
    this.promptNameOrg = testParameter;
    this.expected = expectedParameter;
  }

  // creates the test data
  @Parameters
  public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { "20129142\\1234", "1234" }, { "80808080\\159;20129142\\1234", "1234" } };
    return Arrays.asList(data);
  }

  @Test
  public void extractSimplePromptNameFindsLastPartOfString() {
    String simplePromptName = extractSimplePromptName(this.promptNameOrg);
    Assert.assertEquals(this.expected, simplePromptName);
  }

  // extractSimplePromptName omitted
}

There can only be one @Test in such a class, though.

More info can be found here.

added 1247 characters in body
Source Link
Lars-Erik
  • 1.4k
  • 9
  • 12

Try usingYou could use a parameterized teststest.

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class MyParameterizedClassTest {

  private string promptNameOrg;

  public MyParameterizedClassTest(string testParameter, string expectedParameter) {
    this.promptNameOrg = testParameter;
    this.expected = expectedParameter;
  }

  // creates the test data
  @Parameters
  public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { "20129142\\1234", "1234" }, { "80808080\\159;20129142\\1234", "1234" } };
    return Arrays.asList(data);
  }

  @Test
  public void extractSimplePromptNameFindsLastPartOfString() {
    String simplePromptName = extractSimplePromptName(this.promptNameOrg);
    Assert.assertEquals(this.expected, simplePromptName);
  }

  // extractSimplePromptName omitted
}

There can only be one @Test in such a class, though.

More info can be found here: 
http://www.vogella.com/tutorials/JUnit/article.html#junitadvanced_parameterizedtests

You could use a parameterized test.

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class MyParameterizedClassTest {

  private string promptNameOrg;

  public MyParameterizedClassTest(string testParameter, string expectedParameter) {
    this.promptNameOrg = testParameter;
    this.expected = expectedParameter;
  }

  // creates the test data
  @Parameters
  public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { "20129142\\1234", "1234" }, { "80808080\\159;20129142\\1234", "1234" } };
    return Arrays.asList(data);
  }

  @Test
  public void extractSimplePromptNameFindsLastPartOfString() {
    String simplePromptName = extractSimplePromptName(this.promptNameOrg);
    Assert.assertEquals(this.expected, simplePromptName);
  }

  // extractSimplePromptName omitted
}

There can only be one @Test in such a class, though.

More info can be found here: 
http://www.vogella.com/tutorials/JUnit/article.html#junitadvanced_parameterizedtests

Source Link
Lars-Erik
  • 1.4k
  • 9
  • 12
Loading