java - Is there a procedure to creating JUnit test cases? -


i'm new java development , today first time paired program create test case. kind enough talk out-loud thinking explain why things being done (e.g. creating constructor arguments instead of having default constructor, etc).

while retain 40% of saw/heard today wondering if there set of steps/guidelines follow, end result test case starting point. know usual responses of

  1. every shop has own standard,
  2. there personal preferences , tools,
  3. depends if test class own versus legacy, etc.

either way, figured , ask members.

here snippet of notes took show had in mind, in terms of responses.

  1. using mockito, cannot mock objects being created methods, therefore try , constructor take objects. allow mock them , pass them method easier testing.
  2. mockito not handle static methods, can create wrapper class invokes static method. now, new wrapper class not use static methods , class testing use instead of static method. allows use when/thenreturn without having handle static method directly.
  3. when mocking, declare variables used test cases @ class level. mock classes within setup(); approach ensures commonly used mocks known state , not modified previous test.
  4. can't said enough, use variable names (e.g. listwithonecar, listwithnocars, etc)
  5. since test cases based off truth table, try include in test method names (e.g. ensurecandrive_car_0_withvalidkey_0(), ensurecandrive_car_1_withvalidkey_1(), etc). should visually scan if you've captured bulk of test cases quick visual scan. once done, add edge cases.
  6. 'verify' under used, try use more of in test cases.
  7. individual test cases should have code differs between test cases , no repeating code. makes easier understand , maintain/expand.

you idea. these notes, wondering if had list that's evolved?

you have different things in list. , 1 of topics 'complete' list not possible. make book, possible multiples. here list of books (and other documentation) make complete list.

  • some seem general coding practices. example recommendation use dependency injection via constructor (#1); importance of names (#4). hard make complete list those, there important books. recommend

    • clean code robert c. martin

    • the pragmatic programmer andrew hunt , david thomas

    • effective java joshua bloch

  • some limitations of mockito, go through documentation , blog posts it. might want @ powermock, tries fixes (most?) technical limitations of mockito , other mocking frameworks. reading did improve understanding of mockitos limitations.

  • some designing tests, making code testable , on. might consider following books:

    • xunit test patterns: refactoring test code gerard meszaros

    • test driven: tdd , acceptance tdd java developers lasse koskela

    • test driven development: example kent beck


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -