nosql-unit | NoSQL Unit is a JUnit extension | Database library
kandi X-RAY | nosql-unit Summary
kandi X-RAY | nosql-unit Summary
Unit testing is a method by which the smallest testable part of an application is validated. Unit tests must follow the FIRST Rules; these are Fast, Isolated, Repeatable, Self-Validated and Timely. It is strange to think about a JEE application without persistence layer (typical Relational databases or new NoSQL databases) so should be interesting to write unit tests of persistence layer too. When we are writing unit tests of persistence layer we should focus on to not break two main concepts of FIRST rules, the fast and the isolated ones. Our tests will be fast if they don’t access network nor filesystem, and in case of persistence systems network and filesystem are the most used resources. In case of RDBMS ( SQL ), many Java in-memory databases exist like Apache Derby , H2 or HSQLDB . These databases, as their name suggests are embedded into your program and data are stored in memory, so your tests are still fast. The problem is with NoSQL systems, because of their heterogeneity. Some systems work using Document approach (like MongoDb ), other ones Column (like Hbase ), or Graph (like Neo4J ). For this reason the in-memory mode should be provided by the vendor, there is no a generic solution. Our tests must be isolated from themselves. It is not acceptable that one test method modifies the result of another test method. In case of persistence tests this scenario occurs when previous test method insert an entry to database and next test method execution finds the change. So before execution of each test, database should be found in a known state. Note that if your test found database in a known state, test will be repeatable, if test assertion depends on previous test execution, each execution will be unique. For homogeneous systems like RDBMS , DBUnit exists to maintain database in a known state before each execution. But there is no like DBUnit framework for heterogeneous NoSQL systems. NoSQLUnit resolves this problem by providing a JUnit extension which helps us to manage lifecycle of NoSQL systems and also take care of maintaining databases into known state.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Deep equality between two objects .
- To index options .
- Parses the given collection of strings and returns a map of collection of properties .
- Clears all documents .
- Resolves the default location for the method .
- Creates a new vault configuration from the vault configuration .
- Validates columns families .
- Waits until a replica set is become stable .
- Starts the Cassandra .
- Insert tokens into the vault .
nosql-unit Key Features
nosql-unit Examples and Code Snippets
Community Discussions
Trending Discussions on nosql-unit
QUESTION
I am using fongo as in memory database for testing my mongodbrepository.
I have taken reference from http://dontpanic.42.nl/2015/02/in-memory-mongodb-for-unit-and.html for unit testing.
To populate sample data, I have added required json file under test/resources/json-data/user/user.json but it's not loaded into fongo.
...ANSWER
Answered 2020-Aug-06 at 19:03For testing I recommend this library this
QUESTION
Doing implementation of a microservice with a few endpoints based on Spring Boot
and MongoDB
and trying to write integration tests using @SpringBootTest
annotation capabilities.
At the moment, I am facing an issue that I need to pre-populate an embedded MongoDB
instance that instantiated only during 'test' phrase with some test data.
And I did not find any out-of-the-box option available in Spring Boot
for this purpose.
Some people advice to use for test data pre-populating tools like mongobee or mongoprefill or nosql-unit but for me, it seems like overhead or workaround, do not want to introduce any new dependencies even in test scope.
So could you please advice: In the current Spring Boot
ecosystem, what is the right way to pre-populate MongoDB
for testing purpose, when we are talking about integration (end-to-end) testing with @SpringBootTest
?
ANSWER
Answered 2020-Mar-11 at 06:44There are multiple ways to pre-populate data:
- Use the JUnit lifecycle methods like
@BeforeEach
,@BeforeAll
to fill in data - You could disable the Spring Boot autoconfiguration for the embedded MongoDB and do it on your own and insert data after creating the connection
- You could somehow mirror the
@Sql
feature we have for testing relational databases and write something similar using theAsbtractTestExectuionListener
. For this have a look at the Spring classSqlScriptsTestExecutionListener
- Provide a class that implements the
CommandLineRunner
interface and only activate this bean for your integration test profile with@Profile("integration-test")
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install nosql-unit
In NoSQLUnit we can define a replica set architecture and starting it up, so our tests are executed against a replica set servers instead of a single server. Due the nature of replica set system, we can only create a replica set of managed servers. So let’s see how to define an architecture and starting all related servers. The main class is ReplicaSetManagedMongoDb which manages lifecycle of all servers involved in replica set. To build a ReplicaSetManagedMongoDb class, ReplicaSetBuilder builder class is provided and it will allow us to define the replica set architecture. Using it we can set the eligible servers (those that can be primary or secondary), the only secondary servers, the arbiters, the hidden ones, and configure all of them with the attributes like priority, voters, or setting tags. So let’s see an example where we are defining two eligible servers and one arbiter in a replica set called rs-test. ~~ {.java} import static com.lordofthejars.nosqlunit.mongodb.replicaset.ReplicaSetBuilder.replicaSet;. @ClassRule public static ReplicaSetManagedMongoDb replicaSetManagedMongoDb = replicaSet( "rs-test") .eligible( newManagedMongoDbLifecycle().port(27017) .dbRelativePath("rs-0").logRelativePath("log-0") .get()) .eligible( newManagedMongoDbLifecycle().port(27018) .dbRelativePath("rs-1").logRelativePath("log-1") .get()) .arbiter( newManagedMongoDbLifecycle().port(27019) .dbRelativePath("rs-2").logRelativePath("log-2") .get()) .get(); ~~. Notice that you must define different port for each server and also a different database path. Also note that ReplicaSetManagedMongoDb won’t let start executing tests until all replica set becomes stable (this can take some minutes). Then we only have to create a MongoDbRule as usually which will populate defined data into replica set servers. For this case a new configuration builder is provided that allows us to define the mongo servers location and the write concern used during seeding phase. By default Aknownledge write concern is used. ~~ {.java} import static com.lordofthejars.nosqlunit.mongodb.ReplicationMongoDbConfigurationBuilder.replicationMongoDbConfiguration;. @Rule public MongoDbRule mongoDbRule = newMongoDbRule().configure( replicationMongoDbConfiguration().databaseName("test") .seed("localhost", 27017) .seed("localhost", 27018) .configure()) .build(); ~~. Now we have configured and deployed a replica set and populated them with the dataset. But NoSQLUnit also provides an utility method to cause server failures. It is as easy as calling shutdownServer method. ~~ {.java} replicaSetManagedMongoDb.shutdownServer(27017); ~~.
Because @ClassRule is used, we are responsible for restarting the system by calling startServer.
System may become unstable and Mongo driver can throw many exceptions (that’s normal because of MonitorThread) and even do some test fails. If you want to wait until all servers become stable again (in real life you won’t have this possibility), you can use next call:
In NoSQLUnit we can define a sharding architecture and starting it up, so our tests are executed against it instead of a single server. Due the nature of sharding system, we can only create sharding for managed servers. So let’s see how to define an architecture and starting all related servers. The main class is ShardedManagedMongoDb which manages lifecycle of all servers involved in sharding (shards, configs and mongos). To build a ShardedManagedMongoDb class, ShardedGroupBuilder builder class is provided and it will allow us to define each server involved in sharding. Let’s see an example on how to set up and start a system with two shards, one config server and one mongos. ~~ {.java} @ClassRule public static ShardedManagedMongoDb shardedManagedMongoDb = shardedGroup() .shard(newManagedMongoDbLifecycle().port(27018).dbRelativePath("rs-1").logRelativePath("log-1").get()) .shard(newManagedMongoDbLifecycle().port(27019).dbRelativePath("rs-2").logRelativePath("log-2").get()) .config(newManagedMongoDbLifecycle().port(27020).dbRelativePath("rs-3").logRelativePath("log-3").get()) .mongos(newManagedMongosLifecycle().configServer(27020).get()) .get(); ~~. Notice that you must define different port for each server and also a different database path. Also note that in case of mongos you must set the config server port, and is not necessary to set up the database path. And finally we only have to create a MongoDbRule as usually which will populate defined data into sharding servers. For this case we must use the same builder used for replica set but enabling sharding. Keep in mind that in this case we only have to register the mongos instances, not shards or config servers. ~~ {.java} @Rule public MongoDbRule mongoDbRule = newMongoDbRule().configure( replicationMongoDbConfiguration().databaseName("test") .enableSharding() .seed("localhost", 27017) .configure()) .build(); ~~.
In NoSQLUnit we can define a replicated sharded cluster architecture and starting it up, so our tests are executed against it instead of a single server. Due the nature of replicated sharded cluster, we can only create sharding for managed servers. So let’s see how to define an architecture and starting all related servers. The main class is ShardedManagedMongoDb which manages lifecycle of all servers involved in sharding (shards, configs and mongos). To build a ShardedManagedMongoDb class, ShardedGroupBuilder builder class is provided and it will allow us to define each server involved in sharding, but in contrast of sharding, we need to add a replica set instead of a shard. For this reason ReplicaSetManagedMongoDb is also used. Let’s see an example on how to set up two replicated sharded cluster, with one member each replica set, (of course in production environment you would have more), one config server and one mongos. ~~ {.java} import static com.lordofthejars.nosqlunit.mongodb.shard.ShardedGroupBuilder.shardedGroup; import static com.lordofthejars.nosqlunit.mongodb.replicaset.ReplicaSetBuilder.replicaSet;. @ClassRule public static ShardedManagedMongoDb shardedManagedMongoDb = shardedGroup() .replicaSet(replicaSet("rs-test-1") .eligible( newManagedMongoDbLifecycle() .port(27007).dbRelativePath("rs-0").logRelativePath("log-0") .get() ) .get()) .replicaSet(replicaSet("rs-test-2") .eligible( newManagedMongoDbLifecycle() .port(27009).dbRelativePath("rs-0").logRelativePath("log-0") .get() ) .get()) .config(newManagedMongoDbLifecycle().port(27020).dbRelativePath("rs-3").logRelativePath("log-3").get()) .mongos(newManagedMongosLifecycle().configServer(27020).get()) .get(); ~~. Note that we are using the replicaSet method of shardedGroup to create a replica set inside a sharded, and then we use methods defined into ReplicaSetBuilder to configure the replica set. And finally we only have to create a MongoDbRule as usually which will populate defined data into servers. For replicated sharded clusters we can use the same class and dataset as sharding. ~~ {.java} @Rule public MongoDbRule mongoDbRule = newMongoDbRule().configure( replicationMongoDbConfiguration().databaseName("test") .enableSharding() .seed("localhost", 27017) .configure()) .build(); ~~. ~~ {.json} { "collection_name": { "shard-key-pattern": ["attribute_1", "attribute_2"], "data": [ {"attribute_1":"value_1","attribute_2":value_2, "attribute_3":"value_3"} ] } } ~~.
To use NoSQLUnit with Neo4j you only need to add next dependency:. ~~ {.xml} <dependency> <groupId>com.lordofthejars</groupId> <artifactId>nosqlunit-neo4j</artifactId> <version>${version.nosqlunit}</version> </dependency> ~~.
To use NoSQLUnit with Cassandra you only need to add next dependency:. ~~ {.xml} <dependency> <groupId>com.lordofthejars</groupId> <artifactId>nosqlunit-cassandra</artifactId> <version>${version.nosqlunit}</version> </dependency> ~~.
To use NoSQLUnit with Redis you only need to add next dependency:. ~~ {.xml} <dependency> <groupId>com.lordofthejars</groupId> <artifactId>nosqlunit-redis</artifactId> <version>${version.nosqlunit}</version> </dependency> ~~.
To use NoSQLUnit with HBase you only need to add next dependency:. ~~ {.xml} <dependency> <groupId>com.lordofthejars</groupId> <artifactId>nosqlunit-hbase</artifactId> <version>${version.nosqlunit}</version> </dependency> ~~.
To use NoSQLUnit with CouchDB you only need to add next dependency:. ~~ {.xml} <dependency> <groupId>com.lordofthejars</groupId> <artifactId>nosqlunit-couchdb</artifactId> <version>${version.nosqlunit}</version> </dependency> ~~.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page