Junit 5 looking for actual environmental configuration to run the test

后端 未结 3 1161
夕颜
夕颜 2021-01-27 12:42

My project is using spring-boot, sping-data-jpa, spring-data-rest, hibernate

I have updated to spring boot 2.3.1

3条回答
  •  一整个雨季
    2021-01-27 13:36

    I have solved my problem, so sharing the answer.

    We don't need @SpringBootTest and @ActiveProfiles("unit-test") on the test class now.
    It is working as expected and not calling the actual environmental configuration to run the test. Tests are also super fast as previously.

    mport org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.Mockito;
    import org.mockito.junit.jupiter.MockitoExtension;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @ExtendWith(MockitoExtension.class)
    public class MyServiceTest {
    
        @Mock
        private MyRepository myRepository;
    
        @InjectMocks
        private MyServiceTest myServiceTest;
    
        @BeforeEach
        public void setup() {
    
           MyData myData = new MyData();
           myData.setName("testName");
           myData.setNumber()
        
           Mockito.when(myRepository.findMyDataByNameAndNumber(
                    Mockito.eq("testName"), Mockito.eq("10101010")).thenReturn(Optional.ofNullable(myData));
        }
        
        @Test
        void getMyData(){
            myServiceTest.getDataByNameAndNumber("testName", "10101010")
            
            Mockito.verify(myRepository, times(1))
                    .findMyDataByNameAndNumber(Mockito.eq("testName"), Mockito.eq("10101010"));
        }
    }
    

    Thank you @marc for your suggestion in the comment :)

提交回复
热议问题