2015-06-05 34 views
6

Szukam sposobu na dodanie wbudowanego elasticsearch do mojego testu integracji wiosennego rozruchu.Elasticsearch Test integracji rozruchowej Spring

Spojrzałem na test integracji elastycznego wyszukiwania, ale nie działa on razem z wiosennym startem, ponieważ oba powinny używać różnych testów.

mam test klasy jak poniżej niestety nie działa z powodu błędu:

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); 
    } 

} 

Czy ktoś ma jakieś pomysły, jak je uruchomić lub co jest alternatywy ??

+0

Jak abo ut this: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-data-elasticsearch/? – Val

+0

Widziałem to już. Najpierw nie używam danych źródłowych, po drugie potrzebuję wbudowanej wersji ES. –

Odpowiedz

7

Możesz zrobić to, czego potrzebujesz, bez żadnych dodatkowych zależności testowania elastycznego. Pomysł polega w zasadzie na utworzeniu węzła osadzonego, a następnie komunikowaniu się z nim za pomocą NodeClient.

Do tego tworzę własne EmbeddedElasticsearchServer klasę która wygląda (mniej więcej) tak:

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; 
    } 

} 

Następnie w konfiguracji sprężyny (nazwijmy go integration-test-context.xml) Zrobiłem to:

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

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

Następnie można po prostu autowire klienta w teście tak:

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

    @Autowired 
    private Client elasticsearchClient; 

    // Your rests go here... 

} 
+0

Dziękuję bardzo za odpowiedź. Spróbuję i zaakceptuję odpowiedź później. :) –

+0

Ta odpowiedź bardzo mi pomogła. Spędziłem cały dzień próbując stworzyć wersję org.elasticsearch.test.InternalTestCluster, którą mógłbym umieścić w testach na Jersey. To znacznie ułatwiło sprawę. Należy zwrócić uwagę na to, aby gdzieś zamknąć węzeł i klienta. Muszą być oczyszczone. – Jon

+0

W przypadku, gdy pomaga to ktokolwiek inny, działa to tylko dla mnie, gdy path.data była ścieżką absolutną (z ES 1.7.3). –

Powiązane problemy