2016-05-20 14 views
0

it's my controller methodorg.mockito.exceptions.base.MockitoExceptionは:@InjectMocksで注釈を付けフィールド 'コントローラは、' 私は、コントローラがnullの@InjectMocksにエラーが発生しました

@RequestMapping(value = "/user", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE, headers = "Accept=application/json") 
    public @ResponseBody ResponseMessage getUser(@RequestBody AvailableUser uuid) { 
       return manager.available(uuid); 
    } 

it's my testcontroller

RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath*:WEB-INF/spring/appServlet/servlet-context.xml" }) 

public class TestController { 
    public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), 
      MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); 

    public TestController() { 

    } 

    @Mock 
    private static Manager manager; 
    @InjectMocks 
    private Controller controller; 
    private RMessage msg; 
    private MockMvc mockMvc; 
    final ObjectMapper mapper = new ObjectMapper(); 
    @Before 
    public void setup() { 
     MockitoAnnotations.initMocks(this); 
     this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); 
    } 

    @Test 
    public void testgetUser() throws Exception 
    { 

     AvailableUser availableUser=new AvailableUser(); 
     List<String> lst =new ArrayList<String>(); 
     lst.add("test1"); 
     lst.add("test2"); 
     availableUser.setUuId(lst); 
     this.mockMvc.perform(post("/user").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(availableUser))) 
     .andExpect(status().isOk()); 
     when(manager.available(availableUser)).thenReturn(msg); 
    } 
    } 

nullです。

class SomeTest { 
     @InjectMocks private Foo foo = new Foo(); 

     @Before public void setUp() { 
     MockitoAnnotations.initMock(this); 
}} 

をしかし、私はそれを行うことがあります。 それは私がそのためのヒントを与えます。

@Before 
     public void setup() { 
      MockitoAnnotations.initMocks(this); 
      this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); 
     } 

私は、コンソールにエラーが発生しました:

18:16:20,004 DEBUG CacheAwareContextLoaderDelegate:93 - Storing ApplicationContext in cache under key [[[email protected] testClass = TestSurfboardController, locations = '{classpath*:WEB-INF/spring/appServlet/servlet-context.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]. 
    18:16:20,106 DEBUG DirtiesContextTestExecutionListener:94 - After test method: context [[email protected] testClass = TestSurfboardController, testInstance = [email protected], testMethod = [email protected], testException = org.mockito.exceptions.base.MockitoException: Field 'surfboardController' annotated with @InjectMocks is null. 

i am properly apply the @InjectMock on the controller after that i have done MockitoAnnotations.initMocks(this);

答えて

0

をあなたが最初にあなたのコントローラを初期化する必要があります。

@InjectMocks 
private Controller controller = new Controller(); 

@InjectMocksMockMvcBuilders.standaloneSetupどちらがあなたのためにそれを行うことはありません。 MockMvcBuilders.standaloneSetupは、Null値を渡す場合はNPEを投げます。

+0

ok ...私がそれをすると、 "this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();"の行にエラー "java.lang.NoClassDefFoundError:javax/servlet/SessionCookieConfig"が表示されます。 – Shailu

+0

@ShailjaKantあなたのサービスを嘲笑しているのなら、なぜ@ ContextConfiguration'と '@ RunWith'が必要なのですか?私はあなたのテストクラスでこれらのアノテーションを削除することができると思います。 –

関連する問題