Spring Boot Testing Basics: How to Unit Test & Integration Test REST Controllers

  Рет қаралды 162,806

Dan Vega

Dan Vega

4 жыл бұрын

In this tutorial, I want to discuss some basics of testing a rest controller in a Spring Boot application. This came up because of a tweet I sent out recently asking developers to stop calling a certain test a unit test. The reason I said this is because whenever you start involving Spring and the whole request & response lifecycle its no longer a unit test. In this demo, I will create a new Spring Boot application, create a simple rest controller and then show you how I would Unit & Integration test that controller. If anything this is just meant to spark a discussion so I would love to hear your thoughts on the subject.
When you have completed this tutorial you should understand:
✅ Create a new Spring Boot Application
✅ Create a new Spring Boot Rest Controller
✅ Create a Unit Test
✅ Create an Integration Test
✅ Run a basic test in a Spring Boot Application
✅ How to use JUnit 5 in a Spring Boot Integration Test
🔗Resources & Links mentioned in this video:
Source Code: github.com/danvega/unit-vs-int
Martin Fowler Unit Test: martinfowler.com/bliki/UnitTe...
👋🏻Connect with me:
Website: www.danvega.dev
Twitter: / therealdanvega
Github: github.com/danvega
Instagram: / thedanvega
LinkedIn: / danvega
Coffee & Code: www.danvega/dev/newsletter
SUBSCRIBE TO MY CHANNEL: bit.ly/2re4GH0 ❤️

Пікірлер: 114
@DanVega
@DanVega 4 жыл бұрын
What is your definition of a Unit & Integration Test?
@Anbu_Sampath
@Anbu_Sampath 4 жыл бұрын
I agree with your definition of Unit and Integration Test. For Controller and Data layer, spring boot gives nice mock to test actual annotation and spin up minimal context. Because those layers we are not keeping much business logic.
@pvrsouza
@pvrsouza 4 жыл бұрын
I really never understood why using Unit Test on controllers. For each request a controller usually does a lot of steps like deserialize, validate, translate exceptions, etc its almost impossible do cover that on Unit Tests. IMHO, Unit Tests on controller (http layer) is just to verify business logic has been called and i can't see relevant value on it. Maybe i'm wrong but I really believe it's better build just Integration Tests for HTTP layer.
@Cassio81863636
@Cassio81863636 2 жыл бұрын
@@pvrsouza I agree with you. Usually in projects I work with, Controllers are only tested in Integration tests. and kinda solves coverage so
@handsome_man69
@handsome_man69 Жыл бұрын
There is no hard line between a Unit Test and an Integration Test. If you wanted a pure unit test then you would have to mock every external dependency, which would mean even mocking "String" objects. SUddenly It gets completely absurd. I have been in projects where they have written tests that are about 20 lines long, and that have tested exactly ONE line of code. The value of tests like this are next to ZERO. In my opinion unit tests are generally useless, unless you need to test complex algorithms or things like utils methods like DateUtils and that kind of thing. As soon as you start mocking too much you get tests which have no value. Integration Tests are much better because they more closely resemble Use Cases. As long as the performance/speed of these tests is good (TIP: use in memory databases!!!!! ), then you should focus on them over unit tests. Writing a test that goes from the RestController to the DB is excellent. It perfectly captures a Use Case. As long as the performance is fine..... do it like that!!!!!
@g.v.m7935
@g.v.m7935 Жыл бұрын
Isnt the @Test a spring notation? Refering to the start of the video where you said spring wasnt involved yet. Just out of curiosity so I know if I understand spring well enough in the basics.
@khalilgreenidge981
@khalilgreenidge981 3 жыл бұрын
I literally spent 5 hours trying to achieve this. At 2 am I found your video. Thank you so much!
@DanVega
@DanVega 3 жыл бұрын
Glad it helped!
@shashidhar71
@shashidhar71 3 жыл бұрын
Your definition is right. Unit - Single entity. Integration - How it works with entire system. Imagine if you have missed @RestController annotation, unit will pass but int fails.
@brianocorner5604
@brianocorner5604 2 жыл бұрын
WoW..!! Dan!!! Thanks for sharing an amazing tutorials and good explanation. It is 4:00am here. Thanks to you, my searching ends here.
@haroonafridi1802
@haroonafridi1802 2 жыл бұрын
100% agree with your thoughts about unit testing and integration testing
@teotsi21
@teotsi21 4 жыл бұрын
Great video, couldn't figure out the Integration Testing, thanks!
@joshuaecht
@joshuaecht 3 жыл бұрын
CLEVELAND! Go Browns! :) This is a great vid. I went to We Can Code IT and we learned this, but it helps refresh my learning of Testing.
@saeidkazemi8480
@saeidkazemi8480 3 жыл бұрын
Thanks a millioooon for the great explanation and illustration
@mateovansweevelt5257
@mateovansweevelt5257 2 жыл бұрын
What an absolute beautiful video man!
@muditshukla
@muditshukla 2 жыл бұрын
Great explanation. Thank you Dan
@ChildrenOfDemiurgos
@ChildrenOfDemiurgos Жыл бұрын
Plain and simple. Thank you! Awesome if you could make a more in depth video about this.
@arjungupta4672
@arjungupta4672 2 жыл бұрын
Quick and fast way to understand difference and learn 👍
@procrastinator24
@procrastinator24 Жыл бұрын
Nice and clean explanation, thank you!
@DanVega
@DanVega Жыл бұрын
Glad it was helpful!
@jacsdev
@jacsdev Жыл бұрын
thanks for sharing this kind of content!
@kaiser92600
@kaiser92600 Жыл бұрын
Thank you for your tuto, very good work
@somtovitus
@somtovitus Жыл бұрын
this is beautiful, thank you
@raphaelzamora7974
@raphaelzamora7974 2 жыл бұрын
thank you for this! I’m supposed to be writing a unit test before I move on to integration testing and it seems like I jumped the gun
@natetolbert3671
@natetolbert3671 3 жыл бұрын
I may be wrong about this it's been a while since I've "looked under the hood" in spring, but doesn't the spring framework, as soon as it sees the at test annotation, create its own mock dependency. I'm thinking that I remember it marks the entire environment so that it can run the tests. Also one key point about integration tests just because a test _includes_ integration (via stubs,mocks, etc) that doesn't make it an integration test. When you write a unit test that contains mock dependencies on neighboring classes, you're not writing assertions to check that those integrations are working as intended, as you would in an integration test. in fact, with mocks the integration isn't even there. The entire point of frameworks like makito is to take the 'integration' factor out of the dependencies. In summary, the point I'm trying to make is that while unit tests may contain dependencies, and occasionally with third-party dependencies, may even contain integration, integration tests actually test said integration and fail if it doesn't behave as expected. Just my two cents. Have a great day. Btw, have I commented on this video before? This whole thing is feeling eerily deja Vu to me...
@manuellarrota
@manuellarrota 2 жыл бұрын
Gracias mi pana, muy bien explicado un abrazo!
@catsbite3634
@catsbite3634 2 жыл бұрын
It’s 5:17 am and I found this. Thanks for ur video
@legacylifey182
@legacylifey182 2 жыл бұрын
Thank you so much!
@FellTheSky
@FellTheSky 3 жыл бұрын
This is the best video about testing. Watched so many but yours let everything clear! (im junior)
@DanVega
@DanVega 3 жыл бұрын
Wow, thank you! That is a great compliment. Is there anything else in testing you would like me to cover? I should have a video on Spring Data Slice Testing coming out this week.
@FellTheSky
@FellTheSky 3 жыл бұрын
@@DanVega Well usually every video has details of how to create a rest controller or other types of controllers. I get that, but when you apply for a junior job, they assume you already know testing. Thats not the case. I have a controller that returns a DTO with this format: {"idUser":0,"email":null,"firstName":null,"lastName":null,"loans":null}(loans is a list) Well i have no idea of how to test it. Should i recreate the entire method in the test class? I dont think so because in that case whats the difference with just using postman. Maybe this sound dumb to you, but ive started to code 6 months ago, and right now im applying for spring junior dev (fintech). They want me to know: ORM, Spring Boot, SQL, Hibernate, GitHub, Maven, testing and logs. I think 1 minute at the end of the explanation quickly showing how to test it would be great, but i get it, thats a low of added work.
@batasheemum
@batasheemum 3 жыл бұрын
Dan Vega It would be great if you can do basic spring boot testing videos for junior automation testers/developers etc.
@batasheemum
@batasheemum 3 жыл бұрын
Would be grateful
@Utub-qc2cz
@Utub-qc2cz Жыл бұрын
Thank you Dan for this content. Would you please share your experience with more advanced examples of integration tests using DdUnit ?
@VISITLosAngeles
@VISITLosAngeles 2 жыл бұрын
Thank you so much sir :)
@ABHISHEKMISHRA-ib5vo
@ABHISHEKMISHRA-ib5vo 3 жыл бұрын
Grear Dan! 😊
@aceisastud
@aceisastud 3 жыл бұрын
Love this video BUT love the shirt even better! Go Browns! From a fellow Ohioan to another thanks for the videos!
@DanVega
@DanVega 3 жыл бұрын
OH- Thank you Chad! I actually made that shirt myself. I have a bunch of Browns shirts so I will try working them in.
@luvcode567
@luvcode567 3 жыл бұрын
Really nice one. It helps in understanding the between unit test and integration test. can you make more videos on integration test. Also looking for test using Mockito
@muluadamtemesgen5993
@muluadamtemesgen5993 2 жыл бұрын
That is very interesting, could you make some video on how to test services and repository ?
@rawdiarieswithsunil
@rawdiarieswithsunil 3 жыл бұрын
Pretty clear.. can extend the video series for writing integration for Mapper, Converter, like all different models..? That will clear all the stuff..!
@abeplus7352
@abeplus7352 3 жыл бұрын
Thank you ! I had an argument the other day with one of the devs about unit testing in go lang . spinning up an entire http server to just do a controller test is stupid that's an integration test not unit .
@natetolbert3671
@natetolbert3671 3 жыл бұрын
As I said above, it doesn't depend on what you're running in a test it depends on what you're testing. You can start up the whole damn application and call every single method in your unit, but if you're assert methods are only testing the functionality inside your unit, in this functionality is not being changed by anything else that is happening, it is still just a unit test. One more time, just for clarity, to be an integration test, it has to __test__ that the units are integrating as expected, and fail if they are not. Otherwise it is just a unit test. Simple as that. If we're going to start naming tests based on what they include, then by this logic, every test is a unit test because they all include units...
@abhishekpal7291
@abhishekpal7291 2 жыл бұрын
I have spent a whole night on this thing!
@abhishekpal7291
@abhishekpal7291 2 жыл бұрын
@@natetolbert3671 But my teammates are checking the be rest controller just as an normal classs i.e. calling method and asserting the return value without worrying about checking get/post mapping. Should I go with mockmvc or just normally test like others have done.
@armanhosyan5660
@armanhosyan5660 2 жыл бұрын
Thanks a lot.
@taprandich
@taprandich 3 жыл бұрын
Thank so easy to understand would be nice if you advance this topic on the more complicated methods as well
@foruvasanth
@foruvasanth 2 жыл бұрын
question on integration tests - Whats the best practice if this endpoint need to fetch data from database, do we spin up in memory like H2 or hit the db by running these tests in Dev??
@sarramerabet2611
@sarramerabet2611 3 жыл бұрын
thank you so much
@nguyentoan9012
@nguyentoan9012 3 жыл бұрын
I solved the problem " java.lang.IllegalStateException: Failed to load ApplicationContext" by repacing 2 annotations of the test class with @SpringBootTest and @AutoConfigureMockMvc.
@Wegman7
@Wegman7 3 жыл бұрын
I'm new to Java and I have no idea why this works/ how it's different, but this worked for me too lol.
@rabiebelkaida
@rabiebelkaida 2 жыл бұрын
Thank you bro it's worked well :)
@mtc539
@mtc539 2 жыл бұрын
Hi Dan, great video. Is it fair to assume that these kinds of integration tests involving the Spring components ie. @WebMvcTest are excessive? I'm sure the Spring devs test their own MVC-related components, so aren't we just testing something they're already testing themselves before making releases of their libraries? If my assumption is correct here, then is the unit test you made at first in this video all that is needed to ensure the code that YOU wrote is working properly? I'm still very new to TDD in general and I'm trying to wrap my head around some of these scenarios and would love your input here. Thanks! -Matt
@handsome_man69
@handsome_man69 Жыл бұрын
no because the WebMvc test is testing the request parameters you are defining in the rest controller method. This logic cannot be covered by a vanilla test.
@manufactured_reality
@manufactured_reality 2 жыл бұрын
Do you see any benefit in doing both unit tests and integration tests with mockito?
@livb4139
@livb4139 2 жыл бұрын
Is this integration test or unit test? Should you split both on their own folders or something? Do you have a video like for testing this but also using a service and a repository to do stuff with a db?
@MarkGallo-tt8pt
@MarkGallo-tt8pt 3 жыл бұрын
My assert equals fails because of the URI returns empty with body = null. When I do a get command from the URI it does show me the string I would want to compare but it doesn’t work for the assertEquals
@aussieclicks96
@aussieclicks96 4 жыл бұрын
Thank you
@DanVega
@DanVega 4 жыл бұрын
Thank you for watching!
@smaug9833
@smaug9833 3 жыл бұрын
Hey could you show us how to test REST mappings that must pass through Spring security configuration?
@JuanCamiloGutierrezFuentes
@JuanCamiloGutierrezFuentes 2 жыл бұрын
thanku man i love u
@DanVega
@DanVega 2 жыл бұрын
Happy to help
@n3x4r3
@n3x4r3 3 жыл бұрын
But how can do an integration test when my micro service use other 2 micro service to get me a response ? I have to mock the response from the others micro services ? or what , I have a huge doubt about it
@johnvoorhees2331
@johnvoorhees2331 Жыл бұрын
I just found our videos 2 weeks ago(Love them) Would it be possible to show how to create proper JUNITs over the Spring Repo Layer. A lot of mocking and i get confused
@emaayan
@emaayan 2 жыл бұрын
as far as i know unit tests are designed to test application logic, so if you unit tests a controller that means you have application logic inside a controller which as far as i know , is a big no no.
@demidrek-heyward
@demidrek-heyward 3 жыл бұрын
thanks!
@DanVega
@DanVega 3 жыл бұрын
Welcome!
@FilipCodes
@FilipCodes Жыл бұрын
What is the difference between unit test and component test?
@shakib_04
@shakib_04 2 жыл бұрын
you missed a thing at 10:50 , wheren webmvctest annotation does not create bean for @component , service and reporsitory
@ildar5184
@ildar5184 Ай бұрын
It feels like the distinction between unit and int tests is fuzzy. Because there're different levels of how many levels of your system you can include in your tests. The easiest scenario, when you test just one component, with its integrations with other components replaced by mocks, is unit testing, that's pretty much clear. For integration tests, you can test any level of integration, from the simplest, like just plugging the web framework as in your example, and still mocking stuff like database and calls to other microservices, up to spinning up your entire ecosystem, with all layers included, e.g. locally via docker and test containers. Are these types of testing all integration tests? What I've got is that it's still a matter of debate, some people consider the first type (like in your example) to still be unit testing. And I guess the reason for the debate is that it kinda has features of both.
@HazemAls
@HazemAls 2 жыл бұрын
wow thanks
@DanVega
@DanVega 2 жыл бұрын
You're welcome
@enesb4797
@enesb4797 Жыл бұрын
Hello, I don't have the get() method in 14:55. When I click "Import static method" it gives me lots of methods to choose.
@DanVega
@DanVega Жыл бұрын
I often am confused on where this is coming from as well. Here is the static import for that import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@enesb4797
@enesb4797 Жыл бұрын
@@DanVega thank you so much!
@EdsonLima-hp6ew
@EdsonLima-hp6ew 8 ай бұрын
nice :)
@DanVega
@DanVega 8 ай бұрын
Thanks!
@mykyta1235
@mykyta1235 Жыл бұрын
It does not matter what you think of the component or unit , it doesn't matter if you don't count it as valuable. Integration tests - tests for the stand alone components interaction between their public interfaces. Unit tests - are tests for the smallest testable parts of the application. That's it.
@kekiir
@kekiir Жыл бұрын
Cool to make the first step by step part (Request builder request...., MvcResul result...., assertEquals(...) with this is much more understandable the code after mvc.perform....
@diegoramos27
@diegoramos27 2 жыл бұрын
Hi Sir why do we need to add HelloController.class inside the @WebMvcTest? is this annotation not capable of doing component scan aromatically thanks ?
@DanVega
@DanVega 2 жыл бұрын
You don't have to but if you to avoid scanning and be specific it could help performance in a larger app
@marcop.7278
@marcop.7278 2 жыл бұрын
So how do you test when you return JSON and you want to test that e.g. variable message of this JSON contains a String, without asserting it to a String per se? I'm asking because I'm doing tests where the content of the String is random and cannot be guessed beforehand, we just know it should not be null.
@DanVega
@DanVega 2 жыл бұрын
Do you have an example or could you post one on Github where I could take a look at this?
@Kubakaiser
@Kubakaiser Жыл бұрын
If you test the whole Spring application, with the *@SpringBootTest* , without mocking any beans, is it called an integration test, or acceptance test?
@huhuboss8274
@huhuboss8274 4 жыл бұрын
I always get "java.lang.IllegalStateException: Failed to load ApplicationContext" :/
@Next80sProject
@Next80sProject 3 жыл бұрын
maybe you need to add your base package to the @SpringBootApplication(scanBasePackages = "your.base.package") int the UnitVsIntApplication class.
@nguyentoan9012
@nguyentoan9012 3 жыл бұрын
I solved that problem by repacing 2 annotations of the test class with @SpringBootTest and @AutoConfigureMockMvc.
@xmo3608
@xmo3608 3 жыл бұрын
Isn`t @ExtendWith already included by @WebMvc?
@DanVega
@DanVega 3 жыл бұрын
I believe it is now, yes. You can always cmd or ctrl+click the annotation to take a look.
@Das.Kleine.Krokodil
@Das.Kleine.Krokodil Жыл бұрын
In what cases in tests does it make sense to use a web server, and not MockMvc?
@DanVega
@DanVega Жыл бұрын
Thank you for the question. When you want to do a true end to end test and simulate a production like environment I would use a web server.
@Das.Kleine.Krokodil
@Das.Kleine.Krokodil Жыл бұрын
@@DanVega thanks a lot
@dadams1989
@dadams1989 Жыл бұрын
Where did you get your shirt? Love it!
@DanVega
@DanVega Жыл бұрын
I actually made that myself when I briefly got into the t-shirt side hustle game 🤣
@Das.Kleine.Krokodil
@Das.Kleine.Krokodil Жыл бұрын
Does this mean that the controller cannot be unit tested?
@DanVega
@DanVega Жыл бұрын
It can be..my question to you would be if you wrote a unit test for that controller what value would it be giving you?
@Das.Kleine.Krokodil
@Das.Kleine.Krokodil Жыл бұрын
@@DanVega Possible shorter execution time.
@JafarAli-ly1ue
@JafarAli-ly1ue 3 жыл бұрын
what about service layer unit testing??
@DanVega
@DanVega 3 жыл бұрын
The service layer could mean different things to different people. What are you trying to test in your service layer?
@northdankota
@northdankota 3 жыл бұрын
​@@DanVega if our service layer talks repository and controller layer, wich testing should we do unit ? integration ? or both?
@hurle0409
@hurle0409 3 жыл бұрын
@@northdankota Yeah this is real life scenario. But can't find useful test case for it. Did you find?
@northdankota
@northdankota 3 жыл бұрын
@@hurle0409 i am not sure actually, which or why to choose these options
@apritellolives3920
@apritellolives3920 2 жыл бұрын
Thats how I feel, I feel integration and e2e testing is more valuable than unit testing especially if you developing an IT website that pretty much just manages and displays data.
@AmNotLegend
@AmNotLegend 3 жыл бұрын
@ExtemdWith(SpringExtension.class) is redundant
@johnborys9868
@johnborys9868 2 жыл бұрын
When does anyone ever instantiate a controller in production code? You're righting a test for a scenario that will never occur. This "unit" test seems redundant and totally without value. You are testing your string concatenation. (Logic). A controller should not have business logic. It should delegate that to the service layer.
@Das.Kleine.Krokodil
@Das.Kleine.Krokodil Жыл бұрын
But the controller can contain validation
@handsome_man69
@handsome_man69 Жыл бұрын
There is no hard line between a Unit Test and an Integration Test. If you wanted a pure unit test then you would have to mock every external dependency, which would mean even mocking "String" objects. SUddenly It gets completely absurd. I have been in projects where they have written tests that are about 20 lines long, and that have tested exactly ONE line of code. The value of tests like this are next to ZERO. In my opinion unit tests are generally useless, unless you need to test complex algorithms or things like utils methods like DateUtils and that kind of thing. As soon as you start mocking too much you get tests which have no value. Integration Tests are much better because they more closely resemble Use Cases. As long as the performance/speed of these tests is good (TIP: use in memory databases!!!!! ), then you should focus on them over unit tests. Writing a test that goes from the RestController to the DB is excellent. It perfectly captures a Use Case. As long as the performance is fine..... do it like that!!!!!
@robertpeschke7746
@robertpeschke7746 Жыл бұрын
Sorry, but your background music is so annoying and distracting.
@DanVega
@DanVega Жыл бұрын
Agreed it can be. I was trying new things 🤷‍♂️
@zaycef03
@zaycef03 3 жыл бұрын
That is one useless video.. does not explain what to do with service and DAO dependencies..
@DanVega
@DanVega 3 жыл бұрын
Sorry this video didn't meet your expectations but let's discuss your comment. This video wasn't met to cover everything you need to know about writing unit and integration tests. This was simply my thoughts on the two types of test and how the lines are blurred. If you're interested in seeing me create a tutorial on what to do with service and DAO dependencies in a certain type of test this would have been a much better comment "Thanks for the tutorial Dan... I am a little confused on what do with my service and DAO dependencies in a unit test, any chance you can break that down for me."
Spring Boot Testcontainers - Integration Testing made easy!
33:31
Spring Boot Testing - ** Batteries Included 🔋🔋
41:12
Dan Vega
Рет қаралды 16 М.
ПЕЙ МОЛОКО КАК ФОКУСНИК
00:37
Masomka
Рет қаралды 10 МЛН
Follow @karina-kola please 🙏🥺
00:21
Andrey Grechka
Рет қаралды 26 МЛН
Dynamic #gadgets for math genius! #maths
00:29
FLIP FLOP Hacks
Рет қаралды 18 МЛН
СҰЛТАН СҮЛЕЙМАНДАР | bayGUYS
24:46
bayGUYS
Рет қаралды 734 М.
We can now write scripts using these languages, but is it worth it?
8:28
Java Unit Testing with JUnit - Tutorial - How to Create And Use Unit Tests
21:35
Getting started with Spring Boot AOT + GraalVM Native Images
21:03
When To Unit, E2E, And Integration Test
14:58
ThePrimeTime
Рет қаралды 86 М.
Test Driven Development (TDD) | Crash Course | 2020
43:53
Amigoscode
Рет қаралды 122 М.
Test Driven Development (TDD) in Spring
51:09
Dan Vega
Рет қаралды 20 М.
Testcontainers - From Zero to Hero. By @MarcoCodes
1:01:29
IntelliJ IDEA, a JetBrains IDE
Рет қаралды 71 М.
Spring по верхам: Тестирование REST-сервиса
44:32
Уголок сельского джависта
Рет қаралды 11 М.
Индуктивность и дроссель.
1:00
Hi Dev! – Электроника
Рет қаралды 1,5 МЛН
iphone fold ? #spongebob #spongebobsquarepants
0:15
Si pamer 😏
Рет қаралды 104 М.
Apple, как вас уделал Тюменский бренд CaseGuru? Конец удивил #caseguru #кейсгуру #наушники
0:54
CaseGuru / Наушники / Пылесосы / Смарт-часы /
Рет қаралды 4,4 МЛН
Куда пропал 3D Touch? #apple #iphone
0:51
Не шарю!
Рет қаралды 595 М.