본문 바로가기

Spring & Java

Spring boot Test with Mockito

반응형

Mocking Framworks

  1. mockito
  2. easymock
  3. jmockit

Mockito in spring boot

    mockito framwork is builtin spring boot
    dependency -> spring-boot-starter-test

Mockito Test Process

  1. setup
    set expectations with mock reponses
  2. execute
    call the method want to test
  3. assert
    check the result and verify that it is expected result
  4. verify
    optionally.. verify calls (how many times called etc..)

Mokcito Test in Spring boot process

  1. step 1 : create mock for the dao
     @Mock
     private MyDao myDao;
  2. step 2: Inject mock into service
     //only inject dependencioes annotated with @Mock or @Spy
     @InjectMocks
     private MyService myService;
  3. step3: set up expectations
     String aResponse = "Hello";
     when(doTimeWork()).thenReturn(aResponse);
  4. step4: call method under test and assert results
     assertEquals(excpectedValue, doTimeWorkResult());
반응형

'Spring & Java' 카테고리의 다른 글

Spring boot @MockBean  (0) 2022.05.22
Spring boot Mockito Example - @Mock & @InjectMocks  (0) 2022.05.22
Unit Test - ParameterizedTest  (0) 2022.05.21
Java Conditional Unit Test  (0) 2022.05.21
spring aop  (0) 2021.11.07