2017-10-20 2 views
2

내가하는 방법이 지정된 파라미터로 호출되었는지 확인하고자하는 시험이 진정한 항상 확인 :봄 부팅 및 Mockito

@Autowired 
    private Client client; 

    @Autowired 
    private OtherClient otherClient; 

    @Test 
    public void test() { 
     client.push(); 

     Mockito.verify(otherClient).publishReset(
      Mockito.anyString(), 
      Mockito.argThat(l -> l.size() == 3) 
     ); 
    } 

문제가 Mockito.verify 전혀 실패하지 않는다는 것입니다을, 내가 바꿀 수 있습니다 l -> l.size() == 3과 다른 크기의 일치 및 주어진 테스트는 항상 통과합니다. 확인을 위해 내가 arg에 건네는 모든 것을 통과시키는 것은 어떻게 가능합니까?

아래

전체 예 :

@EnableConfigurationProperties 
@TestExecutionListeners(listeners = { 
    DirtiesContextTestExecutionListener.class, 
    DirtiesContextBeforeModesTestExecutionListener.class, 
    ServletTestExecutionListener.class, 
    DependencyInjectionTestExecutionListener.class, 
    MockitoTestExecutionListener.class, 
    TransactionalTestExecutionListener.class, 
    WithSecurityContextTestExecutionListener.class 
}) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) 
@EnableAspectJAutoProxy(proxyTargetClass = true) 
@ContextConfiguration(
    loader = SpringBootContextLoader.class, 
    classes = {MyApp.class, IntegrationTestContext.class}) 
@RunWith(SpringRunner.class) 
public class FooIT { 
    @Autowired 
    private Client client; 

    @Autowired 
    private OtherClient otherClient; 

    @Test 
    public void test() { 
     client.push(); 

     Mockito.verify(otherClient).publishReset(
      Mockito.anyString(), 
      Mockito.argThat(l -> l.size() == 3) 
     ); 
    } 
} 

그리고 구성 클래스 :

@Configuration 
@MockBeans({ 
    @MockBean(OtherClient.class), 
}) 
public class IntegrationTestContext { 
} 

은 내가 잘못 뭔가가 있나요? Spring이 mockito와 어떻게 든 방해합니까?

답변

0

가비지 컬렉터에서 문제가 발생했습니다. 예를 들어 크기를 줄 였을 때 많은 컨테이너 (많은 필드가있는 30,000 개 이상) 인 큰 목록 l을 만들었습니다. 100 모든 것이 올바르게 작동하기 시작했습니다.

기본적으로 : 너무 많은 메모리를 사용하는 객체 (예 : 일종의 약한 참조가 사용됨)를 사용하여 mockito를 강제 실행하지 마십시오. 목록의 크기를 줄이십시오.

1

Mockito는 스프링 프록시와 최종 클래스/메소드를 감시하는 데 문제가 있습니다. 경우에 따라이 도움이 될 수 있습니다 :이 비슷한 문제로 도움이되었습니다 및 Github에서에이 Mockito-issue에서 영감을받은

Mockito.mock(SomeMockableType.class,AdditionalAnswers.delegatesTo(someInstanceThatIsNotMockableOrSpyable)); 

    // 
    // In your case: 
    @Test 
    public void test() {   
     OtherClient yourSpy = Mockito.mock(OtherClient.class,  
     AdditionalAnswers.delegatesTo(otherClient)); 

     client.push(); 

     Mockito.verify(yourSpy).publishReset(
     Mockito.anyString(), 
     Mockito.argThat(l -> l.size() == 3) 
     ); 
    } 

.