What are the pros and cons of automated Unit Tests vs automated Integration tests?

前端 未结 12 1920
遥遥无期
遥遥无期 2020-12-12 20:35

Recently we have been adding automated tests to our existing java applications.

What we have

The majority of these tests are integration tes

12条回答
  •  旧巷少年郎
    2020-12-12 21:01

    Although the setup you described sounds good, unit testing also offers something important. Unit testing offers fine levels of granularity. With loose coupling and dependency injection, you can pretty much test every important case. You can be sure that the units are robust; you can scrutinise individual methods with scores of inputs or interesting things that don't necessarily occur during your integration tests.

    E.g. if you want to deterministically see how a class will handle some sort of failure that would require a tricky setup (e.g. network exception when retrieving something from a server) you can easily write your own test double network connection class, inject it and tell it to throw an exception whenever you feel like it. You can then make sure that the class under test gracefully handles the exception and carries on in a valid state.

提交回复
热议问题