Proposal for adding Bean-Centric testing. #5008
Replies: 2 comments
-
Hi, I'm not part of the JUnit team, but if you look through the issues you'll see that JUnit team intends for the With that in mind, I don't think your proposal will be accepted. |
Beta Was this translation helpful? Give feedback.
-
AssertJ with value extraction is already very close to this proposal. For example, the two solution examples can be written as: import static org.assertj.core.api.Assertions.assertThat;
@Test
void testUser() {
User user = new User(...);
// Test multiple properties at once
assertThat(user)
.extracting("name", "age", "active", "address.street", "address.city", "address.state")
.containsExactly("Alice", 25, true, "123 Main St", "Springfield", "IL");
} import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
@Test
void testUser() {
List<User> users = List.of(new User(...), ...);
// Test multiple beans at once
assertThat(users)
.extracting("name", "age", "active", "address.street", "address.city", "address.state")
.containsExactly(
tuple("Alice", 25, true, "123 Main St", "Springfield", "IL"),
tuple("Bob", 30, false, "10 Evergreen Way", "Springfield", "IL"));
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Add Bean-Centric Testing to JUnit's Assertions API
Summary
Hi all, long-time fan of JUnit here!
Over the years I've been writing custom assertions for my open source projects.
I've finally gotten around to capturing what I feel are the most useful ones and formalizing the API.
I feel this could be an extremely useful addition to the existing Assertions API in JUnit.
I propose adding Bean-Centric Testing (BCT) capabilities to JUnit's existing Assertions API.
This would provide developers with powerful, concise methods for testing complex Java objects without the verbosity of traditional property-by-property assertions.
Problem Statement
Current JUnit assertions require verbose, repetitive code when testing complex objects:
This approach has several issues:
Proposed Solution
Add Bean-Centric Testing methods to JUnit's Assertions API:
It's even more powerful when having to assert multiple beans at once.
Key Features
1. Concise Assertions
2. Powerful Property Access
"address{street,city}"
"items{0{name},1{name}}"
or"items{#{name}}"
"config{host,port}"
"length"
and"size"
3. Flexible Comparison
4. Type Safety
Proposed API
Real-World Impact
BCT has been successfully deployed in the Apache Juneau project:
assertBean()
calls across 127 filesassertList()
calls across 69 filesassertBeans()
calls across 10 filesImplementation Considerations
1. Backward Compatibility
2. Performance
3. Extensibility
4. Error Messages
Benefits to JUnit Community
Check out the README.MD file on my GitHub repo jamesbognar/bean-centric-testing for more examples, complete javadocs, and documentation.
I'd be thrilled to discuss this proposal further and provide additional details or examples as needed.
Beta Was this translation helpful? Give feedback.
All reactions