2014-07-05 18 views
7

:Eksik bağımlılıkları hasSize() ve hasProperty() İlkbahar kumandanın birim testini uygularken ben şöyle bir bahar MVC denetleyicisi bir yöntemin bir birim testi uygulamak çalışıyorum

@Test 
public void testGetProfile() { 
    Person mockPerson = new Person(); 
    mockPerson.setPersonId(1); 
    mockPerson.setName("Mr Brown"); 
    mockPerson.setAddress("Somewhere"); 
    mockPerson.setTelephone("1234567890"); 
    mockPerson.setEmail("[email protected]"); 

    when(mockPersonService.get(1)).thenReturn(mockPerson); 
    try { 
     mockMvc.perform(get("/person/profile?personId=1")) 
      .andExpect(status().isOk()) 
      .andExpect(view().name("view/profile")) 
      .andExpect(forwardedUrl("/WEB-INF/jsp/view/profile.jsp")) 
      .andExpect(model().attribute("person", hasSize(1L))) 
      .andExpect(model().attribute("person", hasItem(
        allOf(
          hasProperty("personId", is(1L)), 
          hasProperty("name", is("Mr Brown")), 
          hasProperty("address", is("Somewhere")), 
          hasProperty("telephone", is("1234567890")), 
          hasProperty("email", is("[email protected]")), 
        ) 
      ))); 

    } 
    catch(Exception e) { 
     Misc.printStackTrace(e); 
    } 
    verify(mockPersonService, times(1)).get(1); 
    verifyNoMoreInteractions(mockPersonService);   
} 

Ama ilgili mesajlar almak hasSize(long) ve hasProperty(...) ile ilgili bağımlılıklar.

Mockito, HamCrest ve diğerlerinin, uygulamanın sınıf yolundaki güncel sürümlerini kullanıyorum.

Peki neden eksiklerim?

Benim geçerli ithalat vardır:

import static org.hamcrest.CoreMatchers.*; 

hasProperty ve hasSize yöntem Matchers sınıfında sadece:

import library.model.Person; 
import library.service.PersonService; 
import library.util.Misc; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.web.WebAppConfiguration; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 
import org.springframework.web.context.WebApplicationContext; 
import static org.hamcrest.CoreMatchers.*; 
import static org.junit.matchers.JUnitMatchers.hasItem; 
import static org.mockito.Mockito.*; 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; 
+0

Tam hata iletileri nedir? Statik içe aktarma kullanarak 'hasSize' ve' hasProperty' ithal ettiniz mi? – andersschuller

+0

NetBeans kullanıyorum ve 'sembol yöntemi hasProperty (String, Matcher )' bulamıyor diyerek flyover mesajları alıyorum. Mesajlar stacktrace veya derleyici listesinde yok. –

+0

Yöntemi içe aktarmış olmadığınıza benziyor. Sorunuzu test sınıfınızdan içe aktarır mısınız? – andersschuller

cevap

12

İşletme ithalat sadece CoreMatchers olan hamcrest-çekirdekten matchers arasında, daha büyük bir set takımı içeren hamcrest-kütüphanesinin. aşağıdakilere İçe aktarma değiştirmeye çalışın:

import static org.hamcrest.Matchers.*; 

bu işe yaramazsa, sadece hamcrest-çekirdekli bağlı olabilir. Bu durumda, bağımlılığınızı hamcrest kütüphanesine veya hamcrest-artifact'e değiştirin. Maven'e bu bağımlılığı eklemek için bir örnek aşağıda. Daha fazla ayrıntı için Hamcrest GitHub page'a bakın.

<dependency> 
    <groupId>org.hamcrest</groupId> 
    <artifactId>hamcrest-library</artifactId> 
    <version>1.3</version> 
</dependency> 
+0

Teşekkürler efendim. Bu sorunu çözdü. –

+1

Yay önyükleme-starter-test bağımlılığı ile yay önyükleme, bunları varsayılan olarak https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html – SiteTester