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

Using embedded progress with test frameworks outside of JUnit #22

Open
gregsilin opened this issue Dec 12, 2019 · 4 comments
Open

Using embedded progress with test frameworks outside of JUnit #22

gregsilin opened this issue Dec 12, 2019 · 4 comments

Comments

@gregsilin
Copy link

Hi,

We're interested in using the embedded postgres toolkit for our tests, but without JUnit. We are using ScalaTest.

It appears that the library relies heavily on the assumption of integrating with JUnit.

It looks like we should be able to build our own ScalaTest wrappers, unless there is something we're missing from looking at the code.

Could you advise?

@gregsilin gregsilin changed the title Using outside of JUnit Using embedded progress with test frameworks outside of JUnit Dec 12, 2019
@tomix26
Copy link
Collaborator

tomix26 commented Dec 13, 2019

Hi,

yes, it is possible to integrate it with other test frameworks. This library is not tied to any specific framework. Just use lower-level api to create an embedded database directly and wrap it to a ScalaTest extension. Check out the SingleInstancePostgresRule and PreparedDbRule classes for inspiration.

Simple example:

// before tests
EmbeddedPostgres postgres = EmbeddedPostgres.start();
DataSource dataSource = postgres.getPostgresDatabase();

// after tests
postgres.close();

Moreover, if the ScalaTest library were defined as optional dependency, then the scala extension would probably be part of this project, similar to the junit4 and junit5 extensions.

@Tochemey
Copy link

Hey folks, any breakthrough on this? I am using scala test with multiple sbt modules that need the db up before the tests are executed

@tomix26
Copy link
Collaborator

tomix26 commented Nov 30, 2020

@Tochemey This task has a low priority for me. So if you need this feature, help is welcome.

@prayagupa
Copy link

prayagupa commented Jun 15, 2021

As tomix26 mentioned you can simply do start and stop manually inside beforeAll and afterAll: https://www.scalatest.org/scaladoc/1.0/org/scalatest/BeforeAndAfterAll.html#beforeAll%28Map%5BString%2CAny%5D%29,

Example: (Note: I did not run it)

import org.scalatest.{FunSuite, Matchers}

class MyUnitTest extends FunSuite with Matchers {

    private val DB_PORT: Int = 7777
    private var pg: EmbeddedPostgres = null
       
    def beforeAll = {
        pg = EmbeddedPostgres.builder()
                .setPort(DB_PORT)
                .setDataDirectory(Files.createDirectories(Path.of("/tmp").resolve("my_unit_tests").resolve("data")))
                .start()
    }

    test("should return data") {
            val conn = DriverManager.getConnection(String.format("jdbc:postgresql://localhost:%s/postgres?user=postgres&password=", DB_PORT))
            val stmt = conn.createStatement()
            val rs = stmt.executeQuery("SELECT NOW()")
            println("---------------------------------------")
            if (rs.next()) {
                println(rs.getString(1))
            }
    }

    def afterAll = {
      pg.close()
    }
}

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

No branches or pull requests

4 participants