mardi 22 août 2017

How to get result from not mocked method?

I have a problem with getting result from method which is not mocked. I dont want to mock the result but to achieve the real result from this method. The method works in application, so this is not the problem. I have a test:

    @Test
public void shouldGetCompaniesToSelect() throws Exception {
    Company company = new Company("company", new Address());
    Company relatedCompany1 = new Company("relatedCompanyName1", new Address());
    Company notRelatedCompany = new Company("notRelatedCompanyName", new Address());
    Company relatedCompany2 = new Company("relatedCompanyName2", new Address());
    CompanyRelation companyRelation1 = new CompanyRelation(relatedCompany1);
    CompanyRelation companyRelation2 = new CompanyRelation(relatedCompany2);
    company.getCompanyRelations().add(companyRelation1);
    company.getCompanyRelations().add(companyRelation2);

    when(companyServiceMock.findAll()).thenReturn(Arrays.asList
            (company, relatedCompany1, notRelatedCompany, relatedCompany2));
    when(companyServiceMock.findOne(1L)).thenReturn(company);

    List<Company> companiesToSelect =  companyServiceMock.findCompaniesToSelect(company);

    mockMvc.perform(get("/company/1"))
            .andExpect(model().attribute("companiesToSelect", hasSize(1)))
            .andExpect(model().attribute("companiesToSelect", hasItem(
                    hasProperty("relatedCompany", hasProperty(
                            "name", is("notRelatedCompanyName")
                    )))));
}

There are 2 mocked methods (findAll and findOne) and then I want to execute method and get real results from findCompaniesToSelect(company - this is object created to test). Size of the companiesToSelect should be 1, but it returns 0.

findCompaniesToSelect method:

public List<Company> findCompaniesToSelect(Company company) {
    List<Company> companiesToSelect = companyRepository.findAll();
    for (CompanyRelation companyRelation :
            company.getCompanyRelations()) {
        companiesToSelect.remove(companyRelation.getRelatedCompany());
    }
    companiesToSelect.remove(company);
    return companiesToSelect;
}

How can I do that?

Aucun commentaire:

Enregistrer un commentaire