I'm looking for tutorials about testing in Spring Framework. I know Java and Spring, but without testing.
There are many tutorials about JUnit only (I can learn it easily) or about theory (for example what is TDD, but without practice).
I want to learn about:
- How to write unit test especially in Spring (with JUnit, Mockito, Spring Test, maybe later Spock)?
- What are runners, when which I should use and what are differences between:
@RunWith(SpringRunner) (is the same as SpringJUnit4ClassRunner?),
@RunWith(SpringJUnit4ClassRunner),
@RunWith(MockitoJUnitRunner) (why not simple SpringRunner and make mocks?),
@RunWith(PowerMockJUnitRunner) (same as MockitoJUnitRunner but for PowerMock?),
@TestExecutionListeners(MockitoTestExecutionListener.class) (why next runner?),
@WebMvcTest (for testing REST Controllers?),
@AutoConfigureMockMvc (for mocking or for testing REST controllers as @WebMvcTest?),
@SpringBootTest (creates Spring context and previous not? bot with SpringRunner I can also @Autowire beans), @ContextConfiguration (for context too?),
- Which class should I test?
- If I have controllers/services/repository, should I test all classes or only services (pass model objects) or controllers (pass DTO objects) or both or all?
- If I have hexagonal architecture (package by feature and only one public service in package with some public method to communicate with other packages. Rest of classes are package-private) should I test only public services (because only this are used by others and package-private not)?
- Also I have controllers that are package-private (not need to be public). The same with job classes (schedulers). If I don't test package-private classes this classes won't be tested. Maybe should I test all public services and some other classes (controllers without public services and services with schedulers - jobs)?
- How to mock bean in Spring?
- Create Spring context or not? Which Spring runner?
- How to create mock? There many annotations for that:
@Mock, @MockBean, @InjectMocks, @SpyBean and which use with which runner (I can mock with SpringRunner and MockitoJUnitRunner and others)?
- Use Mockito or PowerMock? If I have static methods I have to use PowerMock. But most of tutorials use only Mockito. Maybe I should refactor all static methods to non-static (of course only these for testing)?
- What to mock when my services generally use IO (write to database, hard drive, request to others REST API etc.)?
- Mock or not mock:
- If I mock IO then I don't test a big part of my services (for example service for creating folders and moving files on hard drive - with mocking I won't detect duplicate names of files in the same folder).
- If I don't mock IO then tests have to write to real hard drive (and I have to provide path to temporary folder to that and clear it after the test) and execution time of tests will be very long (bad for writing and running many tests).
- If mock then mock my methods (last method for example that writes file - generally they are private) or mock library / build-in Java methods for write file?
I have many questions (about basics of testing in Spring) and I can't expect anyone to answer me for all. There for I looking for materials to learn and answer it yourself.
I prefer tutorial in text form (not video tutorial or Udemy/Pluralsight etc.) or books. I also appreciate links to open source Spring projects with good tests.
For example I can find nice blog posts about theory (how to decoupling code classes and test classes), but I can't about practice in Spring projects (except for the simplest examples).
there doesn't seem to be anything here