JUnit测试Android空指针异常

huangapple go评论101阅读模式
英文:

JUnit Testing Android NullPointerException

问题

我的项目正在使用Firebase实时数据库作为其数据库存储。我使用MVC架构,具有存储库和服务层。我试图对服务层中的一个方法进行单元测试,该方法将新用户添加到数据库存储库中。

在下面的代码行上,我遇到了空指针异常,但我不知道为什么。有任何想法吗?

User testUser = svc.SaveUserProfile(u);

这是我的单元测试代码:

public class RegistrationActivityTest {

UserService svc;

@Test
public void testAddUserToDb_WhenNone_ShouldSetAllProperties(){
    //act
    User u = new User("1", "John", "john@email.com", "Teacher");
    User testUser = svc.SaveUserProfile(u);

    //assert - that testUser is not null
    assertNotNull(testUser);

    //now assert that the user properties were set correctly
    assertEquals("1", testUser.getUserId());
    assertEquals("John", testUser.getName());
    assertEquals("john@email.com", testUser.getEmail());
    assertEquals("Teacher", testUser.getAccount());
}

这是我的服务层和接口代码:

public class UserService implements IUserService {

//instance of DbContext for firebase handling
private DbContext dbContext;

public UserService(Context context){
    super();
    dbContext = new DbContext(context);
}

@Override
public User SaveUserProfile(User u) {
    User user = new User();
    user.setUserId(u.getUserId());
    user.setName(u.getName());
    user.setEmail(u.getEmail());
    user.setAccount(u.getAccount());

    dbContext.AddUserAccount(user);

    return user;
}

接口:

public interface IUserService {

User SaveUserProfile(User u);
 }
英文:

My project is using a Firebase Realtime Database as its DB storage. I am using MVC architecture with a repository and service layer. I'm trying to unit test a method in the service layer that adds a new user to the database repository.

I'm getting a null pointer exception on the line below and I don't know why. Any ideas?

User testUser = svc.SaveUserProfile(u);

Here is my unit test code:

public class RegistrationActivityTest {

UserService svc;

@Test
public void testAddUserToDb_WhenNone_ShouldSetAllProperties(){
    //act
    User u = new User("1", "John", "john@email.com", "Teacher");
    User testUser = svc.SaveUserProfile(u);

    //assert - that testUser is not null
    assertNotNull(testUser);

    //now assert that the user properties were set correctly
    assertEquals("1", testUser.getUserId());
    assertEquals("John", testUser.getName());
    assertEquals("john@email.com", testUser.getEmail());
    assertEquals("Teacher", testUser.getAccount());
}

Here is my service layer and interface code:

public class UserService implements IUserService {

//instance of DbContext for firebase handling
private DbContext dbContext;

public UserService(Context context){
    super();
    dbContext = new DbContext(context);
}

@Override
public User SaveUserProfile(User u) {
    User user = new User();
    user.setUserId(u.getUserId());
    user.setName(u.getName());
    user.setEmail(u.getEmail());
    user.setAccount(u.getAccount());

    dbContext.AddUserAccount(user);

    return user;
}

Interface:

public interface IUserService {

User SaveUserProfile(User u);
 }

答案1

得分: 1

你的 svc 对象尚未创建。在使用之前,请先创建它:

UserService svc = new UserService(context);
英文:

Your svc is object not created. Create it before using:

UserService svc = new UserService(context)

huangapple
  • 本文由 发表于 2020年8月18日 23:28:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63471861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定