4

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?

asked Oct 14, 2017 at 15:22
2
  • Sounds reasonable to me. Commented 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-quickcheck Commented Oct 19, 2017 at 8:44

2 Answers 2

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.

answered Oct 17, 2017 at 16:58
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.

answered Oct 17, 2017 at 8:01
1
  • 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. Commented Oct 20, 2017 at 6:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.