-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using Java8 stream to find jdbc connection
refs #29
- Loading branch information
rmpestano
committed
Aug 28, 2016
1 parent
1319e2c
commit 46f9814
Showing
2 changed files
with
121 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...t5/src/test/java/com/github/dbunit/junit5/DBUnitJUnit5WithMethodConnectionHolderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.github.dbunit.junit5; | ||
|
||
import com.github.dbunit.junit5.model.User; | ||
import com.github.dbunit.rules.api.connection.ConnectionHolder; | ||
import com.github.dbunit.rules.api.dataset.DataSet; | ||
import com.github.dbunit.rules.api.dataset.ExpectedDataSet; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.List; | ||
|
||
import static com.github.dbunit.rules.util.EntityManagerProvider.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Created by pestano on 28/08/16. | ||
*/ | ||
@ExtendWith(DBUnitExtension.class) | ||
@RunWith(JUnitPlatform.class) | ||
public class DBUnitJUnit5WithMethodConnectionHolderTest { | ||
|
||
//DBUnitExtension will get connection by reflection so either declare a field or a method with ConncetionHolder as return typr | ||
private ConnectionHolder getConnection(){ | ||
return () -> instance("junit5-pu").connection(); | ||
} | ||
|
||
@Test | ||
@DataSet("users.yml") | ||
public void shouldListUsers() { | ||
List<User> users = em().createQuery("select u from User u").getResultList(); | ||
assertThat(users).isNotNull().isNotEmpty().hasSize(2); | ||
} | ||
|
||
@Test | ||
@DataSet(cleanBefore=true) //avoid conflict with other tests | ||
public void shouldInsertUser() { | ||
User user = new User(); | ||
user.setName("user"); | ||
user.setName("@rmpestano"); | ||
tx().begin(); | ||
em().persist(user); | ||
tx().commit(); | ||
User insertedUser = (User)em().createQuery("select u from User u where u.name = '@rmpestano'").getSingleResult(); | ||
assertThat(insertedUser).isNotNull(); | ||
assertThat(insertedUser.getId()).isNotNull(); | ||
} | ||
|
||
@Test | ||
@DataSet("users.yml") //no need for clean before because DBUnit uses CLEAN_INSERT seeding strategy which clears involved tables before seeding | ||
public void shouldUpdateUser() { | ||
User user = (User) em().createQuery("select u from User u where u.id = 1").getSingleResult(); | ||
assertThat(user).isNotNull(); | ||
assertThat(user.getName()).isEqualTo("@realpestano"); | ||
//tx().begin(); | ||
user.setName("@rmpestano"); | ||
em().merge(user); | ||
//tx().commit(); //no needed because of first level cache | ||
User updatedUser = getUser(1L); | ||
assertThat(updatedUser).isNotNull(); | ||
assertThat(updatedUser.getName()).isEqualTo("@rmpestano"); | ||
} | ||
|
||
@Test | ||
@DataSet(value = "users.yml", transactional = true) | ||
@ExpectedDataSet("expectedUser.yml") | ||
public void shouldDeleteUser() { | ||
User user = (User) em().createQuery("select u from User u where u.id = 1").getSingleResult(); | ||
assertThat(user).isNotNull(); | ||
assertThat(user.getName()).isEqualTo("@realpestano"); | ||
em().remove(user); | ||
} | ||
|
||
|
||
public User getUser(Long id){ | ||
return (User) em().createQuery("select u from User u where u.id = :id"). | ||
setParameter("id", id).getSingleResult(); | ||
} | ||
} |