空指针异常在测试服务和DAO类的JUnit时发生。

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

NullPointerException when Testing Service and DAO class JUnit

问题

我正在使用JUnit测试DAO类但是我得到了一个空指针异常我不确定为什么因为我已经初始化了服务类以下是测试类

public class RegisterTest {

    private UserDaoImpl userservice = new UserDaoImpl();

    @Mock
    JdbcTemplate jdbcTemplate;

    User user;

    @Before
    public void setUp() {
        user = new User();
    }

    @Test
    public void testSetAddress() {
        user.setAddress("A");
        assertEquals(user.getAddress(), "A");
    }

    @Test
    public void testSetEmail() {
        user.setEmail("B");
        assertEquals(user.getEmail(), "B");
    }

    @Test
    public void testSetFirstname() {
        user.setFirstname("C");
        assertEquals(user.getFirstname(), "C");
    }

    @Test
    public void testSetLastname() {
        user.setLastname("D");
        assertEquals(user.getLastname(), "D");
    }

    @Test
    public void testSetPassword() {
        user.setPassword("E");
        assertEquals(user.getPassword(), "E");
    }

    @Test
    public void testSetUsername() {
        user.setUsername("F");
        assertEquals(user.getUsername(), "F");
    }

    @Test
    public void testRegister() {
        userservice.register(user);
        String username = user.getUsername();
        assertEquals(userservice.findByUsername(username).size(), 1);
    }
}

以下是UserDaoImpl

public class UserDaoImpl implements UserDao {
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    DataSource datasource;
    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<User> findByUsername(String username) {

        String sql = "select * from users where username='" + username + "'";
        List<User> users = jdbcTemplate.query(sql, new UserMapper());
        return users;
    }

    public int register(User user) {
        // If username is unique
        String uniqueusername = "select * from users where username='" + user.getUsername() + "'";
        List<User> users = jdbcTemplate.query(uniqueusername, new UserMapper());
        if (users.size() == 0) {
            // encode password
            String encryptedPassword = passwordEncoder.encode(user.getPassword());
            // Updating database with new user
            String sql = "insert into users values(?,?,?,?,?,?)";
            return jdbcTemplate.update(sql, new Object[]{
                    user.getUsername(),
                    encryptedPassword,
                    user.getFirstname(),
                    user.getLastname(),
                    user.getEmail(),
                    user.getAddress()});
        } else {
            return 0;
        }
    }
}

如何在测试类中注入该类我猜空指针异常的原因是因为DAO类没有在测试类中被正确注入
英文:

I am testing a DAO class using JUnit and I am getting a nullpointerexception which I am not sure why as I am initiating the service class. The following is the test class:

public class RegisterTest {
private UserDaoImpl userservice = new UserDaoImpl();
@Mock
JdbcTemplate jdbcTemplate;
User user;
@Before
public void setUp() {
user = new User();
}
@Test
public void testSetAddress() {
user.setAddress(&quot;A&quot;);
assertEquals(user.getAddress(), &quot;A&quot;);
}
@Test 
public void testSetEmail() {
user.setEmail(&quot;B&quot;);
assertEquals(user.getEmail(), &quot;B&quot;);
}
@Test 
public void testSetFirstname() {
user.setFirstname(&quot;C&quot;);
assertEquals(user.getFirstname(), &quot;C&quot;);
}
@Test 
public void testSetLastname() {
user.setLastname(&quot;D&quot;);
assertEquals(user.getLastname(), &quot;D&quot;);
}
@Test 
public void testSetPassword() {
user.setPassword(&quot;E&quot;);
assertEquals(user.getPassword(), &quot;E&quot;);
}
@Test 
public void testSetUsername() {
user.setUsername(&quot;F&quot;);
assertEquals(user.getUsername(), &quot;F&quot;);
}
@Test
public void testRegister() {
userservice.register(user);
String username = user.getUsername();
assertEquals(userservice.findByUsername(username), 1);
}
}

The following is the UserDaoImpl

public class UserDaoImpl implements UserDao {
@Autowired
PasswordEncoder passwordEncoder;
@Autowired
DataSource datasource;
@Autowired
JdbcTemplate jdbcTemplate;
public List&lt;User&gt; findByUsername(String username) {
String sql = &quot;select * from users where username=&#39;&quot; + username + 
&quot;&#39;&quot;;
List&lt;User&gt; users = jdbcTemplate.query(sql, new UserMapper());
return users;
}
public int register(User user) {
// If username is unique
String uniqueusername = &quot;select * from users where username=&#39;&quot; + 
user.getUsername() + &quot;&#39;&quot;;
List&lt;User&gt; users = jdbcTemplate.query(uniqueusername, new 
UserMapper());
if(users.size() == 0) {
// encode password
String encryptedPassword = 
passwordEncoder.encode(user.getPassword());
// Updating database with new user
String sql = &quot;insert into users values(?,?,?,?,?,?)&quot;;
return jdbcTemplate.update(sql, new Object[] { 
user.getUsername(), 
encryptedPassword,
user.getFirstname(),
user.getLastname(), 
user.getEmail(), 
user.getAddress() });
}
else {
return 0;
}
}

How can I inject the class in the test class? I guess the reason why the nullpointerxeception is because the dao class is not being injected properly in the test class

答案1

得分: 1

你应该使用适当的运行器来运行你的测试。

@RunWith(MockitoJunitRunner.class)
public class RegisterTest {

然后你需要将你的模拟对象注入到DAO中。

@InjectMocks
private UserDaoImpl userservice = new UserDaoImpl();
英文:

You should run your test with adequate runner

RunWith(MockitoJunitRunner.class)
public class RegisterTest {

Then you need to inject your mock inside the DAO

@InjectMocks
private UserDaoImpl userservice = new UserDaoImpl();

huangapple
  • 本文由 发表于 2020年5月5日 19:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612493.html
匿名

发表评论

匿名网友

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

确定