Comments (4)
Hi,
That could an interesting thing to be able to call a method on each objects of the collection under test.
Do you have any concrete use case on your mind ?
I think the syntax is too heavy, it seems to me we are loosing the focus on assertions because of the plumbery needed to call method, we are repeating the class (TolkienCharacter.class
) as we can deduce it from the type of elements in the collection, I was thinking to the following syntax :
assertThat(fellowshipOfTheRing).calling("getName").contains("Gandalf", "Frodo", "Legolas");
I know it is String based and its is not refactoring friendly but is has the big advantage to be light, maybe we could offer both.
Have you any idea of a lighter syntax that would be refactoring friendly ?
Your comment on type safely is right but it is not a big usability problem according to me, it can even be a problem, let's say your are extracting Employee from the collection (Employee inheriting from Person) then contains will wait for Employee only. But what if your expected objects are declared as Person but actually are Employee, then you cant' use them in contains even though they are true Employee.
Anyway this is a minor issue to me, and I'm not quite sure if type safety is really worth it.
Thanks for the idea,
Joel
from assertj.
As far as a use case, I was considering it especially as an alternative to extracting
that didn't require Strings.
As far as alternatives that don't require the class to be provided, that is not possible because of type erasure. An actual class is required for the mechanism behind the scenes which would basically proxy the class so that you can capture the method you want to invoke repeatedly, by invoking it once on the proxy.
Using the same syntax as I suggested, you could extract out the part that declares the class. It doesn't eliminate the "problem", but rather moves it. This takes a little of the sting out of the actual assert statement, and it could be reused for multiple asserts that way:
TolkienCharacter callsTo = call(TolkienCharacter.class);
assertThat(fellowshipOfTheRing).evaluating(callsTo.getName()).contains("Gandalf", "Frodo", "Legolas");
assertThat(fellowshipOfTheRing).evaluating(callsTo.getRace()).contains(Race.hobbit, Race.elf);
I will have to consider further your comment about the supposed limitations of enforcing type safety, but my first impression is that there is that your example possibly suggests a code smell (in either the test or the code under test) which I haven't put my finger on yet. If I get some time I will return to elaborate your suggested test case to see whether my concern is founded.
from assertj.
It is true that you can't know at runtime the type of list elements but if you are using introspection to call method then you don't need it, you just expect that the method exists in each collection elements.
An example of generics drawback to makes my point clearer :
@Test
public void generics_limitation() {
Employee martin = new Employee("Martin Fowler");
Employee kent = new Employee("Kent Beck");
List<Employee> employees = newArrayList(martin, kent);
// now let's declare Martin as a Human (Employee inherits from Human
Human martinAsHuman = martin;
// this line compile
assertThat(employees).contains(kent, martin);
// this one does not compile because contains expects Employee not Human !
assertThat(employees).contains(martinAsHuman);
}
the last assertion does not compile because contains
, which is generic, expects Employee not Human even though we know by construction that martinAsHuman runtime type is Employee.
from assertj.
Extracting can be type safe thanks to #222.
from assertj.
Related Issues (20)
- Unresolvable stylesheet URLs in latest Javadoc HOT 1
- Recursive comparison depends on the sequence of objects. HOT 4
- assertThat(collection).doesNotHaveDuplicates() is not sorted, even if the collection is HOT 12
- Recursive comparison checks for existence of fields in types that parameterize nested unordered iterables HOT 1
- `satisfiesExactlyInAnyOrder` fails if `actual` overrides `equals` HOT 4
- `isNotEqualTo(boolean)` fails if `actual` is null HOT 2
- Suggesting "hasOnlyDuplicates()" assertion HOT 3
- Field existence check for the elements of a collection field HOT 1
- Checking compared fields existence: handle containers
- ...usingRecursiveComparison().ignoringTransientFields()... HOT 7
- Unable to compare some timestamp types HOT 2
- Add AbstractAssert#matchesWith HOT 1
- AssertFactory with the ability to convert the value upfront HOT 24
- Add default recursive comparison configuration for non accessible classes and fields HOT 6
- `extracting(Function...)` propagates `Object` instead of the common base type of all extractors HOT 6
- NPE with `extracting(Function... extractors)` HOT 3
- AssertJ: issue with Assertions.assertThat().usingRecursiveComparison().isEqualTo() HOT 6
- Provide reason and possible replacement for @Deprecated HOT 3
- AssertJ string assertions not working with Spock 2.3 and Groovy HOT 15
- isEqualTo on date can lead to unexpected failures when switching time zones HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from assertj.