我正在尝试编写并针对GAE JPA服务方法运行JUnit测试用例。我的GAE JPA服务方法如下所示:
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.wisegroup.dto.UserDto;
import com.wisegroup.entity.User;
import com.wisegroup.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public boolean createUser(UserDto userDto) {
EntityManager em = EMF.get().createEntityManager();
try {
User newUser = new User();
newUser.setUsername(userDto.getUsername());
newUser.setPassword(userDto.getPassword());
em.persist(newUser);
em.close();
return true;
} catch(Exception ex){
ex.printStackTrace();
return false;
}
}
@Override
public boolean loginUser(UserDto user) {
EntityManager em = EMF.get().createEntityManager();
try{
Query q = em.createQuery("select from User u where u.username =:username and u.password =:password");
q.setParameter("username",user.getUsername());
q.setParameter("password", user.getPassword());
if(q.getResultList() !=null){
return q.getResultList().size()>0;
}
else{
return false;
}
}
catch(Exception ex){
ex.printStackTrace();
return false;
}
}
}
JUnit测试代码如下:
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.wisegroup.dto.UserDto;
import com.wisegroup.service.Impl.UserServiceImpl;
public class UserServiceTest {
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
private static UserServiceImpl userService;
@Before
public void createUserService(){
helper.setUp();
userService = new UserServiceImpl();
}
@Test
public void testloginUser(){
UserDto userDto =new UserDto();
userDto.setUsername("ramesh");
userDto.setPassword("password");
Assert.assertEquals("<< Logging in the User >>",true,userService.loginUser(userDto));
}
@Test
public void testcreateUser(){
UserDto userDto =new UserDto();
userDto.setUsername("ramesh1");
userDto.setPassword("password");
Assert.assertEquals("<< Creating the User >>",true,userService.createUser(userDto));
}
@After
public void tearDownService(){
helper.tearDown();
userService = null;
}
}
但是当我尝试运行JUnit测试testcreateUser()时,它会通过Assertion测试,但是当我使用以下url http://localhost:8888/_ah/admin
查看本地数据存储区时,不会创建任何用户。没有创建实体。有人可以帮帮我吗?。