how to mock class in java without creating interface












2















First, I am not Java developer, I am using php.



I am curious in Java, for example if I have structure like this:



interface Ainterface {
public String method();
}

public class A implements Ainterface {
public String method() {
//do something
}
}

public class B {
public String method(Ainterface a) {
a.method();
//do something
}
}


Now if I want to test B's method I can mock a



public class Amock implements Ainterface {
public String method() {
//do something
}
}


And inject it into B's method.



But, if I don't want to create interface and I have situation like this:



public class A {
public String method() {
//do something
}
}

public class B {
public String method(A a) {
a.method();
//do something
}
}


Is there any way to mock a or test B's method in other way?










share|improve this question



























    2















    First, I am not Java developer, I am using php.



    I am curious in Java, for example if I have structure like this:



    interface Ainterface {
    public String method();
    }

    public class A implements Ainterface {
    public String method() {
    //do something
    }
    }

    public class B {
    public String method(Ainterface a) {
    a.method();
    //do something
    }
    }


    Now if I want to test B's method I can mock a



    public class Amock implements Ainterface {
    public String method() {
    //do something
    }
    }


    And inject it into B's method.



    But, if I don't want to create interface and I have situation like this:



    public class A {
    public String method() {
    //do something
    }
    }

    public class B {
    public String method(A a) {
    a.method();
    //do something
    }
    }


    Is there any way to mock a or test B's method in other way?










    share|improve this question

























      2












      2








      2








      First, I am not Java developer, I am using php.



      I am curious in Java, for example if I have structure like this:



      interface Ainterface {
      public String method();
      }

      public class A implements Ainterface {
      public String method() {
      //do something
      }
      }

      public class B {
      public String method(Ainterface a) {
      a.method();
      //do something
      }
      }


      Now if I want to test B's method I can mock a



      public class Amock implements Ainterface {
      public String method() {
      //do something
      }
      }


      And inject it into B's method.



      But, if I don't want to create interface and I have situation like this:



      public class A {
      public String method() {
      //do something
      }
      }

      public class B {
      public String method(A a) {
      a.method();
      //do something
      }
      }


      Is there any way to mock a or test B's method in other way?










      share|improve this question














      First, I am not Java developer, I am using php.



      I am curious in Java, for example if I have structure like this:



      interface Ainterface {
      public String method();
      }

      public class A implements Ainterface {
      public String method() {
      //do something
      }
      }

      public class B {
      public String method(Ainterface a) {
      a.method();
      //do something
      }
      }


      Now if I want to test B's method I can mock a



      public class Amock implements Ainterface {
      public String method() {
      //do something
      }
      }


      And inject it into B's method.



      But, if I don't want to create interface and I have situation like this:



      public class A {
      public String method() {
      //do something
      }
      }

      public class B {
      public String method(A a) {
      a.method();
      //do something
      }
      }


      Is there any way to mock a or test B's method in other way?







      java testing mocking






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 22:38









      Nika KhurashviliNika Khurashvili

      75012




      75012
























          2 Answers
          2






          active

          oldest

          votes


















          4














          In Java you can use specific mocking framework like Mockito and use it's specific method - for example



          A aMock = Mockito.mock(A.class);


          Of course this way you can only create a really simple mock that will do literally nothing but the framework allows you to define what specific method should return with providing when/then mechanism. The example of such can be



          when(aMock.method()).thenReturn("I am just a fake");


          The mockito is really powerfull and I cannot explain you whole framework in this answer so please visit Mockito home and reference page to get familiar with this



          Also Mockito is one of solutions - it's kind of popular but not only the one so you can look for the solution that fits your requirements best






          share|improve this answer





















          • 1





            Thanks for answer, I get that, but if I don't want to use some framework is there any native way?

            – Nika Khurashvili
            Nov 22 '18 at 22:45






          • 1





            nooooooooope :)

            – m.antkowicz
            Nov 22 '18 at 22:47











          • Yes, but that would be reinventing the wheel, producing code to do something similar that mockito does. (Building your own mocking framework, for example with Proxies)

            – Pascal Ludwig
            Nov 22 '18 at 23:17













          • Building your own mocking framework is not a native way

            – m.antkowicz
            Nov 23 '18 at 7:37



















          1














          @m.antkowicz's answer shows the correct way of doing it, by using a mocking framework. In the comments you asked for a way without using external framework, so this answer tries to address that.



          Just like you created Amock by implementing Ainterface, you can create a child class that extends A.



          So you would have a class like class AmockClassBased extends A and then you can override method() to make it do what you were doing in Amock class's method().



          So your Amock will be changed to:-



          public class AmockClassBased extends A {

          @Override
          public String method() {
          //do something
          }
          }


          Then you can pass an instance of this class to B's method(A a).






          share|improve this answer























            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',
            autoActivateHeartbeat: false,
            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%2f53438731%2fhow-to-mock-class-in-java-without-creating-interface%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









            4














            In Java you can use specific mocking framework like Mockito and use it's specific method - for example



            A aMock = Mockito.mock(A.class);


            Of course this way you can only create a really simple mock that will do literally nothing but the framework allows you to define what specific method should return with providing when/then mechanism. The example of such can be



            when(aMock.method()).thenReturn("I am just a fake");


            The mockito is really powerfull and I cannot explain you whole framework in this answer so please visit Mockito home and reference page to get familiar with this



            Also Mockito is one of solutions - it's kind of popular but not only the one so you can look for the solution that fits your requirements best






            share|improve this answer





















            • 1





              Thanks for answer, I get that, but if I don't want to use some framework is there any native way?

              – Nika Khurashvili
              Nov 22 '18 at 22:45






            • 1





              nooooooooope :)

              – m.antkowicz
              Nov 22 '18 at 22:47











            • Yes, but that would be reinventing the wheel, producing code to do something similar that mockito does. (Building your own mocking framework, for example with Proxies)

              – Pascal Ludwig
              Nov 22 '18 at 23:17













            • Building your own mocking framework is not a native way

              – m.antkowicz
              Nov 23 '18 at 7:37
















            4














            In Java you can use specific mocking framework like Mockito and use it's specific method - for example



            A aMock = Mockito.mock(A.class);


            Of course this way you can only create a really simple mock that will do literally nothing but the framework allows you to define what specific method should return with providing when/then mechanism. The example of such can be



            when(aMock.method()).thenReturn("I am just a fake");


            The mockito is really powerfull and I cannot explain you whole framework in this answer so please visit Mockito home and reference page to get familiar with this



            Also Mockito is one of solutions - it's kind of popular but not only the one so you can look for the solution that fits your requirements best






            share|improve this answer





















            • 1





              Thanks for answer, I get that, but if I don't want to use some framework is there any native way?

              – Nika Khurashvili
              Nov 22 '18 at 22:45






            • 1





              nooooooooope :)

              – m.antkowicz
              Nov 22 '18 at 22:47











            • Yes, but that would be reinventing the wheel, producing code to do something similar that mockito does. (Building your own mocking framework, for example with Proxies)

              – Pascal Ludwig
              Nov 22 '18 at 23:17













            • Building your own mocking framework is not a native way

              – m.antkowicz
              Nov 23 '18 at 7:37














            4












            4








            4







            In Java you can use specific mocking framework like Mockito and use it's specific method - for example



            A aMock = Mockito.mock(A.class);


            Of course this way you can only create a really simple mock that will do literally nothing but the framework allows you to define what specific method should return with providing when/then mechanism. The example of such can be



            when(aMock.method()).thenReturn("I am just a fake");


            The mockito is really powerfull and I cannot explain you whole framework in this answer so please visit Mockito home and reference page to get familiar with this



            Also Mockito is one of solutions - it's kind of popular but not only the one so you can look for the solution that fits your requirements best






            share|improve this answer















            In Java you can use specific mocking framework like Mockito and use it's specific method - for example



            A aMock = Mockito.mock(A.class);


            Of course this way you can only create a really simple mock that will do literally nothing but the framework allows you to define what specific method should return with providing when/then mechanism. The example of such can be



            when(aMock.method()).thenReturn("I am just a fake");


            The mockito is really powerfull and I cannot explain you whole framework in this answer so please visit Mockito home and reference page to get familiar with this



            Also Mockito is one of solutions - it's kind of popular but not only the one so you can look for the solution that fits your requirements best







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 22:48

























            answered Nov 22 '18 at 22:40









            m.antkowiczm.antkowicz

            8,562928




            8,562928








            • 1





              Thanks for answer, I get that, but if I don't want to use some framework is there any native way?

              – Nika Khurashvili
              Nov 22 '18 at 22:45






            • 1





              nooooooooope :)

              – m.antkowicz
              Nov 22 '18 at 22:47











            • Yes, but that would be reinventing the wheel, producing code to do something similar that mockito does. (Building your own mocking framework, for example with Proxies)

              – Pascal Ludwig
              Nov 22 '18 at 23:17













            • Building your own mocking framework is not a native way

              – m.antkowicz
              Nov 23 '18 at 7:37














            • 1





              Thanks for answer, I get that, but if I don't want to use some framework is there any native way?

              – Nika Khurashvili
              Nov 22 '18 at 22:45






            • 1





              nooooooooope :)

              – m.antkowicz
              Nov 22 '18 at 22:47











            • Yes, but that would be reinventing the wheel, producing code to do something similar that mockito does. (Building your own mocking framework, for example with Proxies)

              – Pascal Ludwig
              Nov 22 '18 at 23:17













            • Building your own mocking framework is not a native way

              – m.antkowicz
              Nov 23 '18 at 7:37








            1




            1





            Thanks for answer, I get that, but if I don't want to use some framework is there any native way?

            – Nika Khurashvili
            Nov 22 '18 at 22:45





            Thanks for answer, I get that, but if I don't want to use some framework is there any native way?

            – Nika Khurashvili
            Nov 22 '18 at 22:45




            1




            1





            nooooooooope :)

            – m.antkowicz
            Nov 22 '18 at 22:47





            nooooooooope :)

            – m.antkowicz
            Nov 22 '18 at 22:47













            Yes, but that would be reinventing the wheel, producing code to do something similar that mockito does. (Building your own mocking framework, for example with Proxies)

            – Pascal Ludwig
            Nov 22 '18 at 23:17







            Yes, but that would be reinventing the wheel, producing code to do something similar that mockito does. (Building your own mocking framework, for example with Proxies)

            – Pascal Ludwig
            Nov 22 '18 at 23:17















            Building your own mocking framework is not a native way

            – m.antkowicz
            Nov 23 '18 at 7:37





            Building your own mocking framework is not a native way

            – m.antkowicz
            Nov 23 '18 at 7:37













            1














            @m.antkowicz's answer shows the correct way of doing it, by using a mocking framework. In the comments you asked for a way without using external framework, so this answer tries to address that.



            Just like you created Amock by implementing Ainterface, you can create a child class that extends A.



            So you would have a class like class AmockClassBased extends A and then you can override method() to make it do what you were doing in Amock class's method().



            So your Amock will be changed to:-



            public class AmockClassBased extends A {

            @Override
            public String method() {
            //do something
            }
            }


            Then you can pass an instance of this class to B's method(A a).






            share|improve this answer




























              1














              @m.antkowicz's answer shows the correct way of doing it, by using a mocking framework. In the comments you asked for a way without using external framework, so this answer tries to address that.



              Just like you created Amock by implementing Ainterface, you can create a child class that extends A.



              So you would have a class like class AmockClassBased extends A and then you can override method() to make it do what you were doing in Amock class's method().



              So your Amock will be changed to:-



              public class AmockClassBased extends A {

              @Override
              public String method() {
              //do something
              }
              }


              Then you can pass an instance of this class to B's method(A a).






              share|improve this answer


























                1












                1








                1







                @m.antkowicz's answer shows the correct way of doing it, by using a mocking framework. In the comments you asked for a way without using external framework, so this answer tries to address that.



                Just like you created Amock by implementing Ainterface, you can create a child class that extends A.



                So you would have a class like class AmockClassBased extends A and then you can override method() to make it do what you were doing in Amock class's method().



                So your Amock will be changed to:-



                public class AmockClassBased extends A {

                @Override
                public String method() {
                //do something
                }
                }


                Then you can pass an instance of this class to B's method(A a).






                share|improve this answer













                @m.antkowicz's answer shows the correct way of doing it, by using a mocking framework. In the comments you asked for a way without using external framework, so this answer tries to address that.



                Just like you created Amock by implementing Ainterface, you can create a child class that extends A.



                So you would have a class like class AmockClassBased extends A and then you can override method() to make it do what you were doing in Amock class's method().



                So your Amock will be changed to:-



                public class AmockClassBased extends A {

                @Override
                public String method() {
                //do something
                }
                }


                Then you can pass an instance of this class to B's method(A a).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 23:21









                KartikKartik

                3,19931435




                3,19931435






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438731%2fhow-to-mock-class-in-java-without-creating-interface%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

                    Wiesbaden

                    Marschland

                    Dieringhausen