Often we want to test a function against a set of variables.
TestNG offers @DataProvider annotation.
My goal is to test a service class method: findByType of class StateMachineObjectService. This method accepts two ENUM arguments: SmProcess process and SmState state.
I need to feed a collection of different processes and states. Best way is to create an object array with desired combinations and feed it to test function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
@ContextConfiguration (locations = {"classpath:test-mvc-servlet.xml"}) public class StateMachineObjectServiceTest extends AbstractTestNGSpringContextTests { @Autowired private StateMachineObjectService service; @DataProvider (name = "Data-Provider-Function") public Object[][] parameterTestProvider() { return new Object[][]{ {SmProcess.ADVERTISE,SmState.INCOMPLETE}, {SmProcess.ADVERTISE,SmState.ACTIVE}, {SmProcess.ADVERTISE,SmState.DELETED}, {SmProcess.ADVERTISE,SmState.EXPIRED}, {SmProcess.ADVERTISE,SmState.ARCHIVED}, {SmProcess.ADVERTISE,SmState.INITIAL}, {SmProcess.ADVERTISE,SmState.SOLD}, {SmProcess.ADVERTISE,SmState.BUMPED}, }; } @Test (dataProvider = "Data-Provider-Function") public void findByType(SmProcess process, SmState state) { System.out.println(process.name() + " " + state.name()); StateMachineEntity entity = null; try { entity = service.findByType(process, state); //System.out.println(entity.getId() + " " + entity.getProcess().name() + " " + entity.getState().name()); } catch (RecordNotFoundException e) { fail(e.toString()); } assertEquals(entity.getProcess(), process, "returned wrong process"); assertEquals(entity.getState(), state, "returned wrong state"); } } |
Output is like:
ADVERTISE INCOMPLETE
Hibernate: select statemachi0_.id as id1_22_, statemachi0_.process as process2_22_, statemachi0_.state as state3_22_ from sm_map statemachi0_ where statemachi0_.process=0 and statemachi0_.state=0
ADVERTISE ACTIVE
Hibernate: select statemachi0_.id as id1_22_, statemachi0_.process as process2_22_, statemachi0_.state as state3_22_ from sm_map statemachi0_ where statemachi0_.process=0 and statemachi0_.state=1
…
===============================================
Custom suite
Total tests run: 8, Failures: 0, Skips: 0
===============================================