Category Archives: Java

Sniff Request Parameters

If you need to debug request parameters for a HttpServletRequest:

 

TestNG with Parameters

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:

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
===============================================

Criteria Query – predicate

The following code will return one result based on 2 criteria:

  1. process entity attribute to be equal with process parameter
  2. state entity attribute to be equal with state parameter from function

Entity has these two attributes of type ENUM: process and state.

DB Table consist in a map between process and allowed states. Method findByType will return the correct entry based on one process and one state:

 

Criteria Query – find by id

Example of DAO implementation for Hibernate Criteria Query -> Find By Id

 

Model class has attribute: id

Random number

This code generates a random number between low (inclusive) and high (exclusive):