Spring Boot Testing a REST Controller with Unit, Integration & Acceptance Tests

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ต.ค. 2024
  • In this video we will be testing a Spring REST controller. We will be writing a full test suite with unit tests, integration tests & acceptance tests. We will be using JUnit5, Mockito, MockMvc & more. We will also be discussing the purpose of each type of test with their advantages & disadvantages.
    Source code: github.com/kri...
    Spring Initialiser: start.spring.io/

ความคิดเห็น • 68

  • @KrisFoster1
    @KrisFoster1  3 ปีที่แล้ว +23

    Unit testing starts: 1:40
    Integration testing starts: 11:25
    Acceptance testing starts: 21:50

    • @arihantdhobe
      @arihantdhobe 2 ปีที่แล้ว

      What do I need to do If I am writing a Batch Scheduler 15:08

  • @blankvoidsea
    @blankvoidsea 3 ปีที่แล้ว +7

    No more hours of scouring the Internet, this here is the good deal. Thanks!

  • @malg5547
    @malg5547 3 ปีที่แล้ว +16

    This is the clearest video i've seen on this topic. Thanks for the tutorial, was really helpful.

  • @xamael1989
    @xamael1989 2 ปีที่แล้ว +2

    Sincerely brother thanks for taking the time to make this tutorial its very clear and straight forward much love and appreciation from Tanzania

  • @federicobau8651
    @federicobau8651 ปีที่แล้ว +1

    5 min in and the video is way way more valuable than an entire series of some other youtubers that I wont name. I am an experienced programmer working with different languages and learning now java, but I can recognise who is really a developer and who is just a youtuber influencer with not real experience but the RGB keyboard and few screen unfortunally makes it look like they do. I can tell you are a real seasoned developer ! Either way thanks for these videos

  • @madhanwilliams1464
    @madhanwilliams1464 2 ปีที่แล้ว +3

    This video gave me a confidence to do unit and integration testing in my project. Thanks for this very neat and clear explanation

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว

      Glad you enjoyed, thank you!

  • @hadihadidi6059
    @hadihadidi6059 7 หลายเดือนก่อน

    Best tutorial out there about Testing. Thank you!

  • @oakinola
    @oakinola 4 หลายเดือนก่อน

    Amazing video!! Thank you so much...clearly explains the difference between all 3 tests.

  • @jesusrivas5812
    @jesusrivas5812 3 ปีที่แล้ว +1

    Best spring boot test video! Thanks for share this.

  • @Ddos2212
    @Ddos2212 2 ปีที่แล้ว +1

    You are a very good teacher. And I've seen a lot of teachers. Amazing job!

  • @lorenzobonanni8625
    @lorenzobonanni8625 3 ปีที่แล้ว +2

    Great Video, short but complete!

  • @OmarMendozaKS
    @OmarMendozaKS ปีที่แล้ว

    The best testing tutorial I've seen ever, congrats and ty

  • @mohammadahmedragab837
    @mohammadahmedragab837 2 ปีที่แล้ว +1

    your explanation is very clear and simple, I like it, please do more videos about spring boot integration and unit testing ✨

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว +1

      Thank you for the feedback!

  • @Женя-р8ц3й
    @Женя-р8ц3й 2 ปีที่แล้ว +1

    Man, it is awesome! I have been looking for your way of simple explanation of difficult thighs for two weeks) And finally it becomes clear for me. Thanx!!!

  • @drbulltrader9107
    @drbulltrader9107 2 ปีที่แล้ว

    I wanna say thank you for the crystal clear explanation.
    Good stuff, very useful.

  • @dima_kowalski
    @dima_kowalski 2 ปีที่แล้ว +2

    Your video is the best!!!

    • @KrisFoster1
      @KrisFoster1  2 ปีที่แล้ว +1

      Thank you @Dimon, glad you enjoyed!

  • @akhileshsharma5119
    @akhileshsharma5119 2 ปีที่แล้ว +1

    Simple and easy explanation.

  • @saichander8288
    @saichander8288 ปีที่แล้ว

    Thank you very much for the content ☺️

  • @MarkChan
    @MarkChan 2 ปีที่แล้ว +1

    Thank you so much! You've explained it so clearly and it was easy to understand!

  • @jd12345-m
    @jd12345-m 2 ปีที่แล้ว

    Amazing video. Thank you so much. Been looking for this for some time.

  • @nouribenz
    @nouribenz 2 ปีที่แล้ว +1

    That was super helpful as a new tester 👌 thank you man 💪

  • @natalysilva5815
    @natalysilva5815 3 ปีที่แล้ว +2

    Great tutorial, keep up with the good work! 😉

  • @francksgenlecroyant
    @francksgenlecroyant 2 ปีที่แล้ว

    Kris Foster always delivers good, quality and relevant content, thanks man!

  • @ogookafor2137
    @ogookafor2137 2 ปีที่แล้ว +1

    Liking and subscribing + notification THANK YOU!

  • @sunpacplussoftware5948
    @sunpacplussoftware5948 3 ปีที่แล้ว +1

    Thank you. Excellent tutorial!

  • @newaz9666
    @newaz9666 2 ปีที่แล้ว

    Thank you, It is very helpful.

  • @muratorhun8236
    @muratorhun8236 ปีที่แล้ว

    Very helpful, thanks!

  • @GuilhermeBressan
    @GuilhermeBressan 2 ปีที่แล้ว

    Great content. Thanks for the lesson bro! I'm migrating the backend of a project from PHP to Java, those tips really helped me.

  • @mr.j4840
    @mr.j4840 3 ปีที่แล้ว +4

    Super super super! But you can use autowired without creating contructor.

    • @KrisFoster1
      @KrisFoster1  3 ปีที่แล้ว +1

      That's a great point @MR. J, yes you can

  • @umutcanceyhan6781
    @umutcanceyhan6781 2 ปีที่แล้ว

    Thanks man your content really helped me, cheers! Keep up with the good work

  • @ariwansrisetya5714
    @ariwansrisetya5714 2 ปีที่แล้ว

    Thank you so much! This helped me tremendously.

  • @nelsonlzrz5826
    @nelsonlzrz5826 2 ปีที่แล้ว +1

    Muchas gracias por el tutorial! Very thanks, was helpful!

  • @devtools8704
    @devtools8704 3 ปีที่แล้ว +1

    Thank you, this was very clear

  • @tuenguyen8433
    @tuenguyen8433 2 ปีที่แล้ว

    Thanks for sharing! this video really useful

  • @bronxed3666
    @bronxed3666 2 ปีที่แล้ว +1

    amazing. thank you

  • @haditube
    @haditube 2 ปีที่แล้ว

    just brilliant. respeKt !

  • @symbolykos1810
    @symbolykos1810 3 ปีที่แล้ว +1

    Very good video! Thank you!

    • @KrisFoster1
      @KrisFoster1  3 ปีที่แล้ว

      Thank you for the feedback!

  • @Das.Kleine.Krokodil
    @Das.Kleine.Krokodil 2 ปีที่แล้ว

    Awesome!

  • @sujathamunjety2029
    @sujathamunjety2029 3 ปีที่แล้ว

    Very clear,Thanks

  • @alpavaidya412
    @alpavaidya412 2 ปีที่แล้ว

    Very informative

  • @ignacioDGP
    @ignacioDGP 2 ปีที่แล้ว

    Thanks!

  • @arihantdhobe
    @arihantdhobe 2 ปีที่แล้ว +2

    What do I need to do If I am writing a Batch Scheduler 15:08

  • @AmNotLegend
    @AmNotLegend 3 ปีที่แล้ว +5

    is @MvcTest not a Unit test? you mocked its dependencies. for the acceptance test, is it good to use Rest-Template to simulate invoking API from a client side?

    • @dusanristic4400
      @dusanristic4400 2 ปีที่แล้ว +1

      It's not necessarily; you still have an integration between the actual HTTP Request and the Controller that handles the request. The rest is mocked to narrow the test (make it cheaper).
      As they are cheaper than Acceptance Tests, I find them useful to test error scenarios such as 403, 404, 400 or any other business/validation logic that might be returned by the controller.

    • @christianwulf8224
      @christianwulf8224 2 ปีที่แล้ว +2

      I agree with you. It is a unit test, too. I would never call it an integration test. It does not integrate any two or more classes of the application.

  • @sergeychernikov6595
    @sergeychernikov6595 ปีที่แล้ว

    Hi! Could you tell what keyboard you're using? Some mechanical one? Sound is amazing:)

  • @frankoppermann1877
    @frankoppermann1877 2 ปีที่แล้ว

    Very nice video. How do you handle external service calls in acceptance tests. Are you using mocks or real calls?

  • @Das.Kleine.Krokodil
    @Das.Kleine.Krokodil 2 ปีที่แล้ว

    Why add a mock to the context? What will change if you use @Mock instead of @MockBean in the controller?

  • @themistocies50
    @themistocies50 ปีที่แล้ว

    For some reason, an integration test execution was faster for me than simple unit test...

  • @peshutanpavri1599
    @peshutanpavri1599 2 ปีที่แล้ว +1

    Very Good tutorial, but the MockMVC code is ugly & confusing

  • @brunosteffen8173
    @brunosteffen8173 3 ปีที่แล้ว +2

    Hey,
    cool Tutorial!
    Can you also work those with Springsecurity?
    I can't do the acceptance test without my credentials. My tests would even need to get data based on who is logged in... Can you still test those?

    • @KrisFoster1
      @KrisFoster1  3 ปีที่แล้ว +2

      Hi @Bruno Steffen, thank you for your comment! Things get a bit more complicated once spring security is added to the equation. There are a few different approaches I can think of that you could take:
      1. turn off spring security for the test. You could create a new spring security configuration that basically disables security for your endpoints. You could then use this configuration for your test. Here's a stackoverflow discussion where they're talking about how to do that stackoverflow.com/questions/23894010/spring-boot-security-disable-security.
      2. create a mock user before the test & authenticate as this user for your tests. This is probably a bit more complex but I think for acceptance tests this would be preferred because in the end, you want to make sure everything is working correctly.
      Sorry this answer probably doesn't tell you much more than you already know. Good luck!

    • @brunosteffen8173
      @brunosteffen8173 3 ปีที่แล้ว +2

      @@KrisFoster1 On a different note, can you run the springboot application without running the tests? all i could find were people saying you would need to skip plugins in the maven lifecycle... I thought if you run the application in your IDE, the maven lifecycle doesnt even get triggered.

    • @KrisFoster1
      @KrisFoster1  3 ปีที่แล้ว +1

      Yes if you run the application in from your ide or with mvn spring-boot:run from the command line the tests don't run. If you're using mvn compile /mvn package then they will

    • @mr.j4840
      @mr.j4840 3 ปีที่แล้ว +1

      Spring Security series pls!

  • @KAVYABN-d1z
    @KAVYABN-d1z ปีที่แล้ว

    controller call is not happening

  • @josecarlosrodriguez4574
    @josecarlosrodriguez4574 3 ปีที่แล้ว

    Short and concise, awful dude.

  • @theProgrammerX965
    @theProgrammerX965 2 ปีที่แล้ว

    Best tutorial out there about Testing. Thank you!