I am trying to do testing my save method in my service impl class. It has Page as return type. The test succeeds but I am writting something wrong because it succeeds for all the cases which normally shouldn't Please see my code below.
Service Class Implementation
@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {
private final CompanyRepository companyRepository;
public CompanyServiceImpl(CompanyRepository companyRepository) {
this.companyRepository = companyRepository;
}
@Override
public Page<Company> findAll(Pageable pageable) {
Page<Company> result = companyRepository.findAll(pageable);
return result;
}
@Override
public Page<Company> searchCompany(String companyName, Long companyGroupId, Pageable pageable) {
Page<Company> result = companyRepository.findByParametersWeb(companyName,companyGroupId,pageable);
return result;
}
@Override
public Optional<Company> findById(Long id) {
Optional<Company> entity = companyRepository.findById(id);
return entity;
}
@Override
public Company save(Company company) {
Company entity = companyRepository.save(company);
return entity;
}
@Override
public void delete(Long id) {
companyRepository.deleteById(id);
}
}
Testing Service class
class CompanyServiceImplTest {
@Mock
private CompanyRepository companyRepository;
private CompanyService companyService;
private Company company;
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
companyService = new CompanyServiceImpl(companyRepository);
company = new Company();
company.setName("company");
company.setCompanyGroupId(1L);
}
@Test
void searchCompany() {
List<Company> companies = new ArrayList<>();
Pageable pageable= PageRequest.of(0,5);
Page<Company> result = new PageImpl<>(companies,pageable,1);
when(companyRepository.findByParametersWeb(anyString(),anyLong(),any(Pageable.class))).thenReturn(result);
Page<Company> newResult = companyService.searchCompany("giorgos",1L,pageable);
assertEquals(newResult.getTotalElements(),result.getTotalElements());
}
}
Finally My Company Repository
@Repository
public interface CompanyRepository extends JpaRepository<Company, Long> {
@Query("SELECT a FROM Company a WHERE (:name is null or ((a.name LIKE :name AND LENGTH(:name) > 0) OR ( a.name = '%')))")
List<Company> findByCompanyName(@Param("name") String name);
@Query("SELECT a FROM Company a WHERE (:name is null or (LENGTH(:name) > 0 " +
" AND ((:option = 'yes' AND a.name = :name) or (:option = 'start' AND a.name LIKE CONCAT(:name,'%')) " +
" or (:option = 'end' AND a.name LIKE CONCAT('%',:name)) or (a.name LIKE CONCAT('%',:name,'%'))))) " +
" AND (:companyGroupId is null or a.companyGroupId = :companyGroupId) ORDER BY a.name")
Page<Company> findByParametersWeb(String name,Long companyGroupId, Pageable pageable);
List<Company> findAllByNameOrderByName();
}
So you want to differentiate unit tests from integration or component tests here. Your test would qualify as a unit test, it solely tests the functionality of your service layer isolated from everything else.
That is also why you mock your repository layer, to be independent from a database.
Contrary to that, integration and component tests test your whole application stack.
For this, the spring boot environment must be running, so you have to annotate your testclass with @ExtendWith(SpringExtension.class)
.
For these kind of tests you need an active db, so commonly you use a h2 database for your tests, that is filled with data prior to your tests. Have a look at this and this .
In the test itself you either inject your service and test from there, or you call your endpoints using RestTemplate
.
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Sql("/schema.sql")
public class DocumentManagementBackendApplicationTest {
@Autowired
private final CompanyServiceImpl companyServiceImpl;
@Test
@Sql("/searchCompany.sql")
public void testSearchCompany() {
List<Company> companies = new ArrayList<>();
Pageable pageable= PageRequest.of(0, 5);
Page<Company> result = companyService.searchCompany("giorgos",1L,pageable);
// now here you know, based on what you insert into your db with your sql scripts,
// what you should expect and so you can test for it
(...)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.