Creating unit tests in Spring 3

I'm starting to test applications in general, and I want to create some tests to learn Mockito in Spring. I read some information, but I have some general doubts that I would like to ask.

  • I saw the Mockito tests, and they comment on the class test using @RunWith (MockitoJUnitRunner.class) , and the Spring documentation uses @RunWith (SpringJUnit4ClassRunner.class) . I don't know what the difference is between them and which one I should use for a Spring application where tests use Mockito.
  • Since I have not seen a single real application that has a test, I would like to know a typical test that the developer will do. For example, in a typical CRUD application for users (users can be created, updated ...) can anyone do a normal test that this will be done.

Thanks.

+7
source share
1 answer
@RunWith(MockitoJUnitRunner.class) 

In this declaration you should write unit test . Unit tests carry out one class, making fun of all the dependencies. Typically, in a test case, you introduce abuse of dependencies:

 @Mock private YourDependency yourDependencyMock; 

 @RunWith(SpringJUnit4ClassRunner.class) 

Spring runner is for integration test (component test?). In this type of test, you run a whole bunch of classes, in other words, you test one class with real dependencies (testing a controller with real services, DAO, in-memory database, etc.)

You should probably have two categories in your application. Although we recommend having more unit tests and only a few smoke integration tests, I often found myself more confident in writing almost only integration tests.

As for your second question, you should have:

  • unit tests for each class (controller, services, DAO) separately with a mockery of all other classes

  • integration tests for all one CRUD operation. For example, creating a user who uses a controller, service, DAO, and database in memory.

+14
source

All Articles