mercredi 29 novembre 2017

Verifiying time between calls with mockito

I've read a lot of documentation, examples on the net, and still have no idea of how to do it. (if it could be done).

I need to test a method in a object called by other thread and it has a requeriment of time. So my original idea was creating a spy, override the method and do the task, but the problem is, i can't access test method variables from the spy. So i can't get the value returned from System.currentTimeMillis() from the test method and proccess it to get the difference within times.

i will write some abstract code describing the situation so you could understand better the situation.

@Test
public void test()
{
   Objectspied spy = Mockito.spy(new Objectspied(Parameters p));
   long[] timesRetrieved = new long[numberOfCallsIExpect];
   //here goes the tricky part
   Mockito.doAnswer(new Answer<void>(){
       @override
       public methodCalledFromAnotherThread(int index)
       {
          //what i wish to do but can't do it
          timesRetrieved[i] = System.currentTimeMilis();
       }
   }
   ).when(spy).methodCalledFromAnotherThread(anyInt);
   //here initialize the thread who call it
}

public class AnotherThread 
{
   ObjectSpied reference;
   public void run()
   {
      for (int i=0;i<repeat;i++)
      {
         reference.methodCalledFromAnotherThread(i);
         Thread.sleep(1000);
      }
   }
}

I'm new to Mockito so i know sysntax is totally wrong but i can fix that by myself, all i need to know is:

  • Is there any way to do it with mockito?
  • If so, can you point me to the right direction?

Aucun commentaire:

Enregistrer un commentaire