Unit testing Spring Boot in Docker and Yandex cloud

Hello.





My name is Evgeny Frolikov, I'm a developer at AlfaStrakhovanie





In the course of work on a project at AlfaStrakhovanie, we are writing a project on micro services and it turned out that one of the "micro services" has grown greatly (but it is still far from the monolith :)). We lived so happily ever after, until we began to "move" to the cloud, and then the adventure began.





The move was not particularly memorable for the development team, only with questions from DevOps about ports, etc. Note that we cut out all the integration tests in order to get rid of dependence on other teams when something falls on their test benches. But "magic" began to happen in JUnit tests, namely tests began to fail. They fell phantom and unpredictably, for the time being it was treated with a retraem pipeline, until this problem became a blocker for calculating changes.





test 1 run
test 1 run

retraem





test 2 launch
2

" " .





( ) . ( , c Sonar).





@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ContractStatusServiceTest {
    @Autowired
    private ContractStatusService contractStatusService;
    @MockBean
    private RsaInfoComponent rsaInfoComponent;
    @MockBean
    private ContractRepository contractRepository;
      
      



""





  1. @RunWith(SpringJUnit4ClassRunner.class) - Spring





  2. @SpringBootTest - Spring Boot (, @SpringBootApplication) Spring. SpringBootTest





  3. @Autowired - Bean;





@Autowired , .





.





@RunWith(SpringRunner.class)
@SpringBootTest
@RequiredArgsConstructor
public class  ComponentTestTest {

   // @Autowired
    private final ComponentTest componentTest;
    

      
      



,





1)@RequiredArgsConstructor - Lombok final.





.....





java.lang.Exception: Test class should have exactly one public zero-argument constructor

	at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:171)
	at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:148)
	at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)
	...

      
      



.





, Mock





@RunWith(MockitoJUnitRunner.class)
public class CrossProductServiceTest {
    @InjectMocks
    private CrossProductService crossProductService;
    @Mock
    private KaskoService kaskoService;
    @Mock
    private CrownVirusOfferService crownVirusOfferService;
      
      







  1. @RunWith(MockitoJUnitRunner.class) - Bean , ( )





  2. @Mock -





  3. @InjectMocks - Bean





"".





:





  1. ( )









:
















All Articles