我需要为此客户编写一个测试案例。我如何模拟enlollextstub.enrollext((方法
@Service
public class CustomerEnrollmentSoapServiceImpl implements CustomerEnrollmentSoapService {
@Override
public EnrollExtStub.EnrollExtResponse enrollMember(LoyalHeaders loyalHeaders, EnrollExtStub.Data_type0 enrollMember) {
EnrollExtStub enrollExtStub = new EnrollExtStub();
EnrollExtStub.EnrollExtResponse enrollExtResponse = enrollExtStub.enrollExt(enrollExt, messageHeader);
return enrollExtResponse;
}
}
没有干净的方法可以做到这一点。假设您真的想测试它,它看起来像生成的代码,我可能不会测试,我测试了很多。但是,如果您这样做,则需要接缝。如果EnrollExtStub
无状态,或在其上调用enrollExt
不会更改内部数据,则可以使其成为自动bean。
@Service
public class CustomerEnrollmentSoapServiceImpl implements CustomerEnrollmentSoapService {
@Autowired
private EnrollExtStub enrollExtStub;
@Override
public EnrollExtStub.EnrollExtResponse enrollMember(LoyalHeaders loyalHeaders, EnrollExtStub.Data_type0 enrollMember) {
EnrollExtStub.EnrollExtResponse enrollExtResponse = enrollExtStub.enrollExt(enrollExt, messageHeader);
return enrollExtResponse;
}
}
然后使EnrollExtStub
bean
@Configuration
public class EnrollExtStubConfig {
@Bean
public EnrollExtStub enrollExtStub(){
return new EnrollExtStub();
}
}
然后在您的测试中
@RunWith(MockitoJUnitRunner.class)
public class CustomerEnrollmentSoapServiceImplTest {
@InjectMocks
private CustomerEnrollmentSoapServiceImpl service;
@Mock
private EnrollExtStub enrollExtStub;
...
另外,您可以直接将其直接调用一个类似于EnrollExtStubConfig
创建EnrollExtStub
的类,然后您可以模拟该类以返回模拟EnrollExtStub
。
@RunWith(PowerMockRunner.class)
@PrepareForTest({CustomerEnrollmentSoapServiceImpl.class})
public class CustomerEnrollmentSoapServiceImplTest {
@Test
public void enrollMemberTest() throws Exception {
EnrollExtStub enrollExtStubMock = PowerMockito.mock(EnrollExtStub.class);
PowerMockito.whenNew(EnrollExtStub.class).thenReturn(enrollExtStubMock);
PowerMockito.when(enrollExtStubMock.enrollExt(Matchers.anyClass(enrollExt.class), Matchers.anyClass(MessageHeader.class))
.thenReturn(enrollExtResponse);
}
}