Java – how to improve my JUnit test

My JUnit test looks like a long story:

>I create 4 users > I delete 1 user > I try to log in with the deleted user and make sure it fails > I log in with the remaining 3 users and verify that I can log in > I send a message from one user to another and confirm that it appears in the inbox of the sender and recipient. > I delete messages >... >

Advantages: tests are very effective (very good error detection) and very stable because they only use APIs. If I refactor the code, the tests are also refactored Because I don't use "dirty tricks", such as saving and reloading the database in a given state, my tests ignore schema changes and implementation changes

Disadvantages: tests become difficult to maintain, and any test change will affect other tests The test runs for 8-9 minutes, which is very good for continuous integration, but a little frustrating for developers Tests cannot run in isolation. The best you can do is stop running in the tests you are interested in, but you must absolutely run all previous tests

How can you improve my test?

Solution

Unit tests should ideally be independent and run in any order Therefore, I suggest you:

Decompose your test is independent. Consider using the memory database as the back end of the test. Consider wrapping each test or suite > unit test in the last rolled back transaction to see the passage of time and focus on it

If it takes 8 minutes to create several users and send several messages, the performance problem may not be in the test, but may be a symptom of the performance problem of the system itself - only your profiler can know for sure!

[please note: I don't think these tests are "integration tests", although I may be in a few; I think these types of tests are functional unit tests, a TDD]

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>