-2

How can I unit test a codebase which contains classes that includes two or three methods. In general these methods are too long. The methods contain lots of tasks like filtering datasets and a couple of dml statements. Methods do not return anything.

I can define an input dataset, call the method and then check the database tables. But Cucumber does the same thing and as I know this is not unit testing.( feature test? Behavior driven Development? Integration test?)

So, in addition to Cucumber tests, how can I write unit test?

closed as too broad by Jörg W Mittag, gnat, BobDalgleish, Doc Brown, ChrisF Dec 1 '18 at 23:50

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

5

The simple answer is probably "you can't unit test this code". You say yourself that the functions are too long and that they're doing multiple tasks. Therefore you have two choices:

  1. Don't write unit tests. Contrary to what some people might tell you, it is possible to write high quality code without unit tests.
  2. Refactor so that you can write unit tests. I would personally recommend Working Effectively with Legacy Code by Michael Feathers, but pick whatever works for you. In this case, it sounds like your function needs to be split into (at least) two: one function which filters the dataset and another which writes it to the database; you may want a repository pattern or similar for abstracting away your database access.
  • I was just about to recommend that book in a comment. I'd also found this YT video to be a great, approachable demonstration of how to refactor legacy code: Practical Refactoring – Filip Milovanović Nov 17 '18 at 16:28
  • @filip your account says that you have upvote privileges. – Robert Harvey Nov 17 '18 at 16:31
  • @RobertHarvey: noted & upvoted :) Give credit where credit is due, if that's what you mean. I was just all caught up trying to find that video. – Filip Milovanović Nov 17 '18 at 16:34
  • "Contrary to what some people might tell you, it is possible to write high quality code without unit tests". Sure, this is possible, in theory. In practice, I've never seen this happen though. "In general these methods are too long" suggests this isn't the case here. And, even if one does beat the odds and write high quality code without tests, it's still always a case of "good luck modifying it"... – David Arno Nov 17 '18 at 22:32

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