Interface MongoDb
-
- All Known Subinterfaces:
MongoDbClassExtension
- All Known Implementing Classes:
StartLocalMongoDbClassExtension
,UseExistingMongoDbClassExtension
public interface MongoDb
JUnit Test extension for running a MongoDB instance alongside the (integration) tests. Can be configured with custom user credentials and database name. UsegetHosts()
to retrieve the hosts to connect to.Example usage:
@RegisterExtension MongoDbClassExtension.builder() .withDatabase(DATABASE_NAME) .withUsername(DATABASE_USERNAME) .withPassword(DATABASE_PASSWORD) .withTimeoutInMillis(30_000) .enableScripting() .build();
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
MongoDb.Builder<T extends MongoDb>
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
OVERRIDE_MONGODB_CONNECTION_STRING_SYSTEM_PROPERTY_NAME
"TEST_MONGODB_CONNECTION_STRING" is the name of the environment variable that may hold a MongoDB Connection String of the database used in tests instead of starting a dedicated instance.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default void
clearCollections()
Removes all documents from the database passed during construction.default void
clearDatabase()
Removes all collections and documents from the database passed during construction.com.mongodb.MongoClient
createClient()
Creates a MongoClient that is connected to the database.java.lang.String
getConnectionString()
java.lang.String
getDatabase()
java.lang.String
getHosts()
Deprecated, for removal: This API element is subject to removal in a future version.Please use 'getConnectionString() instead'.java.lang.String
getOptions()
Deprecated, for removal: This API element is subject to removal in a future version.Please use 'getConnectionString()' instead.java.lang.String
getPassword()
Deprecated, for removal: This API element is subject to removal in a future version.Please use 'getConnectionString()' instead.default java.lang.String
getServerVersion()
java.lang.String
getUsername()
Deprecated, for removal: This API element is subject to removal in a future version.Please use 'getConnectionString()' instead.
-
-
-
Field Detail
-
OVERRIDE_MONGODB_CONNECTION_STRING_SYSTEM_PROPERTY_NAME
static final java.lang.String OVERRIDE_MONGODB_CONNECTION_STRING_SYSTEM_PROPERTY_NAME
"TEST_MONGODB_CONNECTION_STRING" is the name of the environment variable that may hold a MongoDB Connection String of the database used in tests instead of starting a dedicated instance.- See Also:
- Constant Field Values
-
-
Method Detail
-
getHosts
@Deprecated(forRemoval=true) java.lang.String getHosts()
Deprecated, for removal: This API element is subject to removal in a future version.Please use 'getConnectionString() instead'.- Returns:
- the hostname and port that can be used to connect to the database. The result may contain a comma separated list of hosts as in the MongoDB Connection String
-
getUsername
@Deprecated(forRemoval=true) java.lang.String getUsername()
Deprecated, for removal: This API element is subject to removal in a future version.Please use 'getConnectionString()' instead.- Returns:
- the username that must be used to connect to the database.
-
getPassword
@Deprecated(forRemoval=true) java.lang.String getPassword()
Deprecated, for removal: This API element is subject to removal in a future version.Please use 'getConnectionString()' instead.- Returns:
- the password that must be used to connect to the database.
-
getDatabase
java.lang.String getDatabase()
- Returns:
- the initialized database
-
getOptions
@Deprecated(forRemoval=true) java.lang.String getOptions()
Deprecated, for removal: This API element is subject to removal in a future version.Please use 'getConnectionString()' instead.- Returns:
- the MongoDB options String without leading question mark
-
getConnectionString
java.lang.String getConnectionString()
- Returns:
- the MongoDB connection String
-
createClient
com.mongodb.MongoClient createClient()
Creates a MongoClient that is connected to the database. The caller is responsible for closing the connection.- Returns:
- A MongoClient
-
getServerVersion
default java.lang.String getServerVersion()
- Returns:
- the version of the MongoDB instance which is associated with this MongoDbClassExtension
-
clearCollections
default void clearCollections()
Removes all documents from the database passed during construction. Keeps the collections and indices on the collections.
-
clearDatabase
default void clearDatabase()
Removes all collections and documents from the database passed during construction. Take care that this also removes all indices from collections.
-
-