GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - bwaldvogel/mongo-java-server: Fake implementation of MongoDB in Java that speaks the wire protocol.

BranchesTags
Open more actions menu

Latest commit

 

History

1,495 Commits
1,495 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
code-style/eclipse
code-style/eclipse
 
 
core
core
 
 
examples
examples
 
 
gradle/wrapper
gradle/wrapper
 
 
h2-backend
h2-backend
 
 
memory-backend
memory-backend
 
 
postgresql-backend
postgresql-backend
 
 
test-common
test-common
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
buildscript-gradle.lockfile
buildscript-gradle.lockfile
 
 
docker-compose.yml
docker-compose.yml
 
 
gradle.lockfile
gradle.lockfile
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
release.sh
release.sh
 
 
settings.gradle
settings.gradle
 
 
updateDependencies.sh
updateDependencies.sh
 
 

Repository files navigation

CI Maven Central codecov BSD 3-Clause License

MongoDB Java Server

Fake implementation of the core MongoDB server in Java that can be used for integration tests.

Think of H2/HSQLDB/SQLite but for MongoDB.

Java 21 or newer is required.

The MongoDB Wire Protocol is implemented with Netty. Different backends are possible and can be extended.

In-Memory backend

The in-memory backend is the default backend that is typically used to fake MongoDB for integration tests. It supports most CRUD operations, commands and the aggregation framework. Some features are not yet implemented, such as transactions, full-text search or map/reduce.

Add the following Maven dependency to your project:

<dependency>
    <groupId>de.bwaldvogel</groupId>
    <artifactId>mongo-java-server</artifactId>
    <version>1.47.0</version>
</dependency>

Example

class SimpleTest {

    private MongoCollection<Document> collection;
    private MongoClient client;
    private MongoServer server;

    @BeforeEach
    void setUp() {
        server = new MongoServer(new MemoryBackend());

        // optionally:
        // server.enableSsl(key, keyPassword, certificate);
        // server.enableOplog();

        // bind on a random local port
        String connectionString = server.bindAndGetConnectionString();

        client = MongoClients.create(connectionString);
        collection = client.getDatabase("testdb").getCollection("testcollection");
    }

    @AfterEach
    void tearDown() {
        client.close();
        server.shutdown();
    }

    @Test
    void testSimpleInsertQuery() {
        assertThat(collection.countDocuments()).isZero();

        // creates the database and collection in memory and insert the object
        Document obj = new Document("_id", 1).append("key", "value");
        collection.insertOne(obj);

        assertThat(collection.countDocuments()).isEqualTo(1L);
        assertThat(collection.find().first()).isEqualTo(obj);
    }

}

Example with SpringBoot

@RunWith(SpringRunner.class)
@SpringBootTest(classes={SimpleSpringBootTest.TestConfiguration.class})
public class SimpleSpringBootTest {

    @Autowired private MyRepository repository;

    @Before
    public void setUp() {
        // initialize your repository with some test data
        repository.deleteAll();
        repository.save(...);
    }

    @Test
    public void testMyRepository() {
        // test your repository ...
        ...
    }

    @Configuration
    @EnableMongoTestServer
    @EnableMongoRepositories(basePackageClasses={MyRepository.class})
    protected static class TestConfiguration {
        // test bean definitions ...
        ...
    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(MongoTestServerConfiguration.class)
public @interface EnableMongoTestServer {

}

public class MongoTestServerConfiguration {
	@Bean
	public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDbFactory) {
		return new MongoTemplate(mongoDbFactory);
	}

	@Bean
	public MongoDatabaseFactory mongoDbFactory(MongoServer mongoServer) {
		String connectionString = mongoServer.getConnectionString();
		return new SimpleMongoClientDatabaseFactory(connectionString + "/test");
	}

	@Bean(destroyMethod = "shutdown")
	public MongoServer mongoServer() {
		MongoServer mongoServer = new MongoServer(new MemoryBackend());
		mongoServer.bind();
		return mongoServer;
	}
}

H2 MVStore backend

The H2 MVStore backend connects the server to a MVStore that can either be in-memory or on-disk.

<dependency>
    <groupId>de.bwaldvogel</groupId>
    <artifactId>mongo-java-server-h2-backend</artifactId>
    <version>1.47.0</version>
</dependency>

Example

public class Application {

    public static void main(String[] args) throws Exception {
        MongoServer server = new MongoServer(new H2Backend("database.mv"));
        server.bind("localhost", 27017);
    }

}

PostgreSQL backend

The PostgreSQL backend is a proof-of-concept implementation that connects the server to a database in a running PostgreSQL 9.5+ instance. Each MongoDB database is mapped to a schema in Postgres and each MongoDB collection is stored as a table.

<dependency>
    <groupId>de.bwaldvogel</groupId>
    <artifactId>mongo-java-server-postgresql-backend</artifactId>
    <version>1.47.0</version>
</dependency>

Example

public class Application {

    public static void main(String[] args) throws Exception {
        DataSource dataSource = new org.postgresql.jdbc3.Jdbc3PoolingDataSource();
        dataSource.setDatabaseName(…);
        dataSource.setUser(…);
        dataSource.setPassword(…);
        MongoServer server = new MongoServer(new PostgresqlBackend(dataSource));
        server.bind("localhost", 27017);
    }

}

Building a "fat" JAR that contains all dependencies

If you want to build a version that is not on Maven Central you can do the following:

  1. Build a "fat" JAR that includes all dependencies using "./gradlew shadowJar"
  2. Copy build/libs/mongo-java-server-[version]-all.jar to your project, e.g. to the libs directory.
  3. Import that folder (e.g. via Gradle using testCompile fileTree(dir: 'libs', include: '*.jar'))

Contributing

Please read the contributing guidelines if you want to contribute code to the project.

Ideas for other backends

Faulty backend

A faulty backend could randomly fail queries or cause timeouts. This could be used to test the client for error resilience.

Fuzzy backend

Fuzzing the wire protocol could be used to check the robustness of client drivers.

Transactions

Please note that transactions are currently not supported. Please see the discussion in issue #143.

When using mongo-java-server for integration tests, you can use Testcontainers or Embedded MongoDB instead to spin-up a real MongoDB that will have full transaction support.

Related Work

  • Testcontainers

    • Can be used to spin-up a real MongoDB instance in a Docker container
  • Embedded MongoDB

    • Spins up a real MongoDB instance
  • fongo

    • focus on unit testing
    • no wire protocol implementation
    • intercepts the java mongo driver
    • currently used in nosql-unit

About

Fake implementation of MongoDB in Java that speaks the wire protocol.

Topics

in-memoryjavamongodbnetty

Resources

Readme
BSD-3-Clause license

Contributing

Contributing
Activity

Stars

290 stars

Watchers

17 watching

Forks

98 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.