Elasticsearch Spring boot integration test

2020-08-09 09:23发布

I am looking for the way to add embedded elasticsearch to my spring boot integration test.

I looked at elastic search integration test but it does not work together with spring boot as both should uses different test runner.

I have a class test as below unfortunately it does not work with error:

java.lang.IllegalStateException: No context information for thread: Thread[id=1, name=main, state=RUNNABLE, group=main]. Is this thread running under a class com.carrotsearch.randomizedtesting.RandomizedRunner runner context? Add @RunWith(class com.carrotsearch.randomizedtesting.RandomizedRunner.class) to your test class. Make sure your code accesses random contexts within @BeforeClass and @AfterClass boundary (for example, static test class initializers are not permitted to access random contexts).

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = App.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class TestExample extends ElasticsearchIntegrationTest {

    TestRestTemplate testRestTemplate = new TestRestTemplate();

    @Value("${local.server.port}")
    int port;

    @Test
    public void testOne(){
        ResponseEntity<String> results = testRestTemplate.getForEntity(String.format("http://localhost:%d/client/1", port), String.class);



     System.out.print(results);
    }

}

Does anybody has some ideas how to make them run or what is alternatives ??

1条回答
贼婆χ
2楼-- · 2020-08-09 10:22

You can actually do what you need without any additional elasticsearch testing dependencies. The idea is basically to create an embedded node and then use the NodeClient to communicate with it.

For that, I created my own EmbeddedElasticsearchServer class which looks (more or less) like this:

public class EmbeddedElasticsearchServer implements InitializingBean {

    public EmbeddedElasticsearchServer() {

        ImmutableSettings.Builder elasticsearchSettings = ImmutableSettings.settingsBuilder()
                .put("http.enabled", "false")
                .put("path.data", "target/elasticsearch-data");

        node = nodeBuilder()
                .local(true)
                .settings(elasticsearchSettings.build())
                .node();

        client = node.client();


    }

    @Override
    public void afterPropertiesSet() throws Exception {
         // Initialization stuff:
         // - create required indices
         // - define mappings
         // - populate with test data
    }

    public Client getClient() {
         return client;
    }

}

Then, in spring configuration (let's call it integration-test-context.xml) I did this:

<bean id="embeddedElasticsearchServer" 
      class="com.example.EmbeddedElasticsearchServer" />

<bean id="elasticsearchClient"
      class="org.elasticsearch.client.node.NodeClient" 
      factory-bean="embeddedElasticsearchServer" 
      factory-method="getClient" />

Then you can just autowire the client in your test like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/integration-test-context.xml")
public abstract class AbstractElasticsearchIntegrationTest {

    @Autowired
    private Client elasticsearchClient;

    // Your rests go here...

}
查看更多
登录 后发表回答