How to implement BDD on very complex business rules?

1k views Asked by At

I am learning the art of Unit Testing and BDD and in my company there is no one who follows this approach. I try a lot to learn it by myself but get stuck somewhere and give up after trying for a few days. After some time I get inspiration again from someone and try to learn to swim in these waters again.

I have recently developed a Windows Service that started out small but ended up a big ball of messy business rules. Here is a brief overview of what the service does.

  1. Log to database “Service starting…”

  2. Get Data From DB that needs to be posted to another web service

  3. If there is no data to post Log to database “No data to process…” and exit service

  4. If data contains duplicate values Log to database “Duplicate data found, this record will be skipped.”

  5. Update the status of the record for which duplicate data was found to something e.g. 302

  6. If the data is null, Log to database “Record contains null value and cannot be processed.”

  7. Update the status of the record appropriately e.g. 310

  8. If the database is unavailable or down due to some reason, Log in a file “Database is down…”

  9. If the service is down where we have to post the data log to database “Receiver service is down.”

  10. Log to the database “Exiting service…”

So my service basically retrieves some data from the Database, creates JSON request from it and post it to another service.

It also parses the response from that service and logs if the data was posted successfully or not. I have just entered some of the business rules that are currently implemented in the service to give you an idea of what lies under the hood. I am learning BDD and unit testing and would really love to know how an expert will write test cases that test these complex business rules?

From my understanding BDD does not need to focus internally on how the service is written, instead it will test scenarios that service should fulfill For example

When executing the windows service with duplicate data

  • It should log to database “Duplicate data found, this record will be skipped.”
  • It should update the status of the record to 302

I can write multiple scenarios that test some functionality of the service. Is this the right approach or should I right very large sets of scenarios that test each business rule in every test?

Secondly as the service is communicating with the DB as well as a web service, how can I test the HttpRequest and HttpResponse that is sent and received by the service?

Finally how do I actually test something as complex as the business rules I have written above, If I simple assert that the service called some specific method of some class is that enough? How do we know that by simply calling some method it will perform the right task?

1

There are 1 answers

6
Damon On BEST ANSWER

A few simple thoughts to help keep it in perspective:

  1. You say learning...
    Keep that in mind and don't get hung up on what is perfect, right, or proper. You're learning, feel free to make mistakes and know that you'll improve as you go. Keep at it, keep practicing, you'll get better and it will feel more natural the more you do and the more you think about it.
  2. BDD tests behaviors.
    You use this to say the system should behave in a specific way, and that implies that it must be the system. You may still stub in a couple dummy services, on occasion (like a fake credit-card processing service) but for the most part, you want this to prove the system works as needed. Think of them as more of integration tests.
  3. Your BDD tests should drive your Unit Tests.
    Write the BDD test to fail, and then let that dictate what unit tests should be written in order to get your system to behave as you expect. This essentially means that each BDD test of yours will introduce a set of Unit Tests as well.
  4. In summary, let BDD drive TDD and you'll have the right balance of tests. The starting point is with your first BDD test.

And in your scenario, if your system is supposed to alert the user that they are attempting to add a duplicate, that's a valid test.

The annoying thing with testing Http Requests and Responses are that you end up doing string comparisons, but that is doable. The BDD tests should just care that the system responded as you expect.

The unit tests should isolate in respect to what you are doing, so you would have unit tests inside your web service to make sure it responds correctly, but you would not have a unit test on the outside that calls the web service; extract it out instead.

It all can become pretty philosophical, and that probably gets into what makes a good unit test versus what makes a good behavioral test, but hopefully this helps you get started down the road.