Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 21, 2025

Tests using AbstractDAOTest.loadObject() were failing with uncaught ObjectNotFoundException when database entities didn't exist, instead of gracefully skipping as intended.

Changes

  • Changed AbstractDAOTest.loadObject() to use hibernateTemplate.get() instead of hibernateTemplate.load()
  • Method now returns null immediately when entities don't exist, preventing lazy loading exceptions

Technical Details

Hibernate's hibernateTemplate.load() returns a lazy proxy object even when an entity doesn't exist in the database. The ObjectNotFoundException is only thrown later when properties are accessed on this proxy, not during the load() call itself. This caused exceptions to occur in test code rather than being handled in loadObject().

The solution uses hibernateTemplate.get() instead, which:

  • Immediately queries the database
  • Returns null if the entity doesn't exist (no proxy created)
  • Prevents ObjectNotFoundException from being thrown later in test code
public TBPersistable loadObject(Class pClass, Long pID) {
    TBPersistable obj = null;
    try {
        obj = (TBPersistable) hibernateTemplate.get(pClass, pID);  // Changed from load() to get()
    } catch (DataAccessException ex) {
        obj = null;
    }
    return obj;
}

All affected tests already check for null and skip with "empty database, test skipped" messages - they just needed the entity loading to return null instead of a proxy that throws exceptions.

Impact

Fixes 6 failing tests that expect entities with specific IDs (Matrix #265, #683, Study #794, #586). Applies to all 40 test classes extending AbstractDAOTest.

Original prompt

Numerous unit tests are failing in the same way. Implement a generalized way to address this. If the row doesn't exist, it must mean that we're testing against an empty database, in which case the test is skipped with an informational message. Otherwise proceed with the test as intended.

org.cipres.treebase.domain.matrix.MatrixTest ► testGetFormatInfo: treebase-core/src/test/java/org/cipres/treebase/domain/matrix/MatrixTest.java#L40
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.matrix.MatrixTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.matrix.Matrix#265]
---
org.cipres.treebase.domain.matrix.MatrixTest ► testGenerateNexusBlock: treebase-core/src/test/java/org/cipres/treebase/domain/matrix/MatrixTest.java#L71
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.matrix.MatrixTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.matrix.Matrix#683]
---
org.cipres.treebase.domain.nexus.NexmlAnalysisConverterTest ► testNexmlAnalysisConverter: treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlObjectConverter.java#L45
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.nexus.NexmlAnalysisConverterTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.study.Study#794]    
---
org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest ► testNexmlMatrixConverter: treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java#L54
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.study.Study#794]
---
org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest ► testNexmlEmptyMatrix: treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java#L166
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.study.Study#586]
---
org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest ► testNexmlMatrixCharSets: treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java#L269
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.study.Study#794]        

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Implement generalized test skipping for missing database rows Fix ObjectNotFoundException in test base class causing spurious test failures Dec 21, 2025
Copilot AI requested a review from rvosa December 21, 2025 19:59
@rvosa
Copy link
Contributor

rvosa commented Dec 21, 2025

@copilot we're still getting these failures:

org.cipres.treebase.domain.matrix.MatrixTest ► testGetFormatInfo: treebase-core/src/test/java/org/cipres/treebase/domain/matrix/MatrixTest.java#L40
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.matrix.MatrixTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.matrix.Matrix#265]

org.cipres.treebase.domain.matrix.MatrixTest ► testGenerateNexusBlock: treebase-core/src/test/java/org/cipres/treebase/domain/matrix/MatrixTest.java#L71
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.matrix.MatrixTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.matrix.Matrix#683]

org.cipres.treebase.domain.nexus.NexmlAnalysisConverterTest ► testNexmlAnalysisConverter: treebase-core/src/main/java/org/cipres/treebase/domain/nexus/nexml/NexmlObjectConverter.java#L45
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.nexus.NexmlAnalysisConverterTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.study.Study#794]

org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest ► testNexmlMatrixConverter: treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java#L54
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.study.Study#794]

org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest ► testNexmlEmptyMatrix: treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java#L166
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.study.Study#586]

org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest ► testNexmlMatrixCharSets: treebase-core/src/test/java/org/cipres/treebase/domain/nexus/NexmlMatrixConverterTest.java#L269
Failed test found in:
  treebase-core/target/surefire-reports/TEST-org.cipres.treebase.domain.nexus.NexmlMatrixConverterTest.xml
Error:
  org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.cipres.treebase.domain.study.Study#794]

…ialization issues

The previous fix using load() with ObjectNotFoundException catch didn't work because load() returns a proxy object that only throws ObjectNotFoundException when properties are accessed later. Using get() instead immediately queries the database and returns null if the entity doesn't exist, preventing the exception from occurring in test code.

Co-authored-by: rvosa <[email protected]>
Copy link
Contributor Author

Copilot AI commented Dec 21, 2025

@copilot we're still getting these failures:

...

Fixed in commit 1e1e8f1. The issue was that hibernateTemplate.load() returns a lazy proxy object that doesn't throw ObjectNotFoundException until you access its properties. Changed to use hibernateTemplate.get() instead, which immediately queries the database and returns null if the entity doesn't exist, preventing the exception from occurring later in test code.

Copilot AI changed the title Fix ObjectNotFoundException in test base class causing spurious test failures Fix lazy loading issue in test base class causing spurious test failures Dec 21, 2025
@rvosa rvosa marked this pull request as ready for review December 21, 2025 20:11
@rvosa rvosa merged commit 177614b into master Dec 21, 2025
0 of 2 checks passed
@rvosa rvosa deleted the copilot/implement-test-skipping-logic branch December 21, 2025 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants