It would be cool if Diffblue could create DataJpaTests for Spring Data Repositories or other kind of database repositories (maybe Hibernate Template or smiliar)
Hi arnzel,
Thank you for your message. Diffblue Cover supports these kind of tests. Please give it a go on the PetClinic project
which has a few examples. Diffblue covers more than 80% of this project as line coverage.
The unit test class shown below was written by Diffblue Cover running in the CLI mode.
Please let us know if data JPA unit tests are satisfactorily written in your case and feel free to raise a support ticket otherwise,
Kind Regards,
Cristian
package org.springframework.samples.petclinic.owner;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(classes = { OwnerRepository.class })
@EnableAutoConfiguration
@EntityScan(basePackages = { “org.springframework.samples.petclinic.owner” })
@DataJpaTest
class OwnerRepositoryDiffblueTest {
@Autowired
private OwnerRepository ownerRepository;
/**
* Method under test: {@link OwnerRepository#findPetTypes()}
*/
@Test
void testFindPetTypes() {
// Arrange
Owner owner = new Owner();
owner.setAddress("42 Main St");
owner.setCity("Oxford");
owner.setFirstName("Jane");
owner.setLastName("Doe");
owner.setTelephone("4105551212");
Owner owner1 = new Owner();
owner1.setAddress("42 Main St");
owner1.setCity("Oxford");
owner1.setFirstName("Jane");
owner1.setLastName("Doe");
owner1.setTelephone("4105551212");
ownerRepository.save(owner);
ownerRepository.save(owner1);
// Act and Assert
assertEquals(6, ownerRepository.findPetTypes().size());
}
/**
* Method under test: {@link OwnerRepository#findByLastName(String, Pageable)}
*/
@Test
void testFindByLastName() {
// Arrange
Owner owner = new Owner();
owner.setAddress("42 Main St");
owner.setCity("Oxford");
owner.setFirstName("Jane");
owner.setLastName("Doe");
owner.setTelephone("4105551212");
Owner owner1 = new Owner();
owner1.setAddress("42 Main St");
owner1.setCity("Oxford");
owner1.setFirstName("Jane");
owner1.setLastName("Doe");
owner1.setTelephone("4105551212");
ownerRepository.save(owner);
ownerRepository.save(owner1);
// Act and Assert
assertTrue(ownerRepository.findByLastName("foo", Pageable.unpaged()).toList().isEmpty());
}
/**
* Method under test: {@link OwnerRepository#findById(Integer)}
*/
@Test
void testFindById() {
// Arrange
Owner owner = new Owner();
owner.setAddress("42 Main St");
owner.setCity("Oxford");
owner.setFirstName("Jane");
owner.setLastName("Doe");
owner.setTelephone("4105551212");
ownerRepository.save(owner);
Owner owner1 = new Owner();
owner1.setAddress("42 Main St");
owner1.setCity("Oxford");
owner1.setFirstName("Jane");
owner1.setLastName("Doe");
owner1.setTelephone("4105551212");
ownerRepository.save(owner1);
Owner owner2 = new Owner();
owner2.setAddress("42 Main St");
owner2.setCity("Oxford");
owner2.setFirstName("Jane");
owner2.setLastName("Doe");
owner2.setTelephone("4105551212");
ownerRepository.save(owner2);
// Act and Assert
assertSame(owner2, ownerRepository.findById(owner2.getId()));
}
/**
* Method under test: {@link OwnerRepository#save(Owner)}
*/
@Test
void testSave() {
// Arrange
Owner owner = new Owner();
owner.setAddress("42 Main St");
owner.setCity("Oxford");
owner.setFirstName("Jane");
owner.setLastName("Doe");
owner.setTelephone("4105551212");
Owner owner1 = new Owner();
owner1.setAddress("42 Main St");
owner1.setCity("Oxford");
owner1.setFirstName("Jane");
owner1.setLastName("Doe");
owner1.setTelephone("4105551212");
Owner owner2 = new Owner();
owner2.setAddress("42 Main St");
owner2.setCity("Oxford");
owner2.setFirstName("Jane");
owner2.setLastName("Doe");
owner2.setTelephone("4105551212");
ownerRepository.save(owner);
ownerRepository.save(owner1);
ownerRepository.save(owner2);
// Act
ownerRepository.save(owner2);
// Assert
List<PetType> findPetTypesResult = ownerRepository.findPetTypes();
assertEquals(6, findPetTypesResult.size());
PetType getResult = findPetTypesResult.get(4);
assertFalse(getResult.isNew());
PetType getResult1 = findPetTypesResult.get(2);
assertFalse(getResult1.isNew());
assertEquals("dog", getResult1.getName());
assertEquals(2, getResult1.getId().intValue());
assertEquals("lizard", getResult.getName());
assertEquals(3, getResult.getId().intValue());
PetType getResult2 = findPetTypesResult.get(3);
assertEquals("hamster", getResult2.getName());
assertEquals(6, getResult2.getId().intValue());
PetType getResult3 = findPetTypesResult.get(1);
assertEquals("cat", getResult3.getName());
assertEquals(1, getResult3.getId().intValue());
PetType getResult4 = findPetTypesResult.get(5);
assertEquals("snake", getResult4.getName());
assertEquals(4, getResult4.getId().intValue());
PetType getResult5 = findPetTypesResult.get(0);
assertEquals("bird", getResult5.getName());
assertEquals(5, getResult5.getId().intValue());
assertFalse(getResult4.isNew());
assertFalse(getResult5.isNew());
assertFalse(getResult2.isNew());
assertFalse(getResult3.isNew());
}
/**
* Method under test: {@link OwnerRepository#findAll(Pageable)}
*/
@Test
void testFindAll() {
// Arrange
Owner owner = new Owner();
owner.setAddress("42 Main St");
owner.setCity("Oxford");
owner.setFirstName("Jane");
owner.setLastName("Doe");
owner.setTelephone("4105551212");
Owner owner1 = new Owner();
owner1.setAddress("42 Main St");
owner1.setCity("Oxford");
owner1.setFirstName("Jane");
owner1.setLastName("Doe");
owner1.setTelephone("4105551212");
ownerRepository.save(owner);
ownerRepository.save(owner1);
// Act and Assert
assertEquals(12, ownerRepository.findAll(Pageable.unpaged()).toList().size());
}
}
Hi David thanks for your answer. The documentation does not really tell me which kind of tests are supported so i did not know.
For the follow interface unfornutately no tests are created
package myDomain;
import myDomain.MyEntity;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
Optional<MyEntity> findBySomething(
String s, String clientId, Long l);
}
Output in Intellij plugin is
Starting “Write tests for prefix unknown”
No methods found to test: Diffblue Cover was unable to find any methods that can be tested in your selection. Ensure that your selection includes some Java code that contains methods that are not private See Output codes | Diffblue Docs to resolve this issue.
Hi arnzel
Here we suspect a problem with the project setup and we kindly ask you to upload the support log file. Since you are a plugin user, this will be found in the /username/.difflbue/log/ folder. I guess in your case, no other test is written and there is an issue with the classpath, package naming or project structure. This can be fixed with the details available in the log file.
Otherwise I can confirm that for a similar interface such as this:
package com.example.bookshop.repository;
import com.example.bookshop.model.BookEntity;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends JpaRepository<BookEntity, Long> {
Page<BookEntity> findAllByEditionsInPrint(boolean inPrint, PageRequest pageRequest);
Page<BookEntity> findAllByEditionsInPrintAndBlurb(
boolean inPrint, String blurb, PageRequest pageRequest);
@Query(value = "SELECT book FROM BookEntity book WHERE book.blurb = 'Blurb'")
List<BookEntity> findAllWithBlurbExact();
}
we successfully write unit tests.
Please let us know if you need more help.
Kind Regards.
Cristian