2016-11-08 1996 views
1

所以,我有如下─代碼@Mock對象返回null

@RunWith(MockitoJUnitRunner.class) 
public class TeamSubscriptionServiceTest { 

    @InjectMocks 
    private TeamSubscriptionService teamSubscriptionService; 

    @Mock 
    private ImsCustomerProfileService imsService; 

    @Mock 
    private IUserService userService; 

    @Mock 
    private HttpRequestService httpRequestService; 

    @Mock 
    private ISubscriptionDbService subscriptionDbService; 

    private String imsToken = "IMS_Token"; 

    @Before 
    public void setup() { 
     MockitoAnnotations.initMocks(this); 
     when(imsService.getAccessToken()).thenReturn(imsToken); 
     ReflectionTestUtils.setField(teamSubscriptionService, "jilEndpoint", "www.testJil.com"); 
     ReflectionTestUtils.setField(teamSubscriptionService, "adobeIOApiKey", "api_key"); 
    } 


    @Test(groups = { TestGroup.UNIT_TESTS }) 
    public void testAddSeat() throws IOException { 

     String teamId = "TestTeamID"; 
     String locale = "En_US"; 
     String jasonValue = "TestJasonData"; 
     String apiCallContent = "addSeatAPIResult"; 

     HttpResponse addSeatResponse = mock(HttpResponse.class); 
     when(addSeatResponse.getCode()).thenReturn(200); 
     when(addSeatResponse.getContent()).thenReturn(apiCallContent); 

     HttpServletResponse response = mock(HttpServletResponse.class); 
     when(httpRequestService.makeHttpRequest(anyString(),anyString(),anyMap(),anyString())).thenReturn(addSeatResponse); 

     String result = teamSubscriptionService.addSeat(teamId,locale,jasonValue,response); 
     assertNotNull(result); 
     assertEquals(result, "addSeatAPIResult"); 
    } 
} 

當我測試它,我得到一個NullPointerException上線

when(httpRequestService.makeHttpRequest(anyString(),anyString(),anyMap(),anyString())).thenReturn(addSeatResponse); 

我覺得跟@Mock註釋的所有對象都以某種方式爲空,並且該對象不會被注入到teamSubscriptionService對象中。

任何想法什麼是錯的代碼?

回答

1

問題是,您正在混合TestNG和JUnit註釋。

測試方法用@Test(groups = { TestGroup.UNIT_TESTS })進行註釋 - 顯然它是TestNG註釋@org.testng.annotations.Test,因爲JUnit的等價物沒有名爲groups的元素。

但是,您在setup()方法上使用JUnit的@Before註釋,因此此方法從不調用。 TestNG相當於此註釋是@org.testng.annotations.BeforeTest。改用它。

<...> 
import org.mockito.InjectMocks; 
import org.mockito.Mock; 
import org.mockito.MockitoAnnotations 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 
<...> 

public class TeamSubscriptionServiceTest { 
    @InjectMocks 
    private TeamSubscriptionService teamSubscriptionService; 
    @Mock 
    private ImsCustomerProfileService imsService; 
    @Mock 
    private IUserService userService; 
    @Mock 
    private HttpRequestService httpRequestService; 
    @Mock 
    private ISubscriptionDbService subscriptionDbService; 

    private String imsToken = "IMS_Token"; 

    @BeforeTest 
    public void setup() { 
     MockitoAnnotations.initMocks(this); 
     <...> 
    } 

    @Test(groups = { TestGroup.UNIT_TESTS }) 
    public void testAddSeat() throws IOException { 
     <...> 
    } 
} 

作爲邊注,@RunWith(MockitoJUnitRunner.class)是多餘的,以及,用TestNG時。

+0

非常感謝Myk ....它的工作:) – Tapan

+0

@Tapan我會很感激,如果你接受我的答案,因爲它適合你。 –

+0

完成...我是新來的...必須弄清楚如何接受答案;) – Tapan