{"id":15014347,"url":"https://github.com/bwaldvogel/mongo-java-server","last_synced_at":"2025-05-15T05:07:33.761Z","repository":{"id":1667647,"uuid":"2394379","full_name":"bwaldvogel/mongo-java-server","owner":"bwaldvogel","description":"Fake implementation of MongoDB in Java that speaks the wire protocol.","archived":false,"fork":false,"pushed_at":"2024-12-30T11:25:07.000Z","size":4251,"stargazers_count":288,"open_issues_count":26,"forks_count":90,"subscribers_count":18,"default_branch":"main","last_synced_at":"2025-04-15T05:17:45.708Z","etag":null,"topics":["in-memory","java","mongodb","netty"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bwaldvogel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-09-15T18:22:58.000Z","updated_at":"2025-03-10T15:22:09.000Z","dependencies_parsed_at":"2024-09-30T08:00:44.836Z","dependency_job_id":"c86d3759-ea0d-4311-88b2-37b643eca0a6","html_url":"https://github.com/bwaldvogel/mongo-java-server","commit_stats":{"total_commits":1337,"total_committers":35,"mean_commits":38.2,"dds":0.07703814510097229,"last_synced_commit":"b6d0a5e97db311bfe0774eb95c879e48bec22b5a"},"previous_names":[],"tags_count":64,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwaldvogel%2Fmongo-java-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwaldvogel%2Fmongo-java-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwaldvogel%2Fmongo-java-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwaldvogel%2Fmongo-java-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bwaldvogel","download_url":"https://codeload.github.com/bwaldvogel/mongo-java-server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254276447,"owners_count":22043867,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["in-memory","java","mongodb","netty"],"created_at":"2024-09-24T19:45:30.410Z","updated_at":"2025-05-15T05:07:28.752Z","avatar_url":"https://github.com/bwaldvogel.png","language":"Java","readme":"[![CI](https://github.com/bwaldvogel/mongo-java-server/workflows/CI/badge.svg)](https://github.com/bwaldvogel/mongo-java-server/actions)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/de.bwaldvogel/mongo-java-server/badge.svg)](http://maven-badges.herokuapp.com/maven-central/de.bwaldvogel/mongo-java-server)\n[![codecov](https://codecov.io/gh/bwaldvogel/mongo-java-server/branch/main/graph/badge.svg?token=jZ7zBT4niu)](https://codecov.io/gh/bwaldvogel/mongo-java-server)\n[![BSD 3-Clause License](https://img.shields.io/github/license/bwaldvogel/mongo-java-server.svg)](https://opensource.org/licenses/BSD-3-Clause)\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/BenediktWaldvogel)\n\n# MongoDB Java Server #\n\nFake implementation of the core [MongoDB][mongodb] server in Java that can be used for integration tests.\n\nThink of H2/HSQLDB/SQLite but for MongoDB.\n\nThe [MongoDB Wire Protocol][wire-protocol] is implemented with [Netty][netty].\nDifferent backends are possible and can be extended.\n\n## In-Memory backend ##\n\nThe in-memory backend is the default backend that is typically used to fake MongoDB for integration tests.\nIt supports most CRUD operations, commands and the aggregation framework.\nSome features are not yet implemented, such as transactions, full-text search or map/reduce.\n\nAdd the following Maven dependency to your project:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ede.bwaldvogel\u003c/groupId\u003e\n    \u003cartifactId\u003emongo-java-server\u003c/artifactId\u003e\n    \u003cversion\u003e1.46.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Example ###\n\n```java\nclass SimpleTest {\n\n    private MongoCollection\u003cDocument\u003e collection;\n    private MongoClient client;\n    private MongoServer server;\n\n    @BeforeEach\n    void setUp() {\n        server = new MongoServer(new MemoryBackend());\n\n        // optionally:\n        // server.enableSsl(key, keyPassword, certificate);\n        // server.enableOplog();\n\n        // bind on a random local port\n        String connectionString = server.bindAndGetConnectionString();\n\n        client = MongoClients.create(connectionString);\n        collection = client.getDatabase(\"testdb\").getCollection(\"testcollection\");\n    }\n\n    @AfterEach\n    void tearDown() {\n        client.close();\n        server.shutdown();\n    }\n\n    @Test\n    void testSimpleInsertQuery() throws Exception {\n        assertThat(collection.countDocuments()).isZero();\n\n        // creates the database and collection in memory and insert the object\n        Document obj = new Document(\"_id\", 1).append(\"key\", \"value\");\n        collection.insertOne(obj);\n\n        assertThat(collection.countDocuments()).isEqualTo(1L);\n        assertThat(collection.find().first()).isEqualTo(obj);\n    }\n\n}\n```\n\n### Example with SpringBoot ###\n\n```java\n@RunWith(SpringRunner.class)\n@SpringBootTest(classes={SimpleSpringBootTest.TestConfiguration.class})\npublic class SimpleSpringBootTest {\n\n    @Autowired private MyRepository repository;\n\n    @Before\n    public void setUp() {\n        // initialize your repository with some test data\n        repository.deleteAll();\n        repository.save(...);\n    }\n\n    @Test\n    public void testMyRepository() {\n        // test your repository ...\n        ...\n    }\n\n    @Configuration\n    @EnableMongoTestServer\n    @EnableMongoRepositories(basePackageClasses={MyRepository.class})\n    protected static class TestConfiguration {\n        // test bean definitions ...\n        ...\n    }\n}\n\n@Target(ElementType.TYPE)\n@Retention(RetentionPolicy.RUNTIME)\n@Import(MongoTestServerConfiguration.class)\npublic @interface EnableMongoTestServer {\n\n}\n\npublic class MongoTestServerConfiguration {\n\t@Bean\n\tpublic MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDbFactory) {\n\t\treturn new MongoTemplate(mongoDbFactory);\n\t}\n\n\t@Bean\n\tpublic MongoDatabaseFactory mongoDbFactory(MongoServer mongoServer) {\n\t\tString connectionString = mongoServer.getConnectionString();\n\t\treturn new SimpleMongoClientDatabaseFactory(connectionString + \"/test\");\n\t}\n\n\t@Bean(destroyMethod = \"shutdown\")\n\tpublic MongoServer mongoServer() {\n\t\tMongoServer mongoServer = new MongoServer(new MemoryBackend());\n\t\tmongoServer.bind();\n\t\treturn mongoServer;\n\t}\n}\n```\n\n## H2 MVStore backend ##\n\nThe [H2 MVStore][h2-mvstore] backend connects the server to a `MVStore` that\ncan either be in-memory or on-disk.\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ede.bwaldvogel\u003c/groupId\u003e\n    \u003cartifactId\u003emongo-java-server-h2-backend\u003c/artifactId\u003e\n    \u003cversion\u003e1.46.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Example ###\n\n```java\npublic class Application {\n\n    public static void main(String[] args) throws Exception {\n        MongoServer server = new MongoServer(new H2Backend(\"database.mv\"));\n        server.bind(\"localhost\", 27017);\n    }\n\n}\n```\n\n## PostgreSQL backend ##\n\nThe PostgreSQL backend is a proof-of-concept implementation that connects the server to a database in a running\nPostgreSQL 9.5+ instance. Each MongoDB database is mapped to a schema in\nPostgres and each MongoDB collection is stored as a table.\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ede.bwaldvogel\u003c/groupId\u003e\n    \u003cartifactId\u003emongo-java-server-postgresql-backend\u003c/artifactId\u003e\n    \u003cversion\u003e1.46.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n\n### Example ###\n\n```java\npublic class Application {\n\n    public static void main(String[] args) throws Exception {\n        DataSource dataSource = new org.postgresql.jdbc3.Jdbc3PoolingDataSource();\n        dataSource.setDatabaseName(…);\n        dataSource.setUser(…);\n        dataSource.setPassword(…);\n        MongoServer server = new MongoServer(new PostgresqlBackend(dataSource));\n        server.bind(\"localhost\", 27017);\n    }\n\n}\n```\n## Building a \"fat\" JAR that contains all dependencies ##\n\nIf you want to build a version that is not on Maven Central you can do the following:\n\n1. Build a \"fat\" JAR that includes all dependencies using \"`./gradlew shadowJar`\"\n2. Copy `build/libs/mongo-java-server-[version]-all.jar` to your project, e.g. to the `libs` directory.\n3. Import that folder (e.g. via Gradle using `testCompile fileTree(dir: 'libs', include: '*.jar')`)\n\n## Contributing ##\n\nPlease read the [contributing guidelines](CONTRIBUTING.md) if you want to contribute code to the project.\n\nIf you want to thank the author for this library or want to support the maintenance work, we are happy to receive a donation.\n\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/BenediktWaldvogel)\n\n## Ideas for other backends ##\n\n### Faulty backend ###\n\nA faulty backend could randomly fail queries or cause timeouts. This could be\nused to test the client for error resilience.\n\n### Fuzzy backend ###\n\nFuzzing the wire protocol could be used to check the robustness of client\ndrivers.\n\n## Transactions ##\n\nPlease note that transactions are currently not supported.\nPlease see [the discussion in issue #143](https://github.com/bwaldvogel/mongo-java-server/issues/143).\n\nWhen using `mongo-java-server` for integration tests, you can use [Testcontainers][testcontainers] or [Embedded MongoDB][embedded-mongodb] instead to spin-up a real MongoDB that will have full transaction support.\n\n## Related Work ##\n\n* [Testcontainers][testcontainers]\n  * Can be used to spin-up a real MongoDB instance in a Docker container\n\n* [Embedded MongoDB][embedded-mongodb]\n  * Spins up a real MongoDB instance\n\n* [fongo][fongo]\n  * focus on unit testing\n  * no wire protocol implementation\n  * intercepts the java mongo driver\n  * currently used in [nosql-unit][nosql-unit]\n\n[mongodb]: http://www.mongodb.org/\n[wire-protocol]: https://docs.mongodb.org/manual/reference/mongodb-wire-protocol/\n[netty]: http://netty.io/\n[testcontainers]: https://www.testcontainers.org/\n[embedded-mongodb]: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo\n[fongo]: https://github.com/fakemongo/fongo\n[nosql-unit]: https://github.com/lordofthejars/nosql-unit\n[h2-mvstore]: http://www.h2database.com/html/mvstore.html\n","funding_links":["https://www.paypal.me/BenediktWaldvogel"],"categories":["测试"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbwaldvogel%2Fmongo-java-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbwaldvogel%2Fmongo-java-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbwaldvogel%2Fmongo-java-server/lists"}