0

I'm trying to write a unit test for the following function.

responseHandler and ErrorHandler are both methods that are passes using the Command Pattern and are used to continue the program's flow. futureCommand is a lettuce object (Redis implementation in java). I'm having difficulties with how to test this method since is both using a future and does not return anything.

public void getOfferData(EventRequestContext<? extends BaseEvent> ctx, int offerId, ResponseHandler<T1Offer> responseHandler,
        ErrorHandler<Throwable> errorHandler) throws Exception {
    String redisKey = keyPrefix + offerId;
    RedisFuture<List<String>> futureCommand = connectionWrapper.getHashValues(redisKey, getRequiredParams());
    futureCommand.thenAccept(valuesList -> {
        TrackerScheduler.processT1GenreicPool.execute(ctx, () -> {
            Map<String, String> resultMap = reconstructMapValues(valuesList, getRequiredParams(), redisKey, ctx);
            T1Offer offerData;
            if(!resultMap.isEmpty()) {
                offerData = new T1Offer(resultMap);
            } else {
                offerData = new T1Offer();
            }
            if(!offerData.isValid()) {
                errorHandler.onError(new Exception("Invalid fields in offerData"));
            } else {
                responseHandler.onResponse(offerData);
            }
        });
    });
}

My best attempt was to send the assertion using the responseHandler method like this:

@Test
public void getOfferData_offerFullData_parseSuccess() throws Exception {
    T1ClickOfferDao.instance.getOfferData(null, Integer.parseInt(OFF_ID), resultOffer -> { 
        Assert.assertEquals("", resultOffer.getActivationDate());
    }, error -> {
    });
}

but the Test context is finished before the future is evaluated. And even if I Threas.sleep for a second - the assertion does not affect the test result.

Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
gshlomig
  • 21
  • 1

1 Answers1

0

How about

@Test
public void getOfferData_offerFullData_parseSuccess() throws Exception {
    final String lock = new String("lock");
    T1ClickOfferDao.instance.getOfferData(null, Integer.parseInt(OFF_ID), resultOffer -> { 
        Assert.assertEquals("", resultOffer.getActivationDate());
        synchronized(lock){
            lock.notifyAll();
        }
    }, error -> {
    });
    synchronized(lock){
        try{ 
            lock.wait(1000*2); 
        } catch (InterruptedException ex) {
            fail("Timeout");
        }
    }
}
Imaskar
  • 2,773
  • 24
  • 35