Ich kann nicht meine NoteService-Ebene zum Testen meiner NoteController. Beim Ausführen wird die echte Methode noteService.findById(Long id, String login))
aufgerufen, statt gespottet, also stoße ich auf NullPointerException
. Ich benutze Spring Boot 1.33.Nicht möglich, Service-Layer im Frühjahr MVC Controller Mockito Test
Hier id mein NoteController
@Controller
@RequestMapping("/notes")
public class NoteController {
@Autowired
private NoteService noteService;
@ModelAttribute("noteForm")
public NoteForm noteForm() {
return new NoteForm();
}
@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String editNote(@PathVariable("id") Long id, NoteForm noteForm, Principal principal) {
Note note = noteService.findById(id, principal.getName());
noteForm.setNote(note);
noteForm.setAddress(note.getAddress());
return "edit";
}
}
Meine NoteControllerTest Klasse
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PhonebookApplication.class)
@WebAppConfiguration
public class NoteControllerTest {
@Mock
NoteService noteService;
@Mock
View mockView;
@InjectMocks
NoteController controller;
MockMvc mockMvc;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = standaloneSetup(controller).setSingleView(mockView).build();
}
@Test
public void editNote() throws Exception {
Long id = 1L;
Note note = new Note();
when(noteService.findById(id, "user")).thenReturn(note);
mockMvc.perform(get("/notes/edit/{id}", id)).andExpect(status().isOk()).andExpect(view().name("edit"));
}
}
Ausnahme-Stack-Trace
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
at com.lardi.controller.NoteController.editNote(NoteController.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)
at com.lardi.controller.NoteControllerTest.editNote(NoteControllerTest.java:62)
PhonebookApplication
- ist meine Hauptklasse. (NoteController.java:46)
entspricht meiner Controller-Leitung Note note = noteService.findById(id, principal.getName());
Ich habe this post gelesen, habe aber keine Lösung gefunden.
Danke.
aktualisieren
Ich habe den Grund gefunden. Es ist mein when(noteService.findById(id, "user")).thenReturn(note);
Stub. Es fängt nicht Controller Note note = noteService.findById(id, principal.getName());
Anruf. Aber ich weiß immer noch nicht, wie ich es zu überwinden habe, die Änderung when(noteServiceMock.findById(id, any(String.class))).thenReturn(note);
verursacht The method any(Class<String>) is ambiguous for the type NoteControllerTest
Fehler. Ich muss diesen Anruf richtig durchschneiden.
'@Autowired privat 'Felder sind immer ein Schmerz zu testen, haben Sie versucht, die' NoteService' über Konstruktor übergeben? – ESala
@ESala, gerade versucht - das gleiche Ergebnis. –
Versuchen Sie Folgendes: when (noteServiceMock.findById (eq (id), any (String.class))). ThenReturn (Notiz); –