Comments (10)
JSON is just a way to represent the object being tested with snapshot. If the objects compared are deeply equal, default GSON serializer will serialize them equally every time. If you provide a custom JSON serializer function, you must ensure the same behavior.
from json-snapshot.github.io.
default GSON serializer will serialize them equally every time
@andrebonna This doesn't match with my observations. The way some HashMaps are being serialized in my test cases seems to be non-deterministic. I get errors like this one all the time:
Changed content at line 22:
expecting:
[" "LFDNR": "1",",
" "KTOIBAN": "DE56940596610000008888""]
but was:
[" "KTOIBAN": "DE56940596610000008888",",
" "LFDNR": "1""]
Is there a way to avoid this behavior?
from json-snapshot.github.io.
You are right, I totally forgot HashMaps serialization. Fortunatelly, Jackson serializer has a config property to solve this ordering issue, SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS
. I just changed the default JSON serializer library from GSON to Jackson and used the config property to ensure always the same key ordering of a map. That should be fixed in version 1.0.14
. I'm reopening the issue until you confirm the bug has been solved on your side.
from json-snapshot.github.io.
@andrebonna It seems to be fixed. Thanks!
from json-snapshot.github.io.
I'm generation a new version compiled to Java 8. Version 1.0.15
from json-snapshot.github.io.
Hi @andrebonna,
it still doesn't work for me, unless I also add
objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
in the method io.github.jsonSnapshot.SnapshotMatcher.buildObjectMapper()
Further reading: https://www.stubbornjava.com/posts/creating-a-somewhat-deterministic-jackson-objectmapper
Pls let me know if I can help with this issue.
from json-snapshot.github.io.
Please, provide us and example. JSON properties should be in the same order as Class properties definition.
from json-snapshot.github.io.
Please, provide us an example. JSON properties should be in the same order as Class properties definition.
Thx for the reply. I'm sorry, but I lack the time to look into trying to extract the case into an example. However, as we are open source, if you want you can
-
check out our code,
-
switch to branch
gh4959-app
(EDIT: added branch) -
edit https://github.com/metasfresh/metasfresh-webui-api/blob/gh4959-app/pom.xml and remove the
-PR
from<artifactId>json-snapshot-PR</artifactId>
-
and try out the test case https://github.com/metasfresh/metasfresh-webui-api/blob/gh4959-app/src/test/java/de/metas/ui/web/dataentry/window/descriptor/factory/DataEntryTabLoaderTest.java
-
Without
objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
thesnap
file sas created on the first run, and then created differently on the second run -
When I run this with #12 plus
objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
,
then it worked in Eclipse (See next copmment for details, i believe it's related but not same same topic)
from json-snapshot.github.io.
Turned out that for me even objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
didn't really help to create the same snapshot on my local machine and our CI.
I suspect different java versions to be part of the problem
- Note: jackson was the same (tried both
2.8.8
and2.9.8
), and we also use maven-enforcer to keep the dependency tree clean of duplicate classes and different versions of the same artifact)
The problem occurred with an interface that had default methods/properties:
- on my local machine the properties were still ordered alphabetically "globally"
- on jenkins I got the interface's properties (ordered alphabetically) followed by the implementing class's properties (also ordered alphabetically)
Here is my failed test: https://jenkins.metasfresh.com/job/metasfresh-webui/job/gh4959-app/12/testReport/de.metas.ui.web.dataentry.window.descriptor.factory/DataEntryTabLoaderTest/createGroupEntityDescriptors_verify_DocumentEntityDescriptor/ ..just for completeness..i'm not sure if it helps 😊
I eventually got around this problem by explicitly setting the order (i.e. @JsonPropertyOrder(value = { "columnName", "defaultOrderBy", "defaultOrderByAscending", "mandatory" })
), but to me that's just a workaround. I want my collegues to love your tool and use it broadly, and I think for that, can'T tell them "yeah, it's great but you need to instrument your production code like this and that etc etc".
So, I'm going to check out https://github.com/skyscreamer/JSONassert as documented here: https://www.baeldung.com/jsonassert .
I plan to extend SnapshotConfig
to allow a more lenient comparison instead of the current if (!rawSnapshot.trim().equals(currentObject.trim()))
.
from json-snapshot.github.io.
Maybe it would be possible to compare expected and actual JSON trees for equality using JsonNode#equals
?
I'd expect Jackson to return true for two nodes that have the same fields and values, regardless of field order, but I have not checked.
from json-snapshot.github.io.
Related Issues (20)
- [Windows] Comparison with stored snapshot fails due to line endings
- [JUnit5] Support non-public test methods
- Test method lookup fails when method has parameters HOT 1
- Documentation example does not work HOT 1
- Unnecessary transitive dependencies pulled in through this lib HOT 1
- Feature request: Add JUnit 5 extension that invokes start/validateSnapshots
- Support parameterised tests HOT 1
- Change extension `.snap`
- [JUnit 5] Add support for nested tests HOT 2
- Avoid EOL problems when loading snapshot file HOT 1
- Does this work with TestNG?
- Lib can not be used with Jackson 2.10.0 or later HOT 4
- Snapshot failing even though nothing changed HOT 5
- is this project alive? HOT 6
- Add support for meta JUnit annotations
- Getting class not found error at Start Method of snapshot class HOT 2
- Warning about unused snapshot doesn't need to show all the contents
- [Query] Can snapshot testing be done for protobuf messages?
- Ignore certain snapshot fields.
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 json-snapshot.github.io.