I am beginner in Unit Testing. I have a Java Web Application build in Netbeans 8.1 and Maven. I have created this class to try to test querys in my PostgreSQl database:
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();
}
}
I got the next error when I right click on netbeans and select: Test File:
[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
My persistence.xml is:
<?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>
I really would like to test if this is posible. Thanks in advance!
I just created alternative 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>
Under: src/test/resources/META-INF/persistence.xml