JUnit Testing using Mockito and Power Mock - SpringBoot | JavaTechie

Поділитися
Вставка
  • Опубліковано 6 лис 2024
  • This video will explain you how to mock private,static and final method using power mock-api
    #JavaTechie #SpringBoot #PowerMock
    GitHub:
    github.com/Jav...
    Blogs:
    javagyanmantra...
    Facebook:
    / 919464521471923
    Like and subscribe

КОМЕНТАРІ • 94

  • @francksgenlecroyant
    @francksgenlecroyant Рік тому +5

    Hi Bassant, 5 years later and this is still relevant 👊

  • @shiskeyoffles
    @shiskeyoffles 5 років тому +8

    I am currently working in a company and my Job is to write unit test cases and this is really helpful. Thank you.

    • @Javatechie
      @Javatechie  5 років тому +1

      Thanks Holmes,
      More test case video is on the way using rest assured and TestRestTemplate
      Will upload soon

  • @pratapsingh-le2br
    @pratapsingh-le2br 5 років тому +7

    Really Help full to the interview prospective Thank' you Basant Bahi :) Great Effort Keep Going

  • @sabakaiser4505
    @sabakaiser4505 5 років тому +4

    This is a good tutorial to know how to use power mock, but before this if setup part is explained it will be helpful.

    • @Javatechie
      @Javatechie  5 років тому +1

      Hi Saba , there is no additional set up as far I know , all required step I. Mentioned in video .
      Please do let me know anything else if I can help you

    • @TheTubepc
      @TheTubepc 2 роки тому

      @@Javatechie if you can provide github link it will be useful

  • @veenatiwari8621
    @veenatiwari8621 Рік тому +2

    Thanks a lot sir for so knowledge video , kindly make a video for test case of predicate and specification.(dynamic query JPA criteria.

  • @chetanbhandari3033
    @chetanbhandari3033 4 роки тому +3

    Hi awesome video and explained very well with use case pls.create multiple scenario and all junit and test case series which will help day to day programing

  • @mukundpatel1768
    @mukundpatel1768 4 роки тому +2

    Very nicely explained. I have used your way and implemented unit test ... Thanks

  • @rameshchowdarykonka5551
    @rameshchowdarykonka5551 5 років тому +4

    Tq.. Just now i am searching for........Again Tq........

  • @literatebasics6305
    @literatebasics6305 5 років тому +5

    Good video. Worth watching. Can you please do more videos on Power Mockito ?

  • @shivakamlikar2144
    @shivakamlikar2144 5 років тому +1

    I am very happy..... Your explanation is awesome and very helpfull for development

    • @Javatechie
      @Javatechie  5 років тому

      Thanks dude , keep watching

  • @hiteshupreti4250
    @hiteshupreti4250 3 роки тому +1

    why we are using "spy" for private and final methods??

  • @shalinishalu3465
    @shalinishalu3465 3 роки тому +1

    Great. . really helpful. Thanku

  • @blaxminarayanpatra7311
    @blaxminarayanpatra7311 4 роки тому +1

    Hi Bro, Suppose I have one method inside multiple private methods inside each method have multiple methods in that case should I maintain test case for all method or I will ignore (In realtime) Thanks In advance.

    • @Javatechie
      @Javatechie  4 роки тому

      No we should ignore only for actual business you need to write one positive and one negative scenario

  • @gaurav16413
    @gaurav16413 4 роки тому +1

    Great Tutorial Sir..

  • @javadream6085
    @javadream6085 3 роки тому +1

    Do you know how to debug this test cases using powermock. Because i am trying to debug this but all my endpoints are skipped. Same is working fine with mockito but with poermock i am not able to debug.

    • @Javatechie
      @Javatechie  3 роки тому

      Debugger should work for any implementation let me check

  • @rahimkhan-fh9dd
    @rahimkhan-fh9dd 5 років тому +1

    tutorial was more helpful,
    Can we use mock instead of spy in private method like PowerMockito.mock()?

  • @o7n71a0453
    @o7n71a0453 Рік тому

    But in code coverage the private method lines will still not get covered right

  • @Deepika_Durairaj
    @Deepika_Durairaj 5 місяців тому

    Can I use powermock with java 17?

  • @brianspinos
    @brianspinos 4 роки тому +1

    What are the dependencies (with versions) ?

  • @manishnegi2882
    @manishnegi2882 3 роки тому +1

    could you please share the dependency version that you used I got the exception "Class not prepared"

    • @Javatechie
      @Javatechie  3 роки тому

      Please check source code link in video description

  • @sureshdhulipudi600
    @sureshdhulipudi600 4 роки тому +1

    How to write power mockito for private methods and After writing unit test cases if we click "Coverage As" it should cover those private methods as well.
    thanks in advance :)

    • @Javatechie
      @Javatechie  4 роки тому

      Power mockito won't provide you mechanism for coverage
      You need to use Jacoco for coverage

    • @sureshdhulipudi600
      @sureshdhulipudi600 4 роки тому

      @@Javatechie, without any errors, test cases are executing but coverage is not happening for private methods in both IDE and Jacoco report.

    • @sureshdhulipudi600
      @sureshdhulipudi600 4 роки тому

      we are using activiti bpm with spring boot in our project, so there only protected methods in those classes, that methods directly call by activiti bpm.

  • @amargupta1728
    @amargupta1728 4 роки тому

    Nice but How can we run Power Mock with spring boot 2.3 and mockito? @RunWith is not available with Boot 2.3 so please suggest

    • @Javatechie
      @Javatechie  4 роки тому +1

      You can directly run using @SpringBootTesr

    • @amargupta1728
      @amargupta1728 4 роки тому

      @@Javatechie I tried but didn't get PowerMockito and its helper classes

  • @architbaba1
    @architbaba1 Рік тому

    Please autowire some class and give example for complex

  • @PankajKumar-qk8oh
    @PankajKumar-qk8oh 4 роки тому +1

    Helpful video👍

  • @aditigupta6870
    @aditigupta6870 Рік тому

    Is there a need to write init method here? You are doing mockinits on NotificationService class but there is no mock intialisations there, so what is the need of doing this?

    • @Javatechie
      @Javatechie  Рік тому

      Yes it requires only if you want to pre initialize something

  • @ravishakya8605
    @ravishakya8605 4 роки тому

    Hi, Can you suggest how to write test cases for protected method?

  • @MrMadhavsai
    @MrMadhavsai 5 років тому +1

    great explanation bro

  • @shanmugapriya3762
    @shanmugapriya3762 4 роки тому

    I want to access static method so I used powermockrunner class..while executing test case I'm getting like initialisation error.do I needs to add any configuration?.how to solve?

  • @pviswatmula
    @pviswatmula 4 роки тому +2

    In the addDiscount method, you are setting discount amount to price when the price is less than or equal to 1000. Should n't the discount amount be zero though.

    • @Javatechie
      @Javatechie  4 роки тому

      Yup you could add that

    • @pviswatmula
      @pviswatmula 4 роки тому

      @@Javatechie Nice tutorial except the logical error. Do you really want to give 100% discount when price is less than or equal to 1000?

  • @トゥアンファム
    @トゥアンファム 2 роки тому

    how to mock final class using power mock-api, bro?

  • @dattun976
    @dattun976 5 років тому +1

    hi bro, nice tutorial... can we do testing for inner class ? and also how to test lambda expression code ?

    • @Javatechie
      @Javatechie  5 років тому

      We are writing test case for outer class right so inner class will be execute by any member from outer class .
      So I don't think we need to write test case for it .but yes if you want to achieve code coverage then you must have to cover test case for it .

    • @dattun976
      @dattun976 5 років тому

      @@Javatechie I'm trying to cover the inner class while testing one service layer method,but the problem here is, Mockito is not covering the inner class code and simply it is skipping that inner class code .kindly suggest a way to solve this issue. and Thank u v much for the reply.

    • @Javatechie
      @Javatechie  5 років тому

      Dattu I will check and update soon

    • @dattun976
      @dattun976 5 років тому

      @@Javatechie Thank u :)

  • @dattun976
    @dattun976 5 років тому +1

    HI bro, Code coverage is showing 0 % when I use powermockito to run test cases. even I've tried to run your example but still showing 0% as code coverage. do we need to do any changes in configuration? if yes.. please reply ..

    • @Javatechie
      @Javatechie  5 років тому

      There is some issue with power mock and code coverage , I already reported a bug for it ...once I will get updates will let you know ok

    • @dattun976
      @dattun976 5 років тому +1

      @@Javatechie thank u bro for the reply :)

  • @shaikshasayyad131
    @shaikshasayyad131 5 років тому +2

    Could you please upload a complete spring boot-microservices application?

    • @Javatechie
      @Javatechie  5 років тому

      Yeah am planning to do that but before that I would suggest you to go through all spring Boot video where I explained each component of microservice like Eureka,zuul, hystrix etc...

    • @shaikshasayyad131
      @shaikshasayyad131 5 років тому +1

      I've completed watching them. I'm eager to see the complete microservice application.
      Thanks for a quick reply

    • @Javatechie
      @Javatechie  5 років тому

      @@shaikshasayyad131 cool will do soon

  • @CodeWithJava114
    @CodeWithJava114 2 роки тому

    getting error that "Failed to find the modifires field in method setInternalstate " pleae help

  • @shivakamlikar2144
    @shivakamlikar2144 5 років тому +2

    Bro... You can upload spring batch related junit test cases with power Mockoto

    • @Javatechie
      @Javatechie  5 років тому

      I don't think we need to write test case for spring batch With power mock
      You can go for mockito by mocking batch service

  • @subhojit1741984
    @subhojit1741984 Рік тому

    Hi,I am getting error "java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.lang.Object.finalize() throws java.lang.Throwable accessible: module java.base does not "opens java.lang" to unnamed module" while running in Intellij IDE.Kindly help

    • @AishwaryaB-t2i
      @AishwaryaB-t2i Рік тому

      Have you resolved it? Even I am getting the same error

  • @salamomran1507
    @salamomran1507 4 роки тому +1

    thank you sooo much

  • @SHIVAMSRIVASTAVSIEC
    @SHIVAMSRIVASTAVSIEC 2 роки тому

    I am getting unrooted test error please help me

  • @sairamreddy500
    @sairamreddy500 2 роки тому +1

    Can we mock enum methods?

    • @Javatechie
      @Javatechie  2 роки тому

      Yes we can

    • @sairamreddy500
      @sairamreddy500 2 роки тому

      Thank you for confirmation. Could you plz provide short video or code sample on it.

  • @nikhilraj9524
    @nikhilraj9524 2 роки тому

    I stuck because after using powermock also private method is being called.

  • @sushilkumarpandeypandey1087
    @sushilkumarpandeypandey1087 5 років тому +1

    why @InjectMocks is used why not @Autowired ?

    • @Javatechie
      @Javatechie  5 років тому +1

      @,Autowired will create a new object where injectMock will give u proxy object instead of actual object

    • @sushilkumarpandeypandey1087
      @sushilkumarpandeypandey1087 5 років тому

      can u explain the difference between proxy object and mock object

    • @Javatechie
      @Javatechie  5 років тому

      Mock object means proxy object

    • @sushilkumarpandeypandey1087
      @sushilkumarpandeypandey1087 5 років тому

      that means instead of @InjectMocks we can simply use @Mock also

  • @saketkumar6671
    @saketkumar6671 2 роки тому +1

    Code is Not visible

    • @Javatechie
      @Javatechie  2 роки тому

      Where in video or in git ?

  • @saktiranjanbehera7010
    @saktiranjanbehera7010 2 роки тому

    Thanks Bhai

  • @maheswarareddy6783
    @maheswarareddy6783 5 років тому

    when we should use fail() method in JUnit testing

  • @salauddinansari2072
    @salauddinansari2072 5 років тому +1

    Hello, Sir, I want to learn SpringBoot step by step please help me

    • @Javatechie
      @Javatechie  5 років тому

      Yeah sure please let me know your doubts

    • @salauddinansari2072
      @salauddinansari2072 5 років тому +1

      thanks for your reply. I'm a beginner but already know about Core java servlet. No doubt any but i want to know how to make a project using spring-boot and java

    • @Javatechie
      @Javatechie  5 років тому

      I would suggest you to learn spring first before moving forward to spring boot

  • @kuldeepmishra7889
    @kuldeepmishra7889 5 років тому +1

    Visibility is not clear

    • @Javatechie
      @Javatechie  5 років тому

      Kuldeep video resolution is 1080 HD
      Please check your internet connection ,if it is not strong then default it will play on 360 resolution.
      So try with good internet connection

  • @swadhin1985
    @swadhin1985 4 роки тому +1

    You are uploading video on youtube, but you don't know pom is very importing, you are not showing pom files, so this is not informative

    • @Javatechie
      @Javatechie  4 роки тому

      Yes I agreed Going forward I will show pom

    • @swadhin1985
      @swadhin1985 4 роки тому

      @@Javatechie Can you please upload a single video on spring boot, junit, mockito and power mockito with details pom, I hove you will upload thanks in advance