英文:
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("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), 1);
}
}
The following is the 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;
}
}
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论