Fehlende Abhängigkeiten hasSize() und hasProperty() bei der Implementierung der unit-testen von Spring-controller

Ich versuche zu implementieren ist ein unit test eine Methode in einer Spring MVC-controller wie folgt:

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

Aber ich bekomme Nachrichten über Abhängigkeiten in Bezug hasSize(long) und hasProperty(...).

Habe ich neuere Versionen von Mockito, HamCrest et al in der Anwendung-Klasse Weg.

So was bin ich?

Meinem derzeitigen imports sind:

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;
  • Was sind die vollständigen Fehlermeldungen, die Sie bekommen? Haben Sie importiert hasSize und hasProperty statische Importe?
  • Ich bin mit NetBeans und bekommen flyover-Nachrichten, die sagen, 'nicht finden können symbol-Methode hasProperty(String, Matcher<String>). Die Nachrichten werden nicht in einem stacktrace oder compiler-listing.
  • Das klingt wie Sie, möglicherweise nicht importiert haben, die Methode. Könnte man auch die Importe aus dem test-Klasse, die in Ihrer Frage?
  • Ich habe Sie auf die Frage.
InformationsquelleAutor Mr Morgan | 2014-07-05
Schreibe einen Kommentar