在SpringBoot中模拟JestClient

时间:2019-03-21 12:10:03

标签: mockito elasticsearch-5 spring-boot-test

Springboot应用程序不会自动装配所有实例,因此我手动连接了所有实例。现在JestClient模拟尚未设置,我只能看到活动对象。接下来,我实际上想测试“ aMethod是否具有jestclient.execute()然后返回jestResult”

@RunWith(SpringRunner.class)
@SpringBootTest(classes = IngestionApplicationTests.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:application-test.yml")
@ComponentScan(basePackages = { "com.package.accounts" })
public class IngestionApplicationTests {

    @Autowired
    private IngestionServiceImpl vaService;

    @Autowired
    private IngestionQueryUtil queryUtil;

    @Mock
    private JestClient client;

    private JestResult result;

...
    @Before
    public void setUp() {

    client = mock(JestClient.class);
    String resultString="<jsonString>";
    result.setJsonString(resultString);
    result.setJsonObject(new JsonParser().parse(resultString).getAsJsonObject());
    result.setSucceeded(true);

    try {
            when(client.execute(anyObject())).thenReturn(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

}
}

我看到Spring-Boot正在创建自己的新上下文,而不是在测试上下文中运行。

0 个答案:

没有答案