Elasticsearch Spring Integration Test

I am looking for a way to add inline elasticsearch to my w260 integration test.

I watched the integration test with elastic search, but it does not work along with spring loading, since both must use a different test runner.

I have a class test, since below, unfortunately, it does not work with an error:

java.lang.IllegalStateException: there is no contextual information for the stream: Topic [id = 1, name = main, state = RUNNABLE, group = main]. Does this thread work under the 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 inside @BeforeClass and @AfterClass (for example, static test class initiators are not allowed access to 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 anyone have any ideas how to make them work or what alternatives?

+6
source share
1 answer

In fact, you can do what you need without any additional elasticsearch testing dependencies. The idea is to create an inline node and then use NodeClient to communicate with it.

To do this, I created my own class EmbeddedElasticsearchServer , which looks (more or less) as follows:

 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 the spring configuration (let's call it integration-test-context.xml ) I did the following:

 <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 simply audit the client in your test as follows:

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/integration-test-context.xml") public abstract class AbstractElasticsearchIntegrationTest { @Autowired private Client elasticsearchClient; // Your rests go here... } 
+8
source

Source: https://habr.com/ru/post/988599/


All Articles