質問

We have a text file where a list of search query and the expected result is given. Such as,

Search Result

a:120         result1
b:220         result2
.....
.....

Now, we need to write a JUnit (heavily used in our daily build) test class, where each of the row will represent one @Test method. So by that, we know, which search case failed (UI).

We already have a solution, where we have one @Test method only, and we have log to check which case passed or failed.

But, we are trying to achieve per case represented as a junit method. Is it really possible, to dynamically create a @Test method to JUnit architecture.

Our, @Test method is same for every search case. That means, we just want to pass a different parameter every time.

I have come up with a JUnit3 solution to my problem. Need help to translate it to Junit4.

public static Test suite()
   {
      TestSuite suite = new TestSuite();
      for ( int i = 1; i <= 5; i++ ) {

         final int j = i;
         suite.addTest(
            new Test1( "testQuery" + i ) {
               protected void runTest()
               {
                  try {
                     testQuery( j );
                  } catch ( MalformedURLException e ) {

                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  } catch ( SolrServerException e ) {

                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  }
               }
            }
            );
      }

      return suite;
   }
役に立ちましたか?

解決

In JUnit 4 there is a concept called "Parameterized Test" that is used for exactly this.

I don't fully understand your test above, but this should give you a hint:

@RunWith(Parameterized.class)
public class ParameterizedTest {

    private String query;
    private String expectedResult;

    public ParameterizedTest(String query, String expectedResult) {
        this.query = datum;
        this.expectedResult = expectedResult;
    }

    @Parameters
    public static Collection<Object[]> generateData() {
        Object[][] data = {
                { "a:120", "result1" },
                { "b:220", "result2" },
        };
        return Arrays.asList(data);
    }

    @Test
    public void checkQueryResult() {
        System.out.println("Checking that the resutl for query " + query + " is " + expectedResult);
        // ...
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top