나는 봄 콩을 모의하기 위해 모치 토를 사용하고 있습니다.모의 스프링 구성 요소
인터페이스를 조롱하면 잘 작동합니다.
우리의 응용 프로그램에는 인터페이스를 구현하지 않는 @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 구현을 찾을 수 없기 때문에 자동 와이어 링이 실패합니다.
테스트 클래스를 첨부하십시오. –
테스트 클래스 첨부 – lives
context-test.xml을 제공하십시오. –