I am trying to make the integration tests run in memory in MongoDB. I would appreciate any idea or suggestion.
What I did:
A dependency on de.flapdoodle.embed:de.flapdoodle.embed.mongo is added to provide embedded MongoDB for integration tests.
The embedmongo-maven-plugin is added to start or stop an instance of MongoDB during a Maven build.
My idea here is when I run 'mvn clean test' command the integration tests connect to the embedded mongo instance and not the "live" one(mongo-java-driver). After running test, I can see in the consolo log embedded mongo is started:
o.s.b.a.mongo.embedded.EmbeddedMongo : 2021-08-04T15:23:14.303+0300 I CONTROL [initandlisten] MongoDB starting : pid=26673 port=54871 dbpath=/var/folders/qv/......
However, I think it doesn't work as expected. Because, in the console log, I can also see:
org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=...
If I shut down the live instance of Mongo, tests failed. :(
Is there anyone to help me to overcome this issue? I want to thank in advance for your time.
Related
I'm making an application for a school project, but I'm running into the issue that when I try to run the unit tests that it tries to connect to the database while starting up the application, which isn't required for the tests (because it will be mocked), and is not available in the CI/CD pipeline.
jdbc connection error
I'm building my project in Java Maven Springboot and would like to know how I can prevent it from trying to connect to the database when running my test.
here is a link to my repository: https://gitlab.com/kwetter_jack/Kwetter_posts/-/tree/ci_cd_setup
Your test classes have #SpringBootTest annotation which will start a Spring application context - as your application uses a database the tests will also try to setup and use a database connection.
The simplest solution is to remove the annotation so the tests no longer try to connect to a database. You'll probably need to mock some more dependencies as a result as Spring is no longer creating these for you. You could also have a look at https://www.baeldung.com/spring-boot-testing for some other ideas how you could alter your tests.
Alternatively if you do want / need the application context to run you can add a application.yaml for the tests that defines and uses a in memory DB so the tests have something to connect to - see https://www.baeldung.com/spring-boot-h2-database for details how to do this.
Just change value under spring.datasource to H2 database to prevent
The application connect the real database.
Test application.yml
FYI, You no need to copy all config from original application.yml, just only some config that you need to override.
while I was investigating the spring boot H2 in-memory database (as suggested by Chris Olive and Paranaaan) I also came across the option of using a test container. after looking into this I saw that this enables the project to create a temp docker container with a MySQL image that I can use during the testing of my project, considering I was planning on using docker anyway for the integration testing of my microservices project I attempted this and it worked as I had hoped it would.
if anyone is interested in the test container solution that I used, the information can be found here:
https://www.testcontainers.org/modules/databases/mysql/
After some radical changes to our schema and reading some posts on why you should avoid in memory databases.
We have decided to use MySQL locally for testing and developing. Using a MySQL docker container with a volume for persistence.
This is fairly straightforward however the issues we are having are the following:
Requires the container to be executed separate from the spring boot application (a manual task docker run
Same goes for stopping the container, its a independant process
My question is essentially, is it possible to have spring boot (when using a dev config profile) to manage this docker container.
i.e. I start development work in IntelliJ and run the service, the service checks if the container is running, if not starts it up.
If this idea is bad, then please let me know.
For testing its not issue, because we are using a maven docker plugin to create the container during the maven lifecycle.
Its more for devs working locally, and getting the service running locally with ease.
Any suggestions welcomed!
Bonus for Intellij setup!
I am using Arquillian to run integration tests. The tool is great, however I am not sure if I am using it correctly. The matter is that after each deployment (I have one deployment per test suite) jboss stops and starts again. It is very annoying as because of it time of running suites is very big. Is there any way to make jboss not restart between deployments?
Thanks in advance
I would guess it's because you are running your Arquillian tests on an embedded container (like this one) which by design automatically stops when the test suite finishes.
You can deploy your tests on a running application server and it wouldn't automatically shut down.
How do I configure derby not to drop my database between each unit/integration test ? I want to keep the data between runs.
dbDialect=DERBY
XADataSourceClassName=org.apache.derby.jdbc.ClientXADataSource
databaseName=ForumThreadDB
createDatabase=update
serverName=localhost
portNumber=1527
DriverClassName=org.apache.derby.jdbc.ClientDriver
url=jdbc:derby://localhost:1527/ForumThreadDB;create=true
user=APP
password=whatever
I just tried to connect to derby outside. It is possible that things are never persisted, although I get no error when persisting, but I remember this have happened before.
I also get this error on startup of the test
---> WARN o.Runtime - An error occurred while registering a ClassTransformer with PersistenceUnitInfo: name 'ForumThreadDomainPU',
root URL
[file:/C:/Projects/OurForum/ForumThreadDomain/target/classes/]. The
error has been consumed. To see it, set your openjpa.Runtime log level
to TRACE. Load-time class transformation will not be available.
I suppose this could be more to do with the Junit setting
try adding
#Rollback(value=false)
before the method for which you don't want the persistence to rollback
If you are running your tests within Maven, you can use a Maven plugin I wrote for Derby. It starts up an in-memory Derby database for the duration of your tests, so all of them could share the same data.
Check out the USAGE file here. The plugin is available via Maven Central so you don't need to add extra repositories.
I'm trying to test my grails app using integration test that makes http requests. Actually it's a selenium/tellurium test, but it doesn't matter. But as i see when i running grails tests it doesn't starts web container, the same time i see many examples in blog articles that people tests grails app using selenium and other test tools that requires http access.
I've create an empty grails app:
mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.grails -DarchetypeArtifactId=grails-maven-archetype -DarchetypeVersion=1.3.4 -DgroupId=example -DartifactId=testportapp
cd testportapp/
mvn initialize
mvn grails:test-app
Tests PASSED - view reports in target/test-reports
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
thats ok.
and now add an integration test that tries to connect to grails test/integration/ListenPortTest.groovy:
class ListenPortTest extends GroovyTestCase {
void testPort(){
def s = new Socket("localhost", 8080);
s.close()
}
}
and run test again. Now we receive following exception:
Connection refused
java.net.ConnectException: Connection refused
....
I've checked it also by using browser, wget and netstat, and looks like grails not started or not opened any port.
And the question:
How i can configure grails to open an port when executing integration tests?
Grails supports two types of tests by default, unit and integration, plus functional testing using a plugin. Unit and integration tests are quite similar and are really both unit tests, except that integration tests have an initialized Spring application context, Hibernate configuration, in-memory database, etc. But no running web server - you need functional tests for that.
There are several options for functional testing, the most popular ones being WebTest: http://grails.org/plugin/webtest, the Functional Testing plugin: http://grails.org/plugin/functional-test, and the Selenium RC plugin: http://grails.org/plugin/selenium-rc.
The newest one is Geb: http://grails.org/plugin/geb and if you're looking for Selenium support it's going to be your best bet. The manual is at http://geb.codehaus.org/ and there was a recent blog post written about it here: http://blog.springsource.com/2010/08/28/the-future-of-functional-web-testing/
I think you are looking for this plugin
http://www.grails.org/plugin/functional-test