Comments (2)
Ooo a chainable method that qualifies the assertion like inOrder()
is quite clever.
Can't be that hard to put a synthetic void
-returning overload in the jar to maintain binary compatibility, right? 😀
For now I'm just leaving a comment with getClass()
, but that also doesn't feel great.
from truth.
Thanks. I am always a little nervous to add new APIs, especially to Subject
, so I'm waffling as usual. I could imagine that someone at Google would eventually do the analysis to give us a better idea of how many assertThat(e.getClass()).isEqualTo(SomeException.class)
callers really want that. That could certainly help.
assertThat(x.getClass())
looks about as popular as Subject.isAnyOf
—a useful point of comparison, since it, too, has prime real estate on Subject
itself. The negative form of the assertion looks maybe 2 orders of magnitude less popular, so there would be a question of whether to include it. (I also see an even smaller number of assertThat(x.getClass()).isAnyOf(...)
/ isIn
calls. Still, we probably(?) wouldn't go so far as assertThat(e).exactClass().isEqualTo(...)
.)
Its functionality is probably not quite as compelling as isAnyOf
, since users can use the straightforward alternative that you've suggested. (Not that reimplementing isAnyOf
is rocket science, either :)) But I always like letting users give us the actual value for us to put into the failure message: Instead of just "expected: SomeException but was: SubSomeException," it's nice for us to be able to chain the actual exception into the failure. isExactlyInstanceOf
would enable that. [edit: And I notice that the common workaround for lack of context, which is to call assertWithMessage
, isn't as helpful here: You'd really like to have the whole exception attached as a cause, not to get only its toString()
(or to have to convert its stack trace to a giant string).]
The big question, as you point out, is how many assertThat(e.getClass()).isEqualTo(SomeException.class)
users just aren't familiar with the Truth API vs. how many want to test the specific class. We'd long ago considered writing static analysis to nudge users toward isInstanceOf
(and learn how often that's not what they want), but we knew of specific patterns where users wanted what you want, like assertThat(copy.getClass()).isEqualTo(original.getClass())
. At the same time, there are also many people who write things like assertThat(foo.getClass()).isEqualTo(String.class)
, who clearly would be better off with isInstanceOf
.
(One thing for us to remember with a new method is to cover it with static analysis: At least assertThat(e.getClass()).isEqualTo(SomeException.class)
is already checked by Error Prone to make sure that e
really can be a SomeException
. And this investigation just now has prompted me to file a feature request for isInstanceOf
/isNotInstanceOf
that I've wanted for years....)
"isExactlyInstanceOf
" does sound like the sort of name to go with. I still somehow sort of wish that it were even clearer about "and not a subclass," which is obviously what "exactly" is there to communicate, but I still worry a little for some reason. (It's also sad that we use "exactly" already for something comparable but distinct in "containsExactly
.") If only Truth's API weren't frozen, I suppose we could consider assertThat(e).isInstanceOf(SomeException.class).andNotASubclass()
, but I don't know what I think of that.
(I want to say that I was just thinking about this exact proposal for some reason earlier in the week... Ah, it must have been related to the idea that the failure message would ideally say something like value of: throwable.getClass()
, and #1224 was inching us in the direction of making that possible.)
from truth.
Related Issues (20)
- Consider reducing ImmutableList array copying HOT 1
- Wanted: Truth8Junit to support Java 8 types with assume() HOT 3
- Compatibility with gradle 7.6 HOT 2
- UsingCorrespondence should fail on use of equals(), like Subject does
- 1.1.4, Guava update leads to java.lang.UnsupportedOperationException at WindowsSecurityDescriptor.java:358 HOT 2
- Add StringSubject isNotBlank or hasText HOT 2
- Idk
- com.google.guava:guava library version update HOT 2
- Write a comparison to Kluent, AssertK, Strikt, Atrium, Kotest, other Kotlin assertion libraries? HOT 13
- Protobuf: allow unpopulated required fields when `comparingExpectedFieldsOnly` HOT 5
- Truth 1.1+ depends on Java 8 bytecode HOT 3
- FAQ entry: Why we don't support multiple chained calls to assertion methods on the same Subject HOT 3
- Port a core set of unit tests to kotlin HOT 2
- JUnit5 support for the existing soft-assertions support (Expect.create JUnit4 Rule) HOT 5
- Document when arrays are compared by their contents vs. their object identity HOT 3
- New extension for Gradle's TestKit HOT 1
- Asymmetric matchers like Jest's expect.stringContaining("") ? HOT 3
- Add MapSubject#containsAnyOf HOT 2
- Allow logical operations on assertions HOT 4
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 truth.