2

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.

  1. 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.

  2. 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.

  3. Integration tests for all(non trivial?) REST endpoints?

  4. 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?

4

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.

2

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 Oct 20 '17 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.