2015-06-05 9 views
6

मैं अपने वसंत बूट एकीकरण परीक्षण में एम्बेडेड लोचदार खोज जोड़ने का तरीका ढूंढ रहा हूं।लोचदार खोज स्प्रिंग बूट एकीकरण परीक्षण

मैंने लोचदार खोज एकीकरण परीक्षण देखा लेकिन यह वसंत बूट के साथ मिलकर काम नहीं करता क्योंकि दोनों को अलग-अलग परीक्षण धावक का उपयोग करना चाहिए। उन्हें कैसे चलाने के लिए या क्या विकल्प है बनाने के लिए

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

} 

करता है किसी को भी कुछ विचार है ??:

मैं दुर्भाग्य से यह त्रुटि के साथ काम नहीं करता है के रूप में नीचे एक कक्षा की परीक्षा है

+0

गाने abo यह: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-data-elasticsearch/? – Val

+0

मैंने इसे पहले ही देखा है। सबसे पहले मैं वसंत-डेटा का उपयोग नहीं करता, दूसरी बात मुझे ईएस के एम्बेडेड संस्करण की आवश्यकता है। –

उत्तर

7

आप वास्तव में ऐसा कर सकते हैं जो आपको अतिरिक्त लोचदार खोज परीक्षण निर्भरताओं के बिना चाहिए। विचार मूल रूप से एक एम्बेडेड नोड बनाने के लिए है और फिर इसके साथ संवाद करने के लिए NodeClient का उपयोग करें। वसंत विन्यास में (इसे integration-test-context.xml कॉल) मैं ऐसा किया

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

} 

फिर,:

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

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

कि के लिए, मैं अपने खुद के EmbeddedElasticsearchServer वर्ग जो लग रहा है (कम या ज्यादा) इस तरह बनाया

तो फिर तुम सिर्फ ग्राहक अपने परीक्षण में इस तरह autowire कर सकते हैं:

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

    @Autowired 
    private Client elasticsearchClient; 

    // Your rests go here... 

} 
+0

आपके उत्तर के लिए बहुत बहुत धन्यवाद। मैं कोशिश करूँगा और बाद में जवाब स्वीकार करूंगा। :) –

+0

इस उत्तर ने मुझे बहुत मदद की। मैंने पूरे दिन एक org.elasticsearch.test.InternalTestCluster का संस्करण बनाने की कोशिश की थी जिसे मैं जर्सीटेस्ट में एम्बेड कर सकता था। इससे चीजें बहुत आसान हो गईं। ध्यान देने योग्य एक बात यह सुनिश्चित कर लें कि आप कहीं नोड और क्लाइंट बंद करें। उन्हें साफ करने की जरूरत है। – Jon

+0

यदि यह किसी और की सहायता करता है तो यह केवल मेरे लिए काम करता है जब path.data एक पूर्ण पथ था (ES 1.7.3 के साथ)। –

संबंधित मुद्दे