I have an arquillian component test where I am wanting to use an in-memory MongoDB (Fongo) database using NoSqlUnit. I am using a @Producer to define my DataStoreConnection and I am using Eclipse MicroProfile on Java SE 8.
The issue is that after initiating the in-memory DB, I am not able to access it programmatically in my code when doing my endpoint tests.
I have a DataStoreConnectionProducer as such:
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
@ApplicationScoped
public class DataStoreConnectionProducer {
private MongoClient mongoClient;
private static final Config config = ConfigProvider.getConfig();
@Produces
public MongoDatabase createMongoClient(){
final String DB_PATH = config.getValue( "mongodb.path", String.class );
final int DB_PORT = config.getValue( "mongodb.port", Integer.class );
final String DB_NAME = config.getValue( "mongodb.dbname", String.class );
if( DB_NAME.equals( "test" ) )
return new MongoClient().getDatabase(DB_NAME);
else
return new MongoClient( DB_PATH, DB_PORT ).getDatabase( DB_NAME );
}
}
My GreetingDAO is injecting the MongoDatabase using
@Inject MongoDatabase mongoDatabase;
My Resource looks as such:
@Path( "/greetings" )
public class HelloResource {
@Inject
private GreetingDAO greetingDAO;
@Inject
private GreetingService greetingService;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getGreeting (){
return Response.ok(greetingDAO.findAll(), MediaType.APPLICATION_JSON).build();
}
@GET
@Path( "{id}" )
@Produces( MediaType.APPLICATION_JSON )
public Response getGreetingById( @PathParam( "id" ) String greetingId ){
try {
return Response.ok( greetingDAO.findByID( greetingId.toLowerCase() ), MediaType.APPLICATION_JSON ).build();
}catch ( NoSuchElementException ex ){
ex.printStackTrace();
return Response.status( 404 ).build();
}
}
Finally my Arquillian test:
@RunWith( Arquillian.class )
@RunAsClient
public class HelloResourceTest extends AbstractTest{
private static final String DB_NAME = "test";
@ClassRule
public static final InMemoryMongoDb inMemoryMongoDb =
newInMemoryMongoDbRule().targetPath( "localhost" ).build();
@Rule
public MongoDbRule embeddedMongoDbRule = newMongoDbRule()
.defaultEmbeddedMongoDb(DB_NAME);
@Inject MongoClient mongoClient;
@Deployment
public static WebArchive createDeployment () {
WebArchive war = createBasicDeployment()
.addClasses(
HelloResource.class,
GreetingDAO.class,
GreetingService.class,
Greeting.class,
DAO.class,
DataStoreConnectionProducer.class
);
System.out.println( war.toString(true) );
return war;
}
private MongoDatabase getFongoDataBase(){
return mongoClient.getDatabase( DB_NAME );
}
This is pretty much where I start getting confused.. Knowing that Fongo is a in-memory DB, surely there is no remote way to access it? Rather, I would surely have to supply that to my DataStoreConnectionProducer or somehow inject it to my GreetingDAO so that the FongoDB is used rather than the @Producer trying to connect to my managed MongoDB.
A question you might ask: Why not use a Managed MongoDB? Answer: Because I wish to do component based tests, rather than Integration testing.