Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

junit5 extensions #125

Open
nhomble opened this issue Apr 5, 2020 · 2 comments
Open

junit5 extensions #125

nhomble opened this issue Apr 5, 2020 · 2 comments

Comments

@nhomble
Copy link

nhomble commented Apr 5, 2020

I love this library and I use it all the time. I find my test code has the same boiler plate (mostly taken from the README) in every test. Are there any concerns with adding another module that would house the test harnesses?

A junit extension could handle the lifecycle of the mongo server and allow test methods to inject the client as a parameter as needed.

@markbigler
Copy link
Contributor

A JUnit extension would definitely be nice.

In the meantime you can also outsource the "boiler plate code" used for the mongo-java-server configuration to a separate class ...

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

	@Bean
	public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
		return new SimpleMongoClientDbFactory(mongoClient, "test");
	}

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

	@Bean(destroyMethod="close")
	public MongoClient mongoClient(MongoServer mongoServer) {
		return MongoClients.create("mongodb://" + mongoServer.getLocalAddress().getHostName() + ":" + mongoServer.getLocalAddress().getPort());
	}
}

... and import it in all your tests:

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

	// @Test ...

	@Configuration
	@Import({MongoTestServerConfiguration.class})
	@EnableMongoRepositories(basePackageClasses={MyRepository.class})
	protected static class TestConfiguration {
		// ...
	}
}

Or you can create an annotation like this ...

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

... and use it in your test:

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

	// @Test ...

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

@bwaldvogel
Copy link
Owner

Yes, such an extension would make sense.
I would be happy to review/merge a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants