Тёмный

KotlinConf 2018 - Best Practices for Unit Testing in Kotlin by Philipp Hauer 

JetBrains
Подписаться 202 тыс.
Просмотров 57 тыс.
50% 1

Recording brought to you by American Express americanexpres...
Unit Testing in Kotlin is fun and tricky at the same time. We can benefit a lot from Kotlin's powerful language features to write readable and concise unit tests. But in order to write idiomatic Kotlin test code in the first place, there is a certain test setup required. We'll talk about test lifecycles, mocking challenges, proper assertion libraries, the power of data classes and about spring integration. This talk contains best practices and guidelines to write unit test code in Kotlin that is idiomatic, readable, concise and produces reasonable failure messages.
About the Presenter:
Philipp Hauer works as a team lead for Spreadshirt in Leipzig, Germany. He focuses on developing JVM-based web applications and is enthusiastic about Kotlin, clean code, architectures and the sociology of software development. Moreover, Philipp is a keen blogger and tweets from time to time.

Наука

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

 

13 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 18   
@zhou7yuan
@zhou7yuan 4 года назад
My First Test in Kotlin... [0:53] (Agenda) [2:17] Recap: Idiomatic Kotlin Code [2:34] Test Class Lifecycle [3:29] JUnit4: always new test class instances [3:34] JUnit4: static for the initial setup code [4:22] JUnit5 to the Rescue [5:34] JUnit5: Reuse the Test Class Instance [6:10] @TestInstance(TestInstance.Lifecycle.PER_CLASS) JUnit5: Change the Lifecycle Default [8:55] src/test/resources/junit-platform.properties: junit.jupiter.testinstance.lifecycle.default = per_class Test Names and Grouping [9:31] Backticks [9:36] @Nested Inner Classes [10:30] Kotlin Test Libraries [11:22] Being Spoilt for Choice [11:45] personal choice: JUnit5 + MockK + AssertJ Test-Specific Extension Functions [13:19] Mock Handling [14:46] Classes Are Final by Default [14:50] Solutions: Interfaces / open explicitly / Mockito: incubating feature / MockK MockK [15:50] Does Test Speed Matter? [18:38] Don't Recreate Mocks [19:10] Create Mocks Once, Reset Them [19:36] init() { clearMocks(repo, client) } Handle Classes with State [20:28] Spring Integration [21:31] All-Open Compiler Plugin [22:52] `org.jetbrains.kotlin:kotlin-maven-allopen:${kotlin.version}` spring Constructor Injection for Spring-free Testing [23:17] Utilize Data Classes [24:30] Data Classes for Assertions [24:35] self-explanatory [26:07] Helper Function for Object Creation [28:38] Data Classes for Parameterized Tests [31:47] Conclusion [33:55] Best Practices for Testing in Kotlin [34:24]
@ky5069
@ky5069 5 лет назад
Unrelated, but I love the intro. It has to be only intro I wish that was longer.
@notnanomercy
@notnanomercy 4 года назад
Why junit5 is not default on gradle?
@KlitosKyriacou
@KlitosKyriacou 2 года назад
Can you explain the bit starting at 19:05. Since each test case creates a new instance of the class, all of the `val`s will be created for each test with a new mock instance - exactly the same number of mockk() calls as using `lateinit var` and setting the values in the `@BeforeEach` method. So the difference in timings must be due to something else, not due to recreation of mocks.
@DB-cb2fu
@DB-cb2fu Год назад
Hi Philipp, I just stumbled over your video. I like the idea to get rid of @TestInstance(PER_CLASS). But one question remains open, how could I close a DB connection (or similiar) after every test of this class was run? Do I need then still the companion object?
@AnnaTheFallMaiden
@AnnaTheFallMaiden 4 года назад
21:20 could you not simply use a custom getter to create a factory? you would have to obtain an instance in your test method, but that would always be reset
@nileshmiskin5711
@nileshmiskin5711 4 года назад
Long way to go! That's why Spock really shines!
@Wavesonics
@Wavesonics 5 лет назад
Great talk, really helpful
@70ME3E
@70ME3E 5 лет назад
gr8 stuff!
@wiseAss
@wiseAss 6 лет назад
Vielen dank Philipp! I was live streaming about TDD and writing tests for suspending functions this morning, but I've already seen a number of things I can improve. I didn't even know about JUnit 5 lol... In any case, I'm glad to say that Unit Testing in Kotlin is now officially as easy as my mature set up with Java used to be. Definitely some hiccups to get set up, but as long as I can Test coroutines, I'll be a happy coder. If anyone's curious about writing tests for coroutines and TDD in general, consider checking this out: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-MZIFSrETRek.html
@Massenhaft1
@Massenhaft1 5 лет назад
Maybe you should try spek2...its also nice :)
@JustTheHighlights
@JustTheHighlights 4 года назад
35:36 - phauer.com/2018/best-practices-unit-testing-kotlin/ 2019 blog - phauer.com/2019/modern-best-practices-testing-java/
@34adse3
@34adse3 5 лет назад
A good talk! But the problem is that you're converting traditional Java approach 1:1 into Kotlin, instead of rethinking the whole testing problem in Kotlin terms. If you use annotations, then instead in Java you program in a mini-language interpreted in runtime by magic (aka annotating test stuff). Please check here for a truly Kotlin approach: github.com/mvysny/dynatest
@boots_on_the_grounds_of_war
@boots_on_the_grounds_of_war 6 месяцев назад
Lets play a super loud intro forcing us to turn it down ... and then back up again because the sound engineer can't do his job properly.
@PaulSebastianM
@PaulSebastianM 5 лет назад
Too few words...
@this-is-bioman
@this-is-bioman 3 года назад
Unfortunatelly I can't give you a thumbs up as I still don't know how to setup a project so that i can run tests :-\
@dasstraat
@dasstraat 3 года назад
Jetbranins is making the world very complicated. This is a lot of bool. The title is misleading, just to attract designers to this boring lecture, not about testing.
@JetBrainsTV
@JetBrainsTV 3 года назад
To be clear, this was a community talk from KotlinConf 2018.
Далее
Untangling Coroutine Testing by Márton Braun
42:49
Просмотров 8 тыс.
KotlinConf 2019: The Power of Types by Danny Preussler
34:49
KotlinConf 2018 - Android Suspenders by Chris Banes
38:29
Understand Kotlin Coroutines on Android (Google I/O'19)
37:49
10 Kotlin Tricks in 10 ish minutes by Jake Wharton
17:24
iPhone 16 Pro Max Trailer | DrTech
1:08
Просмотров 699 тыс.
Lithium cell can get burn
1:01
Просмотров 8 млн
iPhone 16 Pro Max Trailer | DrTech
1:08
Просмотров 699 тыс.
Тараканы убили ПК за 200к
0:45
Просмотров 156 тыс.
D3 XIAOMI SU7 MAX
14:25
Просмотров 476 тыс.