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

1 Answers1

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.