zapodot / embedded-db-junit Goto Github PK
View Code? Open in Web Editor NEWJUnit Rule for providing an embedded in-memory database for your tests
Home Page: https://embedded-db-junit.zapodot.org
License: Other
JUnit Rule for providing an embedded in-memory database for your tests
Home Page: https://embedded-db-junit.zapodot.org
License: Other
Starting from v. 1.1 all releases will target Java 8
Please install our new product, Sonatype Lift with advanced features
Vulnerabilities
DepShield reports that this application's usage of com.h2database:h2:1.4.200 results in the following vulnerability(s):
Occurrences
com.h2database:h2:1.4.200 is a transitive dependency introduced by the following direct dependency(s):
• org.zapodot:embedded-db-core:2.0.3-SNAPSHOT
└─ com.h2database:h2:1.4.200
• org.zapodot:embedded-db-junit:2.0.3-SNAPSHOT
└─ com.h2database:h2:1.4.200
• org.zapodot:embedded-db-core:2.0.3-SNAPSHOT
└─ com.h2database:h2:1.4.200
This is an automated GitHub Issue created by Sonatype DepShield. Details on managing GitHub Apps, including DepShield, are available for personal and organization accounts. Please submit questions or feedback about DepShield to the Sonatype DepShield Community.
I understood that InitializationPlugin.getConnection()
returned Connection that
couldn't be closed for the duration of the test.
But I noticed that when implementing InitializationPlugin.connectionMade
, the Connection
that was passed to it could be closed.
Is that on purpose? Could it be the reason behind #7?
Vulnerabilities
DepShield reports that this application's usage of junit:junit:4.13 results in the following vulnerability(s):
Occurrences
junit:junit:4.13 is a transitive dependency introduced by the following direct dependency(s):
• org.zapodot:embedded-db-junit:2.0.1-SNAPSHOT
└─ junit:junit:4.13
This is an automated GitHub Issue created by Sonatype DepShield. Details on managing GitHub Apps, including DepShield, are available for personal and organization accounts. Please submit questions or feedback about DepShield to the Sonatype DepShield Community.
Vulnerabilities
DepShield reports that this application's usage of junit:junit:4.13.1 results in the following vulnerability(s):
Occurrences
junit:junit:4.13.1 is a transitive dependency introduced by the following direct dependency(s):
• org.zapodot:embedded-db-junit:2.0.1-SNAPSHOT
└─ junit:junit:4.13.1
This is an automated GitHub Issue created by Sonatype DepShield. Details on managing GitHub Apps, including DepShield, are available for personal and organization accounts. Please submit questions or feedback about DepShield to the Sonatype DepShield Community.
TestIssue.java
package test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.zapodot.junit.db.EmbeddedDatabaseExtension;
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
import org.zapodot.junit.db.common.CompatibilityMode;
import org.zapodot.junit.db.common.Engine;
@EmbeddedDatabaseTest(
engine = Engine.H2,
compatibilityMode = CompatibilityMode.MySQL,
initialSqls = "CREATE TABLE Customer(id INTEGER PRIMARY KEY, name VARCHAR(512)); "
+ "INSERT INTO CUSTOMER(id, name) VALUES (1, 'John Doe')"
)
@ExtendWith(EmbeddedDatabaseExtension.class)
public class TestIssue {
@EmbeddedDatabase
public Connection connection;
@Test
public void testUsingConnection() throws SQLException {
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery("SELECT * from CUSTOMER")) {
assertTrue(resultSet.next());
}
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.zapodot</groupId>
<artifactId>embedded-db-junit-jupiter</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.symphonia</groupId>
<artifactId>lambda-logging</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
</project>
... clean verify
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ test ---
[INFO] Deleting C:\WS\Test\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\WS\Test\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\WS\Test\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\WS\Test\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test ---
[INFO] Surefire report directory: C:\WS\Test\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running test.TestIssue
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec <<< FAILURE!
test.TestIssue.testUsingConnection() Time elapsed: 0.006 sec <<< FAILURE!
java.lang.NullPointerException
at test.TestIssue.testUsingConnection(TestIssue.java:31)
Results :
Failed tests: test.TestIssue.testUsingConnection()
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.217 s
[INFO] Finished at: 2020-04-19T18:28:29+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project test: There are test failures.
[ERROR]
[ERROR] Please refer to C:\WS\Test\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
I can execute it successfull with IntelliJ Ide but if I run it with maven (clean verify) the connection is null.
I tried also without @ExtendWith but same result
When I give #testUsingConnection the Connection as Parameter maven does not execute the test.
The version of byte-buddy library that is currently used is not compatible with byte-buddy 0.6 + which is used by Mockito 2 and other libs.
embedded-db-junit should be able to use the same version as Mockito to avoid problems in project depending on both.
I'm trying to use Weld to get the DataSource resource injected in my bean, utilizing EmbeddedDatabaseRule. It should look something like this (might be typos as I'm trying to strip out unrelated things):
private final EmbeddedDatabaseRule smsProDbRule = EmbeddedDatabaseRule.builder()
.withInitialSqlFromResource("classpath:create_tables.sql").build();
private final WeldInitiator weld = WeldInitiator
.from(WeldInitiator.createWeld().addBeanClass(MyDbBean.class))
.bindResource("java:/jdbc/AppDataSource", dbRule.getDataSource())
.inject(this)
.build();
@Rule
public TestRule chain = RuleChain.outerRule(weld).around(dbRule);
@Inject
MyDbBean bean;
The problem here though is that dbRule.getDataSource() will create a DataSource that has a null connection. Is it possible to somehow make this use case work?
Hi,
I'm not sure it is in your plan to use the latest Mockito (and thus byte-buddy, which is the reason I originally wanted to open this issue), but basically, embedded-db-junit can't be used with Mockito 2.x because of API changes in byte-buddy since 1.x..
Add plugin for Flyway to make it easier to test database migrations implemented in Flyway
Vulnerabilities
DepShield reports that this application's usage of com.h2database:h2:1.4.197 results in the following vulnerability(s):
Occurrences
com.h2database:h2:1.4.197 is a transitive dependency introduced by the following direct dependency(s):
• org.zapodot:embedded-db-junit:1.1.2-SNAPSHOT
└─ com.h2database:h2:1.4.197
• org.zapodot:embedded-db-junit:1.1.2-SNAPSHOT
└─ com.h2database:h2:1.4.197
This is an automated GitHub Issue created by Sonatype DepShield. Details on managing GitHub Apps, including DepShield, are available for personal and organization accounts. Please submit questions or feedback about DepShield to the Sonatype DepShield Community.
I'd like to upgrade the version of the Liquibase dependency to 4.4.0. I'd like to do this because it seems it's necessary to upgrade Liquibase if I want to use the newest version of the MySQL connector mysql:mysql-connector-java:8.0.25
.
I've prepared a PR with an upgrade: #227
Sorry, this is maybe just a question and not a bug, but I simply can't make embedded-db-junit with dropwizard database subsystem.
Basically, in my test I have the following:
public class UserSessionTest {
public static final User ADMIN = new User("admin", "Administrator", null);
public static class App extends CockpitApplication<CockpitConfiguration> {
// only needed because of generics
}
@ClassRule
public static final EmbeddedDatabaseRule dbRule = EmbeddedDatabaseRule.builder()
.initializedByPlugin(LiquibaseInitializer.builder().withChangelogResource("migrations.xml").build())
.initializedByPlugin(new InitializationPlugin() {
@Override
public void connectionMade(@Nullable String name, @Nullable Connection connection) {
try (DSLContext using = DSL.using(connection, SQLDialect.H2)) {
using.insertInto(Tables.USERS).set(new UsersRecord(ADMIN.username,
new BCryptPasswordEncoder().encode(ADMIN.username), ADMIN.name, ADMIN.lastWorkspace))
.execute();
}
}
}).build();
@Rule
public final DropwizardAppRule<CockpitConfiguration> appRule = new DropwizardAppRule<>(App.class,
ResourceHelpers.resourceFilePath("user-session-tests.yml"),
ConfigOverride.config("database.url", () -> dbRule.getConnectionJdbcUrl()));
....
}
And while the initialisation of the database with the two plugins works nicely, the SQL query made from dropwizard (I am using dropwizard-jooq with dropwizard-db, which setups a JDBC pool) returns no row.
For example, I even tried putting the following in my dropwizard Application.run()
method (the configuration, I verified, is correctly referring to the embedded database JDBC URL):
System.out.println(DSL.using(jooqConf).selectFrom(Tables.USERS).fetch());
And it says there is zero rows, while if I put the same thing in the test method itself, then it shows me the rows inserted by the plugin…
Either I am doing something wrong, or there is maybe a problem with the connections or maybe a transaction not committed or I don't know what… The only specific thing I can think of is that everything executed directly from the test class itself is done with the connections built by embedded-db-junit and not built directly from the JDBC URL string…
Hope it's ok to ask that here :)
As the extension API has changed a lot between JUnit 4 and JUnit 5 we need to reimplent the functionality using the JUnit 5 extension API
The project could not be analyzed because of build errors. Please review the error messages here. Another build will be scheduled when a change to a manifest file* occurs. If the build is successful this issue will be closed, otherwise the error message will be updated.
This is an automated GitHub Issue created by Sonatype DepShield. GitHub Apps, including DepShield, can be managed from the Developer settings of the repository administrators.
* Supported manifest files are: pom.xml, package.json, package-lock.json, npm-shrinkwrap.json, Cargo.lock, Cargo.toml, main.rs, lib.rs, build.gradle, build.gradle.kts, settings.gradle, settings.gradle.kts, gradle.properties, gradle-wrapper.properties, go.mod, go.sum
When using an EmbeddedDatabaseRule
as a @Rule,
I would expect to have a fresh empty database for each test of my test class, but apparently, it is the same database used in all of the tests.
I guess the problem is that h2 does keep the database in memory even if you close the connection.
Vulnerabilities
DepShield reports that this application's usage of org.yaml:snakeyaml:1.25 results in the following vulnerability(s):
This is an automated GitHub Issue created by Sonatype DepShield. Details on managing GitHub Apps, including DepShield, are available for personal and organization accounts. Please submit questions or feedback about DepShield to the Sonatype DepShield Community.
The CompatibilityMode
enum has until now been a public inner enum in the EmbeddedDatabaseRule
class. As it is used as part of the external interface it should be moved to the upper level as part of package org.zapodot.junit.db
.
When the SQL used to seed the database has problems, the errors are swallowed and it is impossible to debug what is wrong.
The project could not be analyzed because of maven build errors. Please review the error messages here. Another build will be scheduled within 24 hours. If the build is successful this issue will be closed, otherwise the error message will be updated.
This is an automated GitHub Issue created by Sonatype DepShield. GitHub Apps, including DepShield, can be managed from the Developer settings of the repository administrators.
Please install our new product, Sonatype Lift with advanced features
Hello. First of all, thank you for your awesome plugin, it's of great help when it comes to DAO layer integration testing (as you described in the use cases).
However a shortcoming of the H2 database system is its limited support for RDBMS specific syntaxes like MySQL's INSERT ... ON DUPLICATE KEY UPDATE
, so it is not possible to use these useful features in applications if H2 is used to test it. Alternative in-memory databases such as HSQLDB are better off in that regard.
As such, it might be desirable to provide a way for developers to use a different DB driver to power the test rule. Do you think this would be feasible, or would this be out of scope for this project?
We have our sql split in several files - "create_tables.sql", "create_views.sql", "create_indexes.sql".
We cannot load all of them, since each call to .withInitialSqlFromResource()
replaces the previous one.
So don't overwrite the previous value of PROP_INIT_SQL
and, optionally, change the signature of .withInitialSqlFromResource
to
public Builder withInitialSqlFromResource(final String... resource)
The workaround is of course to set PROP_INIT_SQL
manually, but that is less readable
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.