0

NoteControllerをテストするときに私のNoteServiceレイヤーをモックできません。実行中は実際のメソッドnoteService.findById(Long id, String login))が呼び出されますが、私はNullPointerExceptionを実行します。私はSpring Boot 1.33を使用します。Spring MVCコントローラのmockitoテストでサービスレイヤーをモックできません

ここでIDに私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"; 
    }  
} 

マイNoteControllerTestクラス

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

    } 
} 

例外のスタックトレース

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は - 私のメインクラスです。 (NoteController.java:46)は私のコントローララインに対応していますNote note = noteService.findById(id, principal.getName());私はthis postを読んでいますが、解決策が見つかりませんでした。

ありがとうございました。

更新

私は理由を見つけました。それは私のwhen(noteService.findById(id, "user")).thenReturn(note);スタブです。コントローラNote note = noteService.findById(id, principal.getName());を傍受しません。しかし、まだ私はそれを克服する方法がわからない、​​を変更するとThe method any(Class<String>) is ambiguous for the type NoteControllerTestエラーが発生します。私はその呼び出しを適切に交差させる必要があります。

+1

':へ@RunWith(SpringJUnit4ClassRunner.class)

を'フィールドは常にテストする痛みです。コンストラクタを介して' noteService'を渡してみましたか? – ESala

+0

@ESala、ちょうど試しました - 同じ結果。 –

+0

これを試してみてください:(noteServiceMock.findById(eq(id)、any(String.class)))then return(note); –

答えて

0

私の記憶が正しければ、あなたは@Mock@InjectMocks注釈を使用している場合、あなたはMockitoJUnitRunnerがここにjavadocを参照して使用する必要があります。http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/runners/MockitoJUnitRunner.html

ので変更:@RunWith(MockitoJUnitRunner.class)プライベート@Autowired

+0

これを変更しましたが、エラーの理由は、私のスタブメソッドが間違った嘲笑のために実際のサービスコールを傍受しないことです。 –

+0

この場合、モックされたオブジェクトがコントローラに挿入されていません。あなたのテストケースとコントローラの購入設定のブレークポイントを検証して、 'NoteService'が実際に異なっていることを確認してください。 – ndrone

+0

lesser複雑なメソッド(noteServiceMock.findById(id)) 'のように、私は正しい方法で引数を模擬する必要があると思うし、Principalオブジェクトを使って私が原因かもしれないし、それを考慮する必要がある。 –