Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java unit test for different input data

Tags:

java

junit

junit4

I need to test some api. For example I have multiple @Test methods in the class to test my functionality, before init I connect to some url of my service, and use it.

If I have my service at multiple urls(different server environments), how can I Test this functionality for different service urls?

Flow:

  1. Init conncetion by url
  2. Run all Tests
  3. Init conncetion by another url
  4. Run all Tests(the same)
  5. ...

when was only one host I do like this:

public class TestAPI{
    @org.junit.Before
    public void init() {
      service = new Service("www.test.com");
    }
    @org.junit.Test
    public void testA(){
      service.CallA(); 
    }
    @org.junit.Test
    public void testB(){
     service.CallB(); 
    }
}
like image 458
Sergey Avatar asked Apr 05 '12 12:04

Sergey


2 Answers

Happen to me sometime, and them I found this awesome idea called Parameterized Test, for instance: http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/ in this way you can all the same tests couple of time with different argument.

like image 189
shem Avatar answered Nov 10 '22 15:11

shem


there are prameterized tests: http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/ and http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/.

alternatively, you can make your test case abstract and subclass it with each subclass setting up the url.

like image 42
Ray Tayek Avatar answered Nov 10 '22 15:11

Ray Tayek