I am looking at writing test cases (something I haven't done/been taught before). I am creating a web app which has a Java based REST Api backend which uses Spring Boot.
Regarding various types of tests(unit tests, integration tests etc), I am confused about what to apply where.
The code of my application is divided into 3 types of classes following the MVC patters. 1. Model 2. Repositories 3. Controllers
From what I have read so far, this is what I feel.
Model classes and Repositories don't need tests, as they don't have any business code. All they do is help spring to convert data from one format to other and to persist the data.
In controllers, I should write unit tests and integration tests for methods that do non trivial task. Methods that do not have any conditional statements/loops should be skipped.
Integration tests for all(non trivial?) REST endpoints?
Once the web frontend is under progress, I should write end to end tests to verify the UI/UX is working as expected.
Am I on the right path?
-
Sounds reasonable to me.Robert Harvey– Robert Harvey2017年10月14日 15:52:46 +00:00Commented Oct 14, 2017 at 15:52
-
Slightly off topic in a java setting, but an interesting way of testing REST APIs thoroughly with JSON Schema + property tests : infoq.com/presentations/test-web-service-quickcheckguillaume31– guillaume312017年10月19日 08:44:15 +00:00Commented Oct 19, 2017 at 8:44
2 Answers 2
You're correct that Models and Repositories likely don't need unit tests, predicated upon your Models and Repositories being free of logic. If they have logic, it should be tested - but better than testing that logic would be moving it elsewhere in your design (possibly to ViewModels, for example).
You may want to consider moving logic out of the Controllers and into separate Services. The tests for your Controllers would then be much smaller (mostly just around ensuring that the response to the request is has the correct HTTP status code, that sort of thing), and your Services would then have the bulk of the testable logic.
Integration tests are a good idea, but you don't need a ton of them - ideally, you want more unit tests than integration tests, and more integration tests than end-to-end tests. You may also be able to write some frontend unit tests, if you have much logic on the frontend that can't be moved out to the backend.
Your approach is similar to what I've seen in similar projects.
Basically any non-trivial code should be unit tested. Additionally we used RestAssured for testing REST services (integration tests), and Selenium / WebDriver for end-to-end tests of the entire application.
-
The approach makes sense, but consider the tools- Selenium might be replaced by Chrome puppeteer (or not...), and in both cases consider using headless browser (browser without the GUI) if it works in your case- it is a bit faster and more stable. Personally I don't like REST test tools like RestAssured, they are great for ad-hocc or exploratory testing but much less convenient when automating things. I usually use Python with requests, or the equivalent language and library.Rsf– Rsf2017年10月20日 06:23:25 +00:00Commented Oct 20, 2017 at 6:23