java.lang.AssertionError:预期:<1>,但在Junit中是:<30>

时间:2019-08-30 11:14:28

标签: junit mockito

我是Junit和Mockito的新手,我正在尝试测试以下代码,但是我正在获取异常---> java.lang.AssertionError:预期:<1>,但是:<30>     在org.junit.Assert.fail(Assert.java:88)     在org.junit.Assert.failNotEquals(Assert.java:834)     在org.junit.Assert.assertEquals(Assert.java:645)     在org.junit.Assert.assertEquals(Assert.java:631)     在,有人可以帮我什么是错吗?并建议我正确的方式

原始

public List<GithubModel> getGitHubUsersList(){
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<GithubModel>httpEntity = new HttpEntity<GithubModel>(httpHeaders);
        ResponseEntity<List<GithubModel>>usersList = restTemplate.exchange("https://api.github.com/repos/git/git/contributors",
                HttpMethod.GET,httpEntity, new ParameterizedTypeReference<List<GithubModel>>() {
                });
        return usersList.getBody();
    }

TestCase:

    @Spy
    @InjectMocks
    MicroServicesTest1 microServicesTest1;

    @Mock
    RestTemplate mockRestTemplate;

    @Before
    public void create(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getGitHubUsersListTest(){

        ResponseEntity<List<GithubModel>> response = Mockito.mock(ResponseEntity.class);
    Mockito.when(mockRestTemplate.exchange(Mockito.anyString(), Mockito.any(HttpMethod.class),
            Mockito.any(HttpEntity.class), Mockito.eq(new ParameterizedTypeReference<List<GithubModel>>(){}))).thenReturn(response);
    Mockito.when(response.getBody()).thenReturn(getUsersList());
    List<GithubModel> mocklist = microServicesTest1.getGitHubUsersList();
    assertEquals(1, mocklist.size());
    }

    /**
     * getUsersList
     * @return
     */
    public List<GithubModel>getUsersList(){
        GithubModel githubModel = new GithubModel();
        githubModel.setId(1);
        githubModel.setLogin("hello1");
        githubModel.setNode_id("hello2");
        List<GithubModel>list = new ArrayList<>();
        list.add(githubModel);
        return list;
    }

0 个答案:

没有答案