繁体   English   中英

Junit测试不会持久保留Google App Engine实体

[英]Junit test doesn't persist entity with Google App Engine

我正在使用GAE开发一个项目。 我正在用Junit编写集成测试,该测试不会保存实体。 我在类路径中包含了JAR,并在此处复制了实体类,测试类和persistence.xml文件。

Persistence.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <persistence 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/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
        </properties>
    </persistence-unit>
</persistence>

Utente.java

package it.bfm.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import com.google.appengine.api.datastore.Key;

@Entity
public class Utente {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Key key;

    private String nome;
    private String cognome;
    private String username;
    private String password;
    private String email;

    public Utente(String nome, String cognome, String user, String password,
            String email){
        this.nome = nome;
        this.cognome = cognome;
        this.username = user;
        this.password = password;
        this.email = email;
    }

    public Key getKey(){
        return this.key;
    }

    public void setNome(String nome){
        this.nome = nome;
    }

    public String getNome(){
        return this.nome;
    }

    public void setCognome(String cognome){
        this.cognome = cognome;
    }

    public String getCognome(){
        return this.cognome;
    }

    public void setUser(String username){
        this.username = username;
    }

    public String getUsername(){
        return this.username;
    }

    public void setPassword(String password){
        this.password = password;
    }

    public String getPasswrd(){
        return this.password;
    }

    public void setEmail(String email){
        this.email = email;
    }

    public String getEmail(){
        return this.email;
    }
}

UtenteTest.java

package it.bfm.test;

import it.bfm.business.UtenteImpl;
import it.bfm.business.UtenteInterface;
import it.bfm.entity.Utente;

import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import junit.framework.TestCase;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class UtenteTest extends TestCase{

    private final static LocalServiceTestHelper helper =
            new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
    private static UtenteInterface utImpl = new UtenteImpl();

    @BeforeClass
    public static void setUpUtenti() {
        utImpl.creaUtente("Utente1", "Test1", "test1", "test1", "test1@test.it");
        utImpl.creaUtente("Utente2", "Test2", "test2", "test2", "test2@test.it");
        utImpl.creaUtente("Utente3", "Test3", "test3", "test3", "test3@test.it");
    }

    @Before
    public void setUp(){
        helper.setUp();
    }

    @After
    public void tearDown() {
        helper.tearDown();
    }

    @Test
    public void testCreaUtente() {
        utImpl.creaUtente("Utente4", "Test4", "test4", "test4", "test4@test.it");
    }

    @Test
    public void testListaUtenti() {
        List<Utente> utenti = null;
        utenti = utImpl.listaUtenti();
        Assert.assertEquals(4, utenti.size());
    }

    @Test
    public void testCercaUtenteByEmail() {
        Utente utente;
        String emailTest = "test1@test.it";
        String nomeTest = "Utente1";
        String cognomeTest = "Test1";

        utente = utImpl.cercaUtenteByEmail(emailTest);

        Assert.assertEquals(utente.getNome(), nomeTest);
        Assert.assertEquals(utente.getCognome(), cognomeTest);
    }

    @Test
    public void testLogin() {
        Utente utente;
        String usernameTest = "test1";
        String passTest = "test1";
        String nomeTest = "Utente1";
        String cognomeTest = "Test1";

        utente = utImpl.login(usernameTest, passTest);

        Assert.assertEquals(utente.getNome(), nomeTest);
        Assert.assertEquals(utente.getCognome(), cognomeTest);
    }
}

问题在于,setUpUtenti和testCreaUtente方法不会保留实体。 测试testListaUtenti失败,因为预期的Utenti编号为4但为0。

每个@Test注释方法都在新创建的class实例上调用。 因此,基本上您不能在方法之间坚持。 您应该将此代码放入一个方法中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM