0

I need to write a unit test for this piece of code. I am just learning how to write unit tests.I want to know what are the test cases that can be written for following class specially for

public void delay() 

method. it contains Thread.sleep() method.

 import org.apache.log4j.Logger;

public abstract class AbstractDelayService<T> implements DelayService<T>
{
  private static Logger log = Logger.getLogger(AbstractDelayServiceTest.class);

  protected DelayFunction delayFunction;
  protected FailureCounter<T> failureCounter;

  public AbstractDelayService(DelayFunction delayFunction, FailureCounter<T> failureCouner)
  {
    this.delayFunction = delayFunction;
    this.failureCounter = failureCouner;
  }

  @Override
  public void delay()
  {
    long delay = delayFunction.getDelay();
    log.info("Delaying lookup of" + " by " + delay + " ms");

    try
    {
      Thread.sleep(delay);
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
  }

  @Override
  public void reportSendSuccess(T key)
  {
    failureCounter.reportSendSuccess(key);   
  }

  @Override
  public void reportSendFailure(T key)
  {
    failureCounter.reportSendFailure(key);    
  }

}
  • possible duplicate of [How to test abstract class in Java with jUnit?](http://stackoverflow.com/questions/7569444/how-to-test-abstract-class-in-java-with-junit) – John Jul 17 '15 at 10:57
  • assert time difference before and after delay, within some allowed delta error ? – John Jul 17 '15 at 10:59
  • 2
    Pragmatic Unit Testing in Java 8 with JUnit - by Jeff Langr, with Andy Hunt and Dave Thomas. I'd recommend this book. (Kind of off topic) – PaperTsar Jul 17 '15 at 11:00
  • Testing checks that the code produced conforms to its specification. Nobody can answer your question, because you have not told us the specification for your code. – Raedwald Jul 17 '15 at 11:53
  • This is not a code writing service or a test writing service. – Raedwald Jul 17 '15 at 11:53
  • I figured it out the answer, thanks for your concerns. – Widura Wijenayake Jul 17 '15 at 12:28
  • http://stackoverflow.com/questions/14892125/what-is-the-best-practice-to-determine-the-execution-time-of-the-bussiness-relev – Widura Wijenayake Jul 17 '15 at 12:29

1 Answers1

1

You definitively don't want to wait for real using Thread.sleep and then count how much time elapsed - it's slow and unreliable. You have to inject Sleeper interface to tested class, so you can mock i in tests. For example:

interface Sleeper {
    void sleep(long ms) throws InterruptedException;
}

...

class RealSleeper implements Sleeper {
    void sleep(long ms) throws InterruptedException {
        Thread.sleep(ms);
    }
}

...

private long time = 0;

@Test
public void test() {
    AbstractDelayService service = new AbstractDelayService(new Sleeper(){
        void sleep(long ms) {
            time+=ms;
        }, delayFunction, failureCouner){};
    Assert.assertEquals(time, 0);
    service.delay();
    Assert.assertEquals(time, 1000);
}

This way you can verify your class sleeps exactly as long as it's supposed to. You will still need some integration tests to make sure Thread.sleep is actually invoked - but you don't have to sleep long in such tests and you don't need to measure the exact timings.

Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65