У меня есть веб-приложение Java, и я хотел бы написать интеграционные тесты для сервисного уровня. Я решил использовать тестконтейнеры, поэтому в тесте я хотел бы вызвать сервис, который будет работать с базой данных в контейнере docker.
Мой тестовый класс выглядит как пример ниже.
@Testcontainers
class ITPlayerServiceImpl {
@Container
private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER =
new PostgreSQLContainer()
.withDatabaseName("dbName")
.withUsername("dbUserName")
.withPassword("dbPassword");
}
Проверенный сервис.
@Stateless
public class PlayerServiceImpl implements PlayerService {
@PersistenceContext(unitName = "persistence_unit_name")
private EntityManager entityManager;
//Methods
Мне нужно создать EMF, подключенный к БД в контейнере, а затем заполнить EM из этого EMF в проверенный сервис.
Спасибо за помощь или подсказки.
После некоторого исследования и тестирования я заканчиваю решением ниже.
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Testcontainers
class ITApplicationUserService {
@Container
private static final PostgreSQLContainer POSTGRE_SQL_CONTAINER = new PostgreSQLContainer()
.withDatabaseName("someDatabase")
.withUsername("someUsername")
.withPassword("somePassword");
// EMF for integration tests
private static EntityManagerFactory emf;
// EM for tested service
private static EntityManager entityManager;
// Tested service
private static ApplicationUserServiceImpl applicationUserService = new ApplicationUserServiceImpl();
// Object used for testing
private static ApplicationUser testingApplicationUser;
@BeforeAll
static void init() {
// Properties for our EMF, which will make EM connected to POSTGRE_SQL_CONTAINER
Map<String,String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.url",POSTGRE_SQL_CONTAINER.getJdbcUrl());
properties.put("javax.persistence.jdbc.user",POSTGRE_SQL_CONTAINER.getUsername());
properties.put("javax.persistence.jdbc.password",POSTGRE_SQL_CONTAINER.getPassword());
properties.put("javax.persistence.jdbc.driver",POSTGRE_SQL_CONTAINER.getDriverClassName());
properties.put("eclipselink.logging.level","FINE");
// We need create fresh empty schema in POSTGRE_SQL_CONTAINER
properties.put("javax.persistence.schema-generation.database.action","create");
// Creation of EMF
emf = Persistence.createEntityManagerFactory("integrationTesting",properties);
// Player for testing
testingApplicationUser = new ApplicationUser();
testingApplicationUser.setLogin("loginName");
testingApplicationUser.setEmail("[email protected]");
testingApplicationUser.setPassword("123456");
}
@Test
@Order(1)
void saveNewApplicationUserTest() {
assertTrue(testingApplicationUser.getId()==null);
ApplicationUser applicationUser = applicationUserService.saveApplicationUser(testingApplicationUser);
assertTrue(applicationUser.getId()!=null);
}
@Test
@Order(2)
void getApplicationUsers() {
assertTrue(applicationUserService.getAllApplicationUsers().size()==1);
}
@BeforeEach
private void startTransaction() throws IllegalAccessException, NoSuchFieldException {
entityManager = emf.createEntityManager();
// We will declare field of EM in tested service
// EM field in tested class is not public and should not be. We will use reflection for population of EM.
Field emField = applicationUserService.getClass().getDeclaredField("entityManager");
emField.setAccessible(true);
emField.set(applicationUserService,entityManager);
entityManager.getTransaction().begin();
}
@AfterEach
private void commitTransaction() {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().commit();
entityManager.close();
}
}
}
Привет, приятель, у меня та же проблема, и я продолжаю получать javax.persistence.PersistenceException: нет поставщика Persistence для EntityManager с именем, но он находится под моим /test/resources/META-INF/persistance.xml
Можете ли вы попробовать сделать что-то вроде этого. Я считаю, что даже через тестовые контейнеры вы сможете получить доступ к EMF с помощью метода createEMFactory. частный статический EntityManagerFactory emf; @Before public static void setup () { log.debug («создание фабрики диспетчера сущностей»); emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT); } in.relation.to/2016/01/14/hibernate-jpa-test-case-template