【发布时间】:2016-12-06 21:46:30
【问题描述】:
我有一个连接到外部 Web 服务的 Spring Boot 应用程序。该项目是使用 gradle 构建的。我在我的程序中嘲笑外部调用。当我在 STS 中作为 junit 测试运行时,测试运行成功。但是,当我进行“gradle build”时,测试失败了。当我查看日志时,我认为它失败了,因为测试正在命中实际服务而不是返回模拟对象。为了让我的 gradle 构建选择 mockito 生成的模拟对象作为测试的一部分,我需要做些什么吗?
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProxyApplication.class)
@WebAppConfiguration
public class ApplicationWrapperTest {
@Mock
private SoapClient soapClient;
@InjectMocks
@Autowired
private ApplicationWrapper applicationWrapper;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testDatesInApplication() throws Exception{
//return mock object (webservice response) when soapClient is called
}
ApplicationWrapper 代码
@Component
public class ApplicationWrapper {
@Resource(name = "factory.soapClient")
private SoapClientFactory soapClientFactory;
@Autowired
private JsonUtil jsonUtil;
@Autowired
private DomainUtil domainUtil;
private static final String clientId = "soapClient";
public String execute(String request, String apiName){
Object req = domainUtil.createRequest(request, apiName);
Object jaxbResponse = this.soapClientFactory.getClient(clientId).marshalSendAndReceive(req);
Object response = domainUtil.createResponse(jaxbResponse, apiName);
return jsonUtil.toJsonString(response) ;
}
}
【问题讨论】:
-
您能添加您的
ProxyApplication课程代码吗?它可能会覆盖您的模拟,并且由于 Gradle / Eclipse 类路径的差异,它仅在 Gradle 案例中被覆盖。 -
嗨@adamr,ProxyApplication 类是一个SpringBoot Main 类,它存在许多组件扫描。有没有办法确保在构建过程中模拟始终优先?
标签: java spring gradle spring-boot mockito