2016-10-12 6 views
5

में विपरीत के विपरीत क्या है?हैमक्रिस्ट

List<String> list = Arrays.asList("b", "a", "c"); 
    // should fail, because "d" is not in the list 

    expectedInList = new String[]{"a","b", "c", "d"}; 
    Assert.assertThat(list, Matchers.contains(expectedInList)); 


    // should fail, because a IS in the list 
    shouldNotBeInList = Arrays.asList("a","e", "f", "d"); 
    Assert.assertThat(list, _does_not_contains_any_of_(shouldNotBeInList))); 

_does_not_contains_any_of_ क्या होना चाहिए?

+0

तुम सिर्फ 'contains' विधि नकारना कर सकते हैं? यानी 'शामिल है (चाहिएनॉटबिनलिस्ट)' मैंने इसमें बहुत कुछ नहीं किया है, इसलिए मुझे यकीन नहीं है कि यह काम करेगा, लेकिन यह एक कोशिश के लायक है। –

+0

@ माइकल पिकेट: नहीं (सभी तत्व शामिल हैं) = सभी तत्व नहीं हैं – pihentagy

+0

क्या आप हैमक्रिस्ट का उपयोग कर रहे हैं? यदि ऐसा है, तो वाक्यविन्यास अधिक होना चाहिए! AnyOf (...)। मुझे लगता है, सिर्फ एपीआई को देखकर मैंने कभी भी हामिक्रस्ट का उपयोग नहीं किया है। –

उत्तर

4

आप निम्नलिखित तरीके से तीन में निर्मित matchers गठजोड़ कर सकते हैं:

import static org.hamcrest.Matchers.everyItem; 
import static org.hamcrest.Matchers.isIn; 
import static org.hamcrest.Matchers.not; 

@Test 
public void hamcrestTest() throws Exception { 
    List<String> list = Arrays.asList("b", "a", "c"); 
    List<String> shouldNotBeInList = Arrays.asList("a", "e", "f", "d"); 
    Assert.assertThat(list, everyItem(not(isIn(shouldNotBeInList)))); 
} 

इस परीक्षण निष्पादित आपको देगा:

अपेक्षित: हर आइटम की { "एक", "ई", "च", "डी"}
लेकिन एक नहीं है: एक आइटम था "एक"

+0

उत्तर के लिए धन्यवाद। अब मेरे पास matcher सख्तता के बारे में एक संबंधित सवाल है: http://stackoverflow.com/questions/40015862/strict-matching-in-hamcrest – pihentagy

0

JavaDoc से, मैं इसे करने के लिए एक बेवकूफ तरीका देख सकता हूं। शायद एक बेहतर तरीका है! इस परीक्षण है कि क्या सूची a शामिल नहीं करता है और b शामिल नहीं है, और ...

List<Matcher> individual_matchers = new ArrayList<Matcher>(); 
for(String s : shouldNotBeInList) { 
    individual_matchers.add(Matchers.not(Matchers.contains(s)); // might need to use Matchers.contains({s}) - not sure 
} 
Matcher none_we_do_not_want = Matchers.allOf(individual_matchers); 
Assert.assertThat(list, none_we_do_not_want); 

(परीक्षण नहीं किया, शायद गाड़ी:/आशा है कि यह मदद करता है) समाधान के लिए

0

सूची - shouldNotBeInList सूची ही बराबर होना चाहिए (परिवर्तित करने के लिए निर्धारित करने की आवश्यकता)

Set<String> strings = new HashSet<>(list); 
    strings.removeAll(shouldNotBeInList); 

    Set<String> asSet = new HashSet<>(list); 
    Assert.assertTrue(strings.equals(asSet)); 

लेकिन वहाँ डब्ल्यू एक बेहतर होना चाहिए:, एक निम्न का उपयोग कर सकते अरे, मुझे उम्मीद है। इस परीक्षण का मामला साथ

public <T> Matcher<Iterable<? super T>> doesNotContainAnyOf(T... elements) 
{ 
    Matcher<Iterable<? super T>> matcher = null; 
    for(T e : elements) 
    { 
     matcher = matcher == null ? 
      Matchers.not(Matchers.hasItem(e)) : 
      Matchers.allOf(matcher, Matchers.not(Matchers.hasItem(e))); 
    } 
    return matcher; 
} 

:

1

इस विधि का प्रयास करें

List<String> list = Arrays.asList("a", "b", "c"); 
// True 
MatcherAssert.assertThat(list, doesNotContainAnyOf("z","e", "f", "d")); 
// False 
MatcherAssert.assertThat(list, doesNotContainAnyOf("a","e", "f", "d")); 
0

मैं एक ही राशि के लिए किया था मुद्दा। मेरा समाधान इस मैच के लिए सिर्फ उलटा तर्क था।

के बाद आप कोड का स्निपेट देख सकते हैं:

this.mockMvc.perform(get("/posts?page=0&size=1") 
       .with(httpBasic(magelan.getUserName(), magelan.getPassword())) 
       .accept(MediaType.parseMediaType("text/html;charset=UTF-8"))) 
       .andExpect(status().isOk()) 
       .andExpect(content().contentType("text/html;charset=UTF-8")) 
       .andExpect(content().string(allOf(
         containsString("First post") 
       ))) 
       .andExpect(content().string(allOf(
         not(containsString("Second post")) 
       ))); 
संबंधित मुद्दे