我是单元测试的初学者。我在Netbeans 8.1和Maven中构建了一个Java Web Application。我创建了这个类来尝试在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并选择:Test File:
时,我收到了下一个错误[EL Info]:2018-04-30 20:30:57.78 - ServerSession(252553541) - EclipseLink,版本:Eclipse 持久性服务 - 2.5.2.v20140319-9ad6abd [EL Severe]:ejb: 2018-04-30 20:30:57.836 - ServerSession(252553541) - 例外 [EclipseLink-7060](Eclipse持久性服务 - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.ValidationException异常 说明:无法获取数据源[jdbc / controlpersonal]。 内部异常:javax.naming.NoInitialContextException:需要 在环境或系统属性中指定类名,或作为applet指定 参数,或在应用程序资源文件中: 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>
我真的想测试这是否可行。提前谢谢!
答案 0 :(得分:1)
我刚创建了替代persistente .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