Creating unitary tests in Spring 3

帅比萌擦擦* 提交于 2019-12-03 00:26:35
@RunWith(MockitoJUnitRunner.class)

With this declaration you are suppose to write a unit test. Unit tests are exercising a single class mocking all dependencies. Typically you will inject mocked dependencies declared like this in your test case:

@Mock
private YourDependency yourDependencyMock;

@RunWith(SpringJUnit4ClassRunner.class)

Spring runner is meant for integration test (component test?) In this type of tests you are exercising a whole bunch of classes, in other words you are testing a single class with real dependencies (testing a controller with real services, DAOs, in-memory database, etc.)

You should probably have both categories in your application. Althought it is advices to have more unit tests and only few smoke integration tests, but I often found myself more confident writing almost only integration tests.

As for your second question, you should have:

  • unit tests for each class (controller, services, DAOs) separately with mocked all other classes

  • integration tests for a whole single CRUD operation. For instance creating a user that exercises controller, service, DAO and in-memory database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!