@Given("^I wait until the statisticaltest status code is either (\\d) or (\\d) less than (\\d+)$") public void I_wait_until_statisticaltest_status_code_is(int code1, int code2, int secs) throws AuthenticationException { Long code = (Long) ((JSONObject) context.statisticaltest.get("status")).get("code"); GregorianCalendar start = new GregorianCalendar(); start.add(Calendar.SECOND, secs); Date end = start.getTime(); while (code.intValue() != code1 && code.intValue() != code2) { try { Thread.sleep(3000); } catch (InterruptedException e) { } assertTrue("Time exceded ", end.after(new Date())); I_get_the_statisticaltest((String) context.statisticaltest.get("resource")); code = (Long) ((JSONObject) context.statisticaltest.get("status")).get("code"); } assertEquals(code1, code.intValue()); }