Java unit test for different input

I need to check some api. For example, I have several @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 a service on multiple URLs (different server environments), how can I test this functionality for different service URLs?

Flow:

  • Init conncetion by URL
  • Run all tests
  • Init conncetion to other URLs
  • Run all tests (same thing)
  • ...

When there was only one host, I like it:

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(); } } 
+7
source share
2 answers

Something happened to me, and I found this amazing idea called Parameterized Test, for example: http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/ so you can all those tests a couple of times with different arguments.

+9
source

there are tests with extended criteria: 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 the thesis of an abstract test and subclass it with each subclass setting the URL.

+8
source

All Articles