gpt4 book ai didi

java - 使用实体管理器进行 Junit 测试

转载 作者:行者123 更新时间:2023-11-30 06:11:38 27 4
gpt4 key购买 nike

我是单元测试的初学者。我在 Netbeans 8.1 和 Maven 中构建了 Java Web 应用程序。我创建了这个类来尝试测试我的 PostgreSQl 数据库中的查询:

public class DatesUtil {

private Collection<Menusistema> listamenus = new LinkedList<>();

private Menusistema menu= new Menusistema();

private String nombreBuscar = null;

private static final EntityManager entityManager;
static {
entityManager = Persistence.createEntityManagerFactory("com.controlpersonal.pu").
createEntityManager();
}

public static EntityManager getEntityManager() {
return entityManager;
};

public DatesUtil() {

}

@BeforeClass
public static void setUpClass() {


}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void hello() {
buscarTodo();

}


public void buscarTodo(){
findAll(Menusistema.class);
}



public <T> List<T> findAll(Class<T> clazz) {
return (List<T>) getEntityManager().createQuery("SELECT p FROM " + clazz.getSimpleName() + " p", clazz).getResultList();
}


}

当我右键单击 netbeans 并选择:测试文件时,出现下一个错误:

[EL Info]: 2018-04-30 20:30:57.78--ServerSession(252553541)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd [EL Severe]: ejb: 2018-04-30 20:30:57.836--ServerSession(252553541)--Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException Exception Description: Cannot acquire data source [jdbc/controlpersonal]. Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

我的 persistence.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.controlpersonal.pu" transaction-type="JTA">
<jta-data-source>jdbc/controlpersonal</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>

我真的很想测试一下这是否可行。提前致谢!

最佳答案

我刚刚创建了替代的 persiste .xml:

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.controlpersonal.pu.test" transaction-type="RESOURCE_LOCAL">
<!--<jta-data-source>jdbc/controlpersonal</jta-data-source>-->
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.your.class1</class>
<class>com.your.class2</class>
<properties>
<property name="eclipselink.target-database" value="PostgreSQL" />
<property name="eclipselink.logging.level" value="INFO" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/controlpersonal" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="xxx" />
</properties>
</persistence-unit> </persistence>

下:src/test/resources/META-INF/persistence.xml

关于java - 使用实体管理器进行 Junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50110708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com