Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Junit测试不';使用Google应用程序引擎保存实体_Java_Google App Engine_Jpa_Junit - Fatal编程技术网

Java Junit测试不';使用Google应用程序引擎保存实体

Java Junit测试不';使用Google应用程序引擎保存实体,java,google-app-engine,jpa,junit,Java,Google App Engine,Jpa,Junit,我正在使用GAE开发一个项目。我正在用Junit编写一个集成测试,它不保存实体。我已经在classpath中包含了JAR,并在这里复制了实体类、测试类和persistence.xml文件 Persistence.xml <?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://w

我正在使用GAE开发一个项目。我正在用Junit编写一个集成测试,它不保存实体。我已经在classpath中包含了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>
utetest.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;
    }
}
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);
    }
}
package it.bfm.test;
导入it.bfm.business.uteImpl;
导入it.bfm.business.uteInterface;
导入it.bfm.entity.utete;
导入java.util.List;
导入org.junit.After;
导入org.junit.Assert;
导入org.junit.Before;
导入org.junit.BeforeClass;
导入org.junit.Test;
导入junit.framework.TestCase;
导入com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
导入com.google.appengine.tools.development.testing.LocalServiceTestHelper;
公共类UtenteTest扩展了TestCase{
私有最终静态LocalServiceTestHelper帮助程序=
新的LocalServiceTestHelper(新的LocalDatastoreServiceTestConfig());
私有静态UteneInterface utImpl=新的UteneImpl();
@课前
公共静态void setuptenti(){
utImpl.creaUtente(“Utene1”、“Test1”、“Test1”、“Test1”)test1@test.it");
utImpl.creaUtente(“Utene2”、“Test2”、“Test2”、“Test2”test2@test.it");
utImpl.creaUtente(“Utenete3”、“Test3”、“Test3”、“Test3”)test3@test.it");
}
@以前
公共作废设置(){
helper.setUp();
}
@之后
公共无效拆卸(){
helper.tearDown();
}
@试验
公共无效testCreaUtente(){
utImpl.creaUtente(“Utene4”、“Test4”、“Test4”、“Test4”)test4@test.it");
}
@试验
public void testListaUtenti(){
List-uti=null;
utinti=utImpl.listaUtenti();
Assert.assertEquals(4,utenti.size());
}
@试验
public void testcercautebyemail(){
中尉;
字符串emailTest=”test1@test.it";
字符串nomeTest=“ute1”;
字符串cognotest=“Test1”;
utente=utImpl.cercauteByEmail(emailTest);
Assert.assertEquals(utete.getNome(),nomeTest);
Assert.assertEquals(utete.getCognome(),cognomeTest);
}
@试验
公共void testLogin(){
中尉;
字符串usernameTest=“test1”;
字符串passTest=“test1”;
字符串nomeTest=“ute1”;
字符串cognotest=“Test1”;
utinte=utImpl.login(usernameTest,passTest);
Assert.assertEquals(utete.getNome(),nomeTest);
Assert.assertEquals(utete.getCognome(),cognomeTest);
}
}
问题是setuptenti和testCreaUtente方法不能持久化实体。
test testListaUtenti失败,因为Utenti数字应为4,但为0。

每个
@test
注释的方法都会在新创建的类实例上调用。所以基本上你不能在方法之间坚持。您应该将此代码放在一个方法中。

持久化代码没有显示,因此人们必须猜测它为什么没有持久化。当你这么做的时候,日志上写的是什么?谢谢你,彼得。我还有一个问题:异常是“java.lang.AssertionError:expected:but was:”。Junit是否在每次执行之前执行带有@BeforeClass注释的方法?
@BeforeClass
必须用于静态方法,该方法在类中的所有测试之前仅运行一次: