Тёмный

Spring Boot Tips : Part 4 - How to write Unit, Slice & Integration Tests in SpringBoot Applications 

SivaLabs
Подписаться 6 тыс.
Просмотров 3,6 тыс.
50% 1

One of the main reasons for popularity of SpringBoot is its amazing support for Testing.
In addition to writing unit tests for verifying the core business logic, Spring/SpringBoot provides support for testing Web Controllers and Spring Data Repositories as well using Slice Test annotations such as @WebMvcTest, @DataJpaTest etc. Also, we can write integration tests using @SpringBootTest annotation.
#sivalabs #java #springboot #tips #testing
In this video we are going to learn:
1. How to write Unit Tests using Mockito
2. How to Unit Test Web Controller
3. How to Unit Test Spring Data JPA Repository
4. How to Integration Test end-to-end flow.
In addition to this we are going talk about some good practices of testing as well.
Timestamps
00:00 Intro to SpringBoot Tips Part 4 - Testing
00:40 Types of tests for 3-tier applications
03:39 Project Setup that we are going to test
07:16 Write Unit Tests using Mockito
17:35 Write Web Slice Test for Controllers
24:10 Write Repository Slice Tests for Spring Data JPA Repository
33:30 Write Integration Tests
36:52 A common mistake developers do while writing Controller tests
40:10 Summary
🚀 SpringBoot Tips Series Playlist • Spring Boot Tips
Blog: sivalabs.in
Twitter: / sivalabs
Sample Code: github.com/sivaprasadreddy/si...

Наука

Опубликовано:

 

21 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 19   
@antonygenil
@antonygenil 2 года назад
Thanks a lot Siva for coming up with this well articulated technical material. I appreciate all you do !
@civerduche
@civerduche 2 года назад
This is the most accurate, easy and well explained Spring Boot testing video I have ever seen (and I have seen a lot xD). You are even explaining shortly and clear the most important reason for not using field injections! This resource is a treasure, thanks a lot :^]
@sivalabs
@sivalabs 2 года назад
Thanks for your kind words 🙏
@manjugh6812
@manjugh6812 2 года назад
This video cleared many of misunderstandings regarding test framework.
@MadhusudanBhosale
@MadhusudanBhosale 2 года назад
Thanks Siva for this insightful video..
@ravi54217
@ravi54217 2 года назад
Thanks a lot for creating such amazing videos. 🙏
@majdsaadaoui2123
@majdsaadaoui2123 2 года назад
God damn! How this is not at the top of RU-vid's search page? Wasted a couple of days on bullshit tutorials about the Testing topic but this one saved my life in the end! 😅
@sivalabs
@sivalabs 2 года назад
Thanks for your kind words. You made my day 😀
@matheusemm
@matheusemm 2 года назад
Great content! Another situation when it is better to use the Mockito API instead of MockitoExtension to setup mocks and the object under test (OUT) is when the OUT depends on generated code and you don't want to mock that code. An example: I regularly use MapStruct to generate mappers between different data representations in my projects. MapStruct automatically generate code based on annotations and the generated beans are injected as dependencies in my OUTs. I don't want to mock a generated mapper because I don't want to test MapStruct code generation. In this case my code is similar to: private ADependency aDependency; private SomeMapper someMapper; private ObjectUnderTest out; @BeforeEach void setup() { aDependency = Mockito.mock(ADependency.class); someMapper = new SomeMapper(); // just instantiate whatever MapStruct generates out = new ObjectUnderTest(aDependency, someMapper); }
@suddatshende5548
@suddatshende5548 2 года назад
So much enthusiasm and energy while explaining even though it is 3:30 AM. you wake up early or go to bed? in previous videos also i observed u created videos at early morning 4-5 AM
@sivalabs
@sivalabs 2 года назад
Recording videos during day time is a big challenge due to noise from surroundings , kids playing at home etc...so usually I go to bed early and wake up early. Early morning 4 to 6 is my main focus time for any activities that need concentration.
@suddatshende5548
@suddatshende5548 2 года назад
@@sivalabs Thats amazing, Also would love to know from you, how u manage office work, family etc and you are active on twitter(i follow u) also. as i am growing in my career, finding it difficult to balance between work and personal life. might learn something from your experience.
@mehdimohammadi1623
@mehdimohammadi1623 2 года назад
Is it best practice to include classes in @SpringBootTest? As I have experienced without filtering classes (slicing) SpringBootTest starts the entire context including the resources which does not have anything to do with our test. This means when we have so many tests we will face performance issues also.
@sivalabs
@sivalabs Год назад
@SpringBootTest is designed for running integration tests which loads the entire application and tests some behaviour. If your intention is to test specific component then the slice annotation(@WebMvcTest, @DataJpaTest etc) should be used. You can also create custom slices to load a subset of components.
@stevend1070
@stevend1070 2 года назад
Regarding your comments on InjectMocks. I really like that the Extension bringing in strict stubbing. It will throw an error for unnecessary stubbing. That is why I always use this. It guides me while doing TDD.
@sivalabs
@sivalabs 2 года назад
Good point. To get benefit of both strict stubbing and avoid the problems I mentioned in the video, we can use MockitoExtension to create mocks using @Mock and create the subject under test using "new" operator instead of @InjectMocks.
@sivalabs
@sivalabs 2 года назад
Thanks for bringing up this Steven. I have created an issue (github.com/mockito/mockito/issues/2648) in Mockito repo regarding this and they realised they haven't provided support for enabling Strict stubbing while using Mockito.mock(). They asked me if I want to contribute this feature and I did (github.com/mockito/mockito/pull/2650). This will be available in Mockito 4.6.0 release.
@fatemehsalem3850
@fatemehsalem3850 Месяц назад
I added @Sql({"/schema.sql"}) to create the tables and sequences because I encountered an error: "Sequence 'PRODUCT_ID_SEQ' not found." This annotation ensures that the SQL script is executed once before the Spring Boot JUnit test class runs.
@princegovind
@princegovind Год назад
Bro i tried slicing with some small tweaks , but am end up with java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java, any help bro
Далее
Software Testing with Spring boot and Mockito Framework
1:14:12
Elon Musk fires employees in twitter meeting DUB
1:58
🚀  TDD, Where Did It All Go Wrong (Ian Cooper)
1:03:55
Просмотров 554 тыс.
Лучшие кожаные чехлы для iPhone
1:00
Choose a phone for your mom
0:20
Просмотров 7 млн
Собираем комп за 500 000 рублей!
6:44:35