2017-02-05 10 views
3

나는 봄 콩을 모의하기 위해 모치 토를 사용하고 있습니다.모의 스프링 구성 요소

인터페이스를 조롱하면 잘 작동합니다.

우리의 응용 프로그램에는 인터페이스를 구현하지 않는 @Component Bean이 거의 없습니다.

이러한 구성 요소를 조롱하려고하면 스프링 컨텍스트가 해당 구성 요소 안에 속성을 삽입하려고 시도합니다.

Mockito는 인터페이스를 구현하지 않는 조롱 스프링 구성 요소를 지원하지 않습니까? 나는 그것을 완벽하게 잘 작동 Mockito를 사용하여 EmailSender을 조롱하는 경우

첨부 예는 위의 예에서

public interface EmployeeInterface { 
    public Long saveEmployee(Employee employee); 
} 

@Component 
public class EmployeeImpl implements EmployeeInterface { 

    @Autowired 
    public EmailSender emailSender 

    public Long saveEmployee(Employee employee) { 
     ... 
    } 
} 

public interface EmailSender { 
    public boolean sendEmail(Email email); 
} 

@Component 
public class EmailSenderImpl implements EmailSender { 

    @Autowired 
    MailServerInfo MailServerInfo; 

    public boolean sendEmail(Email email) { 
     ... 
    } 
} 

public interface MailServerInfo { 
    public String getMailServerDetails(); 
} 

@Component 
public class MailServerInfoImpl { 

    public String getMailServerDetails() { 
     ... 
    } 
} 

@Profile("Security-test") 
@Configuration 
public class SecurityTestMockConfiguration { 

    @Bean 
    @Primary 
    public EmailSender emailSender() { 
     return Mockito.mock(EmailSender.class); 
    } 
} 

@ActiveProfiles("Security-test") 
@RunWith(PowerMockRunner.class) 
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:context-test.xml" }) 
public class MyTest { 

    @Autowired 
    EmployeeInterface employeeInterface; 

    @Test 
    public void testSaveEmployee() { 
     employeeInterface.saveEmployee(employee); 
    } 
} 

을 요청한다.

아래 시나리오에서 EmailSender는 인터페이스를 구현하지 않는 Spring 구성 요소입니다. 아래의 경우 자동 배선 중에 오류가 발생합니다.

public interface EmployeeInterface { 
    public Long saveEmployee(Employee employee); 
} 

@Component 
public class EmployeeImpl implements EmployeeInterface { 

    @Autowired 
    public EmailSender emailSender 

    public Long saveEmployee(Employee employee) { 
     ... 
    } 
} 

@Component 
public class EmailSender { 

    @Autowired 
    MailServerInfo MailServerInfo; 

    public boolean sendEmail(Email email) { 
     ... 
    } 
} 

public interface MailServerInfo { 
    public String getMailServerDetails(); 
} 

@Component 
public class MailServerInfoImpl { 

    public String getMailServerDetails() { 
     ... 
    } 
} 

@Profile("Security-test") 
@Configuration 
public class SecurityTestMockConfiguration { 

    @Bean 
    @Primary 
    public EmailSender emailSender() { 
     return Mockito.mock(EmailSender.class); 
    } 
} 

@ActiveProfiles("Security-test") 
@RunWith(PowerMockRunner.class) 
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:context-test.xml" }) 
public class MyTest { 

    @Autowired 
    EmployeeInterface employeeInterface; 

    @Test 
    public void testSaveEmployee() { 
     employeeInterface.saveEmployee(employee); 
    } 
} 

두 번째 시나리오에서는 EmailSender가 MailServerInfo 구현을 찾을 수 없기 때문에 자동 와이어 링이 실패합니다.

+1

테스트 클래스를 첨부하십시오. –

+0

테스트 클래스 첨부 – lives

+0

context-test.xml을 제공하십시오. –

답변

3

문제

당신은 꽤 프레임 워크와 주석을 혼합한다. Spring은 @Autowired을 사용합니다. 모키 토는 @Mock@InjectMocks을 사용합니다. 또한이 방법으로 작동하지 않는 여러 가지 방법 (예 : @Configuration@ContextConfiguration(locations = { ... }))으로 응용 프로그램 컨텍스트를 구성합니다. 모든 세부 사항은 Mixing XML, Groovy scripts, and annotated classes을 참조하십시오.

mock을 사용하여 단위 테스트를 작성하거나 mock이 아닌 Spring 컨텍스트를 사용하여 통합 테스트를 작성하려는 경우에 문제가 있습니까? 당신이 EmailSender 다음 Mockito의 @Mock 주석을 사용 조롱하려는 경우

단위

테스트합니다. 그런 다음 Mockito가 @InjectMocks을 통해 EmployeeImpl의 종속성을 삽입하도록 할 수 있습니다. 당신은 통합 테스트 사용을 작성하려는 경우

@Mock 
EmailSender emailSender; 

@InjectMocks 
EmployeeImpl employee; 

또한이 https://dzone.com/articles/use-mockito-mock-autowired

통합과 같은 튜토리얼을 살펴 가질 수는

테스트 중

@Configuration 
public class TestConfiguration { ... } 

@ContextConfiguration(classes = { TestConfiguration.class }) 
public class MyTest { ... } 

또는

@ContextConfiguration(locations = { "classpath:context-test.xml" }) 
public class MyTest { ... } 
+0

주셔서 감사합니다. 유용했습니다. 스프링 컨텍스트에 대한 통합 테스트를 작성하려고합니다. 응용 프로그램 컨텍스트 내에 여러 bean의 중첩 autowiring이 있습니다. ActiveProfile 및 @primary 주석을 사용하여 응용 프로그램 컨텍스트 내에서 이러한 bean을 성공적으로 조롱 할 수 있습니다. 내 예제에서는 junit 테스트 클래스에서 내부적으로 다른 bean에 종속 된 EmployeeInterface에만 액세스한다. 인터페이스를 구현하지 않는 Spring Component를 조롱하려고 할 때만 에러가 발생합니다. 그렇지 않으면 작동합니다. 희망을 분명히했습니다. – lives