assertj: usingRecursiveComparison() returns false positive when comparing to a Spring Data JPA projection
- spring-boot-starter-parent version: 3.3.2 (with managed dependency versions)
- assertj core version: 3.25.3
- java version: 17
- test framework version: junit-jupiter-api 5.10.3
Test case reproducing the bug
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepo extends JpaRepository<PersonRepo.PersonEntity, String> {
@Entity
public static class PersonEntity {
@Id
private String name;
}
public interface Person {
String getName();
}
@Query(value = "SELECT 'alice' as name")
Person getPerson();
}
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
public class PersonRepoTest {
@Autowired
private PersonRepo personRepo;
private record PersonImpl(String getName) implements PersonRepo.Person {
}
@Test
public void test() {
PersonRepo.Person alice = personRepo.getPerson();
PersonRepo.Person bob = new PersonImpl("bob");
// Recursive comparison incorrectly asserts that alice and bob are the same
assertThat(alice).usingRecursiveComparison().isEqualTo(bob);
}
}
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 17 (12 by maintainers)
Commits related to this issue
- add spring boot tests corresponding to issues #3533 and #3551 — committed to assertj/assertj by joel-costigliola 2 years ago
- Move Spring Boot based tests to separate Maven module, simplify tests See #3533 and #3551. — committed to assertj/assertj by scordio 2 years ago
- Move Spring Boot based tests to separate Maven module, simplify tests See #3533 and #3551. — committed to assertj/assertj by scordio 2 years ago
- add spring boot tests corresponding to issues #3533 and #3551 — committed to nosan/assertj by joel-costigliola 2 years ago
- Move Spring Boot based tests to separate Maven module, simplify tests See #3533 and #3551. — committed to nosan/assertj by scordio 2 years ago
I still wonder how to improve the user experience for the original example…
We could throw some misconfiguration exception in case the object under test is a proxy (detected via
Proxy.isProxyClass()), no fields are found andexpectedis not a proxy. Kind of similar to how Mockito fails when stubbing is not done correctly.WDYT?
I couldn’t find any straightforward way to identify the “good” interface that the proxy implements so I made it work with a custom introspection strategy that identifies the properties to check based on a given type:
The example now fails with:
This is just to spark further discussion, I’m not saying you should try this at home 😄