Unit testing @Asynchronous method in a spy in java 7











up vote
0
down vote

favorite












I am trying to test some awful legacy code (Java 7)

I need to spy on a stateless service, but call an asynchronous method



I get the error




Object does not represent an actual Future




How should I try and test this?



@InjectMocks
private OrderDataImport spyImporter;

/* has spyImporter as a dependency */
@InjectMocks
private ImportService sut; /* system under test */

@Before
public void setUp() {
// create spy
spyImporter = Mockito.spy(new OrderDataImport());
MockitoAnnotations.initMocks(this);
}

@Test
public Test() {
sut.import();
}


Import Code



class ImportService {
@EJB
private OrderDataImport dependency;


public boolean import() {
List<Future<Result>> resultList = new ArrayList<>();

int THREAD_COUNT = 3;
while (THREAD_COUNT-- > 0)
resultList.add(dependency.create(...));

for (int i = 0; i < resultList.size(); i++) {
/* Object does not represent an actual Future */
Result result = resultList.get(i).get(60, TimeUnit.MINUTES);
}
}
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    I am trying to test some awful legacy code (Java 7)

    I need to spy on a stateless service, but call an asynchronous method



    I get the error




    Object does not represent an actual Future




    How should I try and test this?



    @InjectMocks
    private OrderDataImport spyImporter;

    /* has spyImporter as a dependency */
    @InjectMocks
    private ImportService sut; /* system under test */

    @Before
    public void setUp() {
    // create spy
    spyImporter = Mockito.spy(new OrderDataImport());
    MockitoAnnotations.initMocks(this);
    }

    @Test
    public Test() {
    sut.import();
    }


    Import Code



    class ImportService {
    @EJB
    private OrderDataImport dependency;


    public boolean import() {
    List<Future<Result>> resultList = new ArrayList<>();

    int THREAD_COUNT = 3;
    while (THREAD_COUNT-- > 0)
    resultList.add(dependency.create(...));

    for (int i = 0; i < resultList.size(); i++) {
    /* Object does not represent an actual Future */
    Result result = resultList.get(i).get(60, TimeUnit.MINUTES);
    }
    }
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to test some awful legacy code (Java 7)

      I need to spy on a stateless service, but call an asynchronous method



      I get the error




      Object does not represent an actual Future




      How should I try and test this?



      @InjectMocks
      private OrderDataImport spyImporter;

      /* has spyImporter as a dependency */
      @InjectMocks
      private ImportService sut; /* system under test */

      @Before
      public void setUp() {
      // create spy
      spyImporter = Mockito.spy(new OrderDataImport());
      MockitoAnnotations.initMocks(this);
      }

      @Test
      public Test() {
      sut.import();
      }


      Import Code



      class ImportService {
      @EJB
      private OrderDataImport dependency;


      public boolean import() {
      List<Future<Result>> resultList = new ArrayList<>();

      int THREAD_COUNT = 3;
      while (THREAD_COUNT-- > 0)
      resultList.add(dependency.create(...));

      for (int i = 0; i < resultList.size(); i++) {
      /* Object does not represent an actual Future */
      Result result = resultList.get(i).get(60, TimeUnit.MINUTES);
      }
      }
      }









      share|improve this question















      I am trying to test some awful legacy code (Java 7)

      I need to spy on a stateless service, but call an asynchronous method



      I get the error




      Object does not represent an actual Future




      How should I try and test this?



      @InjectMocks
      private OrderDataImport spyImporter;

      /* has spyImporter as a dependency */
      @InjectMocks
      private ImportService sut; /* system under test */

      @Before
      public void setUp() {
      // create spy
      spyImporter = Mockito.spy(new OrderDataImport());
      MockitoAnnotations.initMocks(this);
      }

      @Test
      public Test() {
      sut.import();
      }


      Import Code



      class ImportService {
      @EJB
      private OrderDataImport dependency;


      public boolean import() {
      List<Future<Result>> resultList = new ArrayList<>();

      int THREAD_COUNT = 3;
      while (THREAD_COUNT-- > 0)
      resultList.add(dependency.create(...));

      for (int i = 0; i < resultList.size(); i++) {
      /* Object does not represent an actual Future */
      Result result = resultList.get(i).get(60, TimeUnit.MINUTES);
      }
      }
      }






      java junit mockito future






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 8:34

























      asked Nov 9 at 7:50









      Jim

      5,933638108




      5,933638108
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Maybe you need mockito's static method when() , just try it



          doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)






          share|improve this answer




























            up vote
            0
            down vote













            You should not spy on anything else in your test than the SUT itself.



            OrderDataImport is a dependency that you should aim to mock, not spy.



            You also should use the @InjectMocks on the SUT only:



            @Mock
            private OrderDataImport importerStub;

            @InjectMocks
            private ImportService sut;

            @Before
            public void setUp() {
            MockitoAnnotations.initMocks(this);
            }

            @Test
            public Test() {
            // Arrange
            when(importerStub.create(..)).thenReturn(/* Future instance */);

            // Act
            sut.import();
            }


            In the end, I see that you don't really need any spying here.






            share|improve this answer





















            • I need to spy on it, as it's part of the legacy code that needs to execute.
              – Jim
              Nov 9 at 8:31










            • So the ImportService is not the SUT actually here?
              – Maciej Kowalski
              Nov 9 at 8:33










            • yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
              – Jim
              Nov 9 at 8:35










            • Then i would say you should test that inside a test class where OrderDataImport is the SUT
              – Maciej Kowalski
              Nov 9 at 8:35











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221689%2funit-testing-asynchronous-method-in-a-spy-in-java-7%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Maybe you need mockito's static method when() , just try it



            doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              Maybe you need mockito's static method when() , just try it



              doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Maybe you need mockito's static method when() , just try it



                doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)






                share|improve this answer












                Maybe you need mockito's static method when() , just try it



                doReturn({Future subclass instance}).when({reference spyImporter}).get(anyInt(),xx)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 8:06









                winters

                612




                612
























                    up vote
                    0
                    down vote













                    You should not spy on anything else in your test than the SUT itself.



                    OrderDataImport is a dependency that you should aim to mock, not spy.



                    You also should use the @InjectMocks on the SUT only:



                    @Mock
                    private OrderDataImport importerStub;

                    @InjectMocks
                    private ImportService sut;

                    @Before
                    public void setUp() {
                    MockitoAnnotations.initMocks(this);
                    }

                    @Test
                    public Test() {
                    // Arrange
                    when(importerStub.create(..)).thenReturn(/* Future instance */);

                    // Act
                    sut.import();
                    }


                    In the end, I see that you don't really need any spying here.






                    share|improve this answer





















                    • I need to spy on it, as it's part of the legacy code that needs to execute.
                      – Jim
                      Nov 9 at 8:31










                    • So the ImportService is not the SUT actually here?
                      – Maciej Kowalski
                      Nov 9 at 8:33










                    • yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
                      – Jim
                      Nov 9 at 8:35










                    • Then i would say you should test that inside a test class where OrderDataImport is the SUT
                      – Maciej Kowalski
                      Nov 9 at 8:35















                    up vote
                    0
                    down vote













                    You should not spy on anything else in your test than the SUT itself.



                    OrderDataImport is a dependency that you should aim to mock, not spy.



                    You also should use the @InjectMocks on the SUT only:



                    @Mock
                    private OrderDataImport importerStub;

                    @InjectMocks
                    private ImportService sut;

                    @Before
                    public void setUp() {
                    MockitoAnnotations.initMocks(this);
                    }

                    @Test
                    public Test() {
                    // Arrange
                    when(importerStub.create(..)).thenReturn(/* Future instance */);

                    // Act
                    sut.import();
                    }


                    In the end, I see that you don't really need any spying here.






                    share|improve this answer





















                    • I need to spy on it, as it's part of the legacy code that needs to execute.
                      – Jim
                      Nov 9 at 8:31










                    • So the ImportService is not the SUT actually here?
                      – Maciej Kowalski
                      Nov 9 at 8:33










                    • yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
                      – Jim
                      Nov 9 at 8:35










                    • Then i would say you should test that inside a test class where OrderDataImport is the SUT
                      – Maciej Kowalski
                      Nov 9 at 8:35













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You should not spy on anything else in your test than the SUT itself.



                    OrderDataImport is a dependency that you should aim to mock, not spy.



                    You also should use the @InjectMocks on the SUT only:



                    @Mock
                    private OrderDataImport importerStub;

                    @InjectMocks
                    private ImportService sut;

                    @Before
                    public void setUp() {
                    MockitoAnnotations.initMocks(this);
                    }

                    @Test
                    public Test() {
                    // Arrange
                    when(importerStub.create(..)).thenReturn(/* Future instance */);

                    // Act
                    sut.import();
                    }


                    In the end, I see that you don't really need any spying here.






                    share|improve this answer












                    You should not spy on anything else in your test than the SUT itself.



                    OrderDataImport is a dependency that you should aim to mock, not spy.



                    You also should use the @InjectMocks on the SUT only:



                    @Mock
                    private OrderDataImport importerStub;

                    @InjectMocks
                    private ImportService sut;

                    @Before
                    public void setUp() {
                    MockitoAnnotations.initMocks(this);
                    }

                    @Test
                    public Test() {
                    // Arrange
                    when(importerStub.create(..)).thenReturn(/* Future instance */);

                    // Act
                    sut.import();
                    }


                    In the end, I see that you don't really need any spying here.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 8:16









                    Maciej Kowalski

                    11.2k72037




                    11.2k72037












                    • I need to spy on it, as it's part of the legacy code that needs to execute.
                      – Jim
                      Nov 9 at 8:31










                    • So the ImportService is not the SUT actually here?
                      – Maciej Kowalski
                      Nov 9 at 8:33










                    • yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
                      – Jim
                      Nov 9 at 8:35










                    • Then i would say you should test that inside a test class where OrderDataImport is the SUT
                      – Maciej Kowalski
                      Nov 9 at 8:35


















                    • I need to spy on it, as it's part of the legacy code that needs to execute.
                      – Jim
                      Nov 9 at 8:31










                    • So the ImportService is not the SUT actually here?
                      – Maciej Kowalski
                      Nov 9 at 8:33










                    • yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
                      – Jim
                      Nov 9 at 8:35










                    • Then i would say you should test that inside a test class where OrderDataImport is the SUT
                      – Maciej Kowalski
                      Nov 9 at 8:35
















                    I need to spy on it, as it's part of the legacy code that needs to execute.
                    – Jim
                    Nov 9 at 8:31




                    I need to spy on it, as it's part of the legacy code that needs to execute.
                    – Jim
                    Nov 9 at 8:31












                    So the ImportService is not the SUT actually here?
                    – Maciej Kowalski
                    Nov 9 at 8:33




                    So the ImportService is not the SUT actually here?
                    – Maciej Kowalski
                    Nov 9 at 8:33












                    yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
                    – Jim
                    Nov 9 at 8:35




                    yes it is, but I am also testing the create method, I can't stub it out, that's why it's a spy and not a mock.
                    – Jim
                    Nov 9 at 8:35












                    Then i would say you should test that inside a test class where OrderDataImport is the SUT
                    – Maciej Kowalski
                    Nov 9 at 8:35




                    Then i would say you should test that inside a test class where OrderDataImport is the SUT
                    – Maciej Kowalski
                    Nov 9 at 8:35


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221689%2funit-testing-asynchronous-method-in-a-spy-in-java-7%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    how to define a CAPL function taking a sysvar argument

                    Schultheiß

                    Ansible :Unable to parse /etc/ansible/hosts as an inventory source