0

Description: Trying to mock this class (from some library):

public class Signal{

   public static Signal fromPool(){
      // returns a recycled instance from a signal pool
   }

   public int getCode(){
     // returns some signal code;
   }
}

Class Sender uses Signal.fromPool() internally :

public class Sender{
   private Signal sig;

   public void ready(){
     sig = Signal.fromPool();
     System.out.println(sig);
   }

   public void send(OutputStream output){
      output.write(sig.getCode());
   }
}

And we need to write some unit tests for Sender.

But, In Unit test environment, Signal.fromPool() will fail, because environment lacks some internal dependencies.

Hence, we decide to use "JMockit" to mock Signal class:

public class SenderTest{

   // mock signal class
   static class MockSignal extends Mockup<Signal>{

      @Mock
      public static Signal fromPool(){
         return new Signal();
      }

      @Mock
      public int getCode(){
         return 42;
      }
   }

   @Test
   public void testSending(@Mocked OutputStream stream){
       // replace Signal with MockSignal
       new MockSignal();

       // should write 42
       new Expectations(){{
          stream.write(42);
       }};

       // test code
       Sender sender = new Sender();
       sender.ready();
       sender.send(stream);
   }
}

Problem: Mocked Signal.fromPool returns a null. This statement in above code prints null:

     System.out.println(sig);

My understanding is that perhaps inside the mocked fromPool the Mocked class itself isn't being instantiated:

  @Mock
  public static Signal fromPool(){
     return new Signal();
  }

How can new Signal return null ? Anyone has a better perspective ?

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • @AdrianShum Please read the question again. Its not about how to mock static methods. – S.D. Dec 04 '15 at 07:32
  • Please read the link, it seems that you are mocking the static method wrong, hence not giving proper result. However I need to admit that I am not a JMockIt user, I just happened to find the wrong syntax base on a previous question – Adrian Shum Dec 04 '15 at 07:34
  • @AdrianShum Actually, I'm following : http://jmockit.org/tutorial/Faking.html#mocks And same approach has worked for me in other tests and with other classes. – S.D. Dec 04 '15 at 07:37
  • You mean for the static mocking part? But from what I found, when you use JMockit `@Mock` to mock a static method, you should annotate it on the method WITHOUT `static` classifier (as mentioned in the link I shown and http://abhinandanmk.blogspot.in/2012/06/jmockit-tutoriallearn-it-today-with.html#static_method), for which you seems doing wrong – Adrian Shum Dec 04 '15 at 07:41
  • Are you sure the example test code does not work? Because for me it's working fine (the test passes). Check for any error messages in standard output, in case JMockit is failing to initialize itself. – Rogério Dec 07 '15 at 16:09
  • @Rogério Thanks for your response. It was much simpler actually, `@Mocked Signal sig` as a test function parameter and setting the `result = sig` in expectations block. The real reason `new Signal` was returning null was because the runtime platform itself returns null for the constructor. I thought I would never be able to create a new instance, but @Mocked annotation did got me a mock instance to use in test. I'm just exploring JMockit, its awesome so far. – S.D. Dec 08 '15 at 05:54

0 Answers0