Reputation: 576
I'm starting to develop unit tests for my services with JUnit 5.
After running the test I'm getting these errors:
Results :
Tests in error:
WorkSiteRepositoryTest.TestDelete:198 ▒ EmptyResultDataAccess
No class com.xxx.WorkSiteEntity entity with id 5 exists!
WorkSiteRepositoryTest.TestFind:137 ▒ NoSuchElement No value present
WorkSiteRepositoryTest.TestUpdt:161 ▒ NoSuchElement No value present
Tests run: 4, Failures: 0, Errors: 3, Skipped: 0
Entity:
// Company name.
@Size (max = 3)
@Column (name="emp", nullable=false, length=4, unique=true)
private Integer companyId = null;
// Work site ID.
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="codlug", nullable=false, insertable=false, unique=true, updatable=false)
private Long id = null;
// Full name.
@Size (max = 40)
@Column (name="nombre", nullable=false, length=40)
private String fullName = null;
// Short name.
@Size (max = 15)
@Column (name="nomabre", nullable=false, length=15)
private String shortName = null;
// Country code.
@Size (max = 3)
@Column (name="pais", nullable=true, length=3)
private String countryCode = null;
// Province code.
@Size (max = 3)
@Column (name="pcia", nullable=true, length=3)
private String provinceCode = null;
// Province zip code.
@Size (max = 8)
@Column (name="codp", nullable=true, length=8)
private String zipCode = null;
// Work site address.
@Size (max = 40)
@Column (name="direc", nullable=false, length=40)
private String address = null;
// Building site number.
@Size (max = 9)
@Column (name="nroobra", nullable=true, length=9)
private Integer buildingNumber = null;
// Site's title
@Size (max = 4)
@Column (name="titlug", nullable=true, length=4)
private Integer siteTitle = null;
// Status
@Size (max = 1)
@Column (name="status", nullable=true, length=1)
private Integer status = null;
// Active
@Size (max = 4)
@Column (name="activ", nullable=true, length=4)
private Integer active = null;
// Is done
@Size (max = 1)
@Column (name="realiz", nullable=true, length=1)
private Integer done = null;
// Exoneration
@Size (max = 3)
@Column (name="exoneracion", nullable=true, length=3)
private Integer exoneration = null;
// Address number
@Size (max = 6)
@Column (name="domnro", nullable=true, length=2)
private Integer addressNumber = null;
// Office number
@Size (max = 6)
@Column (name="domofi", nullable=true, length=4)
private String officeNumber = null;
// Phone number (1)
@Size (max = 25)
@Column (name="tel1", nullable=true, length=25)
private String phoneNumber = null;
// Fax number
@Size (max = 25)
@Column (name="fax", nullable=true, length=25)
private String fax = null;
Unit test code used:
@Autowired
private IWorkSiteRepository workSiteRepository;
public WorkSiteRepositoryTest() {
super ();
}
@BeforeEach
public void setup() {
List<WorkSiteEntity> workSite = Arrays.asList (
new WorkSiteEntity (1,
"testname", "aa", "aa", "aa", "1100",
"testaddress", null, null, null, null, null, null, null,
"9", "2342", "326"),
new WorkSiteEntity (2,
"testname", "aa", "aa", "aa", "1200",
"testaddress", null, null, null, null, null, null, null,
"9", "623", "436"),
new WorkSiteEntity (3,
"testname", "aa", "aa", "aa", "1300",
"testaddress", null, null, null, null, null, null, null,
"9", "342", "567"),
new WorkSiteEntity (4,
"testname", "aa", "aa", "aa", "1400",
"testaddress", null, null, null, null, null, null, null,
"9", "543", "8567"),
new WorkSiteEntity (5,
"testname", "aa", "aa", "aa", "1900",
"testaddress", null, null, null, null, null, null, null,
"9", "7754", "885")
);
this.workSiteRepository.saveAll(workSite);
}
@AfterEach
public void release() {
this.workSiteRepository.deleteAll();
}
@Test
@DisplayName ("Find a WorkSite Entity By Id.")
public void TestFind () {
WorkSiteEntity workSite;
Long id = Long.valueOf(1500); // PK to search.
workSite = this.workSiteRepository.findById(id).get(); // This is line 137
assertNotNull(workSite, "[FINDBY] WorkSite " + id + " not found!");
}
@Test
@DisplayName ("Update a WorkSite Entity By Id.")
public void TestUpdt () {
WorkSiteEntity workSite;
Long id = Long.valueOf(1777); // PK to search.
workSite = this.workSiteRepository.findById(id).get();
assertNotNull (workSite, "[UPDATE] WorkSite " + id + " not found!");
String before = workSite.getFullName ();
workSite.setFullName (before.toUpperCase());
this.workSiteRepository.saveAndFlush(workSite);
workSite = this.workSiteRepository.findById(id).get(); // This is line 161
assertNotNull (workSite, "[UPDATE] WorkSite not " + id + " found!");
assertEquals (before.toUpperCase (), workSite.getFullName (), "[UPDATE] Update process failed!");
}
@Test
@DisplayName ("Delete a WorkSite Entity By Id.")
public void TestDelete () {
WorkSiteEntity workSite;
Long id = Long.valueOf(5); // PK to search.
this.workSiteRepository.deleteById(id); // This is line 198
workSite = this.workSiteRepository.findById(id).orElse(null);
assertNull (workSite, "[DELETE] WorkSite " + id + " found!");
}
@Test
@DisplayName ("Find all WorkSites")
public void TestFindAll () {
List<WorkSiteEntity> workSite;
workSite = this.workSiteRepository.findAll();
assertNotNull (workSite, "[FINDALL] WorkSite not found (NULL)!");
assertFalse (workSite.isEmpty (), "[FINDALL] WorkSite not found (Empty)!");
}
Why I'm getting No value present error? Also the error shows that there's no ID 5 but I added that ID to the object
I've commented the lines where the errors are shown
Upvotes: 0
Views: 184
Reputation: 5283
Issue: is with primary field definition. Although you are deleting all the records in @AfterEach
method, identity is going to keep increasing for each test, so first test will start from seq from 1..5, second test goes from 6..10 & goes on thats why it doesn`t find the entity by id.
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="codlug", nullable=false, insertable=false, unique=true, updatable=false)
private Long id = null;
Reference :
Result:
Note: Below methods are not gonna work as primary value doesnt going to match.
@Test
@DisplayName ("Find a WorkSite Entity By Id.")
public void TestFind () {
Long id = Long.valueOf(1500); // PK to search.
}
@Test
@DisplayName ("Update a WorkSite Entity By Id.")
public void TestUpdt () {
Long id = Long.valueOf(1777); // PK to search.
}
use Optional to check whether element is present or not.
Upvotes: 1