2016-11-08 2002 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