【发布时间】:2019-08-31 15:58:10
【问题描述】:
我有 java web 应用程序,我想为服务层编写集成测试。我决定使用testcontainers,所以在测试中我想调用服务,它将与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,然后将该 EMF 中的 EM 填充到测试服务中。
感谢您的帮助或提示。
【问题讨论】:
-
你能尝试做这样的事情吗?我相信即使通过测试容器,您也可以通过 createEMFactory 方法访问 EMF。私有静态 EntityManagerFactory 电动势; @Before public static void setup() { log.debug("创建实体管理器工厂"); emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT); } in.relation.to/2016/01/14/hibernate-jpa-test-case-template
标签: java integration-testing entitymanager testcontainers