Я пытаюсь создать тест JUnit для своего объекта DAO. JUnit запускается непосредственно из Netbeans. Я новичок в некоторых из этих технологий, и в результате у меня есть чертовски много времени, чтобы отследить, откуда берутся мои ошибки. Мой код и соответствующая ошибка, которую я вижу:
постоянство.xml
Мой DAO на тестировании:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistencepackage waylon.label.impl;
import com.google.inject.Inject;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import waylon.label.Label;
import waylon.label.LabelDAO;
/**
* {@inheritDoc}
*/
public class LabelDAOImpl implements LabelDAO {
private final EntityManager em;
@Inject
public LabelDAOImpl( EntityManager em ) {
this.em = em;
}
/**
* {@inheritDoc}
*/
@Override
public List<Label> getAllLabels() throws Exception {
TypedQuery<Label> typedQuery = em.createQuery(
"SELECT * from LABEL", Label.class );
return typedQuery.getResultList();
}
/**
* {@inheritDoc}
*/
@Override
public Label createLabel(String name) throws Exception {
em.getTransaction().begin();
Label label = new Label();
label.setName( name );
em.persist( label );
em.getTransaction().commit();
return label;
}
/**
* {@inheritDoc}
*/
@Override
public void removeLabel(String name) throws Exception {
Label label = getLabel( name );
removeLabel( label );
}
/**
* {@inheritDoc}
*/
@Override
public Label getLabel(String name) throws Exception {
Label label = em.find( Label.class, name );
return label;
}
/**
* {@inheritDoc}
*/
@Override
public void removeLabel(Label label) throws Exception {
if ( null != label ) {
em.getTransaction().begin();
em.remove( label );
em.getTransaction().commit();
}
}
}
0.xsd">
<persistence-unit name="WaylonPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>Waylon</jta-data-source>
<class>waylon.label.Label</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
Мой тест JUnit:
package waylon.label.impl;
import com.google.inject.Inject;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import waylon.label.Label;
import waylon.label.LabelDAO;
/**
* {@inheritDoc}
*/
public class LabelDAOImpl implements LabelDAO {
private final EntityManager em;
@Inject
public LabelDAOImpl( EntityManager em ) {
this.em = em;
}
/**
* {@inheritDoc}
*/
@Override
public List<Label> getAllLabels() throws Exception {
TypedQuery<Label> typedQuery = em.createQuery(
"SELECT * from LABEL", Label.class );
return typedQuery.getResultList();
}
/**
* {@inheritDoc}
*/
@Override
public Label createLabel(String name) throws Exception {
em.getTransaction().begin();
Label label = new Label();
label.setName( name );
em.persist( label );
em.getTransaction().commit();
return label;
}
/**
* {@inheritDoc}
*/
@Override
public void removeLabel(String name) throws Exception {
Label label = getLabel( name );
removeLabel( label );
}
/**
* {@inheritDoc}
*/
@Override
public Label getLabel(String name) throws Exception {
Label label = em.find( Label.class, name );
return label;
}
/**
* {@inheritDoc}
*/
@Override
public void removeLabel(Label label) throws Exception {
if ( null != label ) {
em.getTransaction().begin();
em.remove( label );
em.getTransaction().commit();
}
}
}
Любая помощь будет принята с благодарностью. Спасибо.
package waylon.label;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.persist.PersistService;
import com.google.inject.persist.jpa.JpaPersistModule;
import java.util.List;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import org.hamcrest.core.IsEqual;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import waylon.label.impl.LabelDAOModule;
/**
* A JUnit Test to test our database connection and JPA code.
* @author Benjamin Bays
*/
public class LabelDAOIJTest {
private LabelDAO objectInTest = null;
private static final String PUNIT = "WaylonPU";
private static Injector injector = null;
private static PersistService persistService = null;
@BeforeClass
public static void setUpClass() throws Exception {
injector = Guice.createInjector(
new JpaPersistModule(PUNIT),
new LabelDAOModule() );
persistService = injector.getInstance( PersistService.class );
persistService.start();
}
@AfterClass
public static void tearDownClass() throws Exception {
persistService.stop();
}
@Before
public void setUp() {
objectInTest = injector.getInstance( LabelDAO.class );
}
@Test
public void doEverything() throws Exception {
final String testName = "Test Label";
Label label1 = objectInTest.createLabel( testName );
assertThat( label1.getName(), IsEqual.equalTo( testName ) );
Label label2 = objectInTest.getLabel(testName);
assertThat( label2.getName(), IsEqual.equalTo( testName ) );
final String testName3 = "Test Label3";
Label label3 = objectInTest.createLabel( testName3 );
assertThat( label3.getName(), IsEqual.equalTo( testName3 ) );
List<Label> allLabels = objectInTest.getAllLabels();
assertThat( allLabels, hasItems( label1, label3 ) );
objectInTest.removeLabel( label1 );
objectInTest.removeLabel( testName3 );
List<Label> noLabels = objectInTest.getAllLabels();
assertThat( noLabels.size(), IsEqual.equalTo( 0 ) );
}
}
В этом нужно было долго разбираться. Я думаю, что общий урок здесь состоит в том, чтобы изучать только одну новую технологию за раз. Я предоставил серию шагов, которые я предпринял, чтобы запустить свои JUnits, но в целом это было несколько ошибок, которые были устранены RTFM.