Null pointer request when trying to test with Hibernate and Mockito
I'm trying to make a test for this method inside UserHibernateDao:
@Transactional
public User create(final String firstName, final String lastName, final String email,
final String password, final String phoneNumber, String role) {
final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE));
em.persist(user);
return user;
}
In order to test this method I need to autowire the variables in my test but I get a null pointer exception in request and entity manager as if the variables where never initialized.
TestConfig.java
@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence"})
@Configuration
@EnableTransactionManagement
public class TestConfig {
@Mock
@Autowired
private UserHibernateDao userHibernateDao;
@Bean
@Autowired
public UserHibernateDao getUserHibernateDao() {
return userHibernateDao;
}
@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setPackagesToScan("ar.edu.itba.paw.models");
factoryBean.setDataSource(dataSource());
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
final Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "none");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("format_sql", "true");
factoryBean.setJpaProperties(properties);
return factoryBean;
}
@Bean
@Autowired
public DataSource dataSource() {
final SimpleDriverDataSource ds = new SimpleDriverDataSource();
ds.setDriverClass(JDBCDriver.class);
ds.setUrl("jdbc:hsqldb:mem:paw");
ds.setUsername("ha");
ds.setPassword("");
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
}
UserJDBCDaoTest (ignore the JDBC name):
@ContextConfiguration(classes = TestConfig.class)
@Sql("classpath:userScript.sql")
@Transactional
public class UserJdbcDaoTest {
private static final long USERID = 1;
private static final long NONEXISTENTUSERID = -1;
private static final String FIRSTNAME = "TestFirstName";
private static final String NEWFIRSTNAME = "TestNewFirstName";
private static final String LASTNAME = "TestLastName";
private static final String EMAIL = "test1@mail.com";
private static final String PASSWORD = "TestPassword";
private static final String PHONENUMBER = "1522334455";
private static final String ROLE = "role";
private User user;
@Autowired
private UserHibernateDao userHibernateDao;
@Autowired
@Before
public void setUp() {
this.userHibernateDao = new UserHibernateDao();
}
@Test
public void testCreateUser() {
//JdbcTestUtils.deleteFromTables(jdbcTemplate, "users");
user = Mockito.mock(User.class);
when(user.getFirstName()).thenReturn(FIRSTNAME);
when(user.getLastName()).thenReturn(LASTNAME);
when(user.getEmail()).thenReturn(EMAIL);
when(user.getPassword()).thenReturn(PASSWORD);
when(user.getPhoneNumber()).thenReturn(PHONENUMBER);
when(user.getRole()).thenReturn(ROLE);
User u = userHibernateDao.create(FIRSTNAME, LASTNAME, EMAIL, PASSWORD, PHONENUMBER, ROLE);
Assert.assertNotNull(u);
Assert.assertEquals(u.getFirstName(), user.getFirstName());
Assert.assertEquals(u.getLastName(), user.getLastName());
Assert.assertEquals(EMAIL, user.getEmail());
Assert.assertEquals(PASSWORD, user.getPassword());
Assert.assertEquals(PHONENUMBER, user.getPhoneNumber());
Assert.assertEquals(ROLE, user.getRole());
//Assert.assertEquals(1, JdbcTestUtils.countRowsInTable(jdbcTemplate, "users"));
}
}
java hibernate testing mockito
add a comment |
I'm trying to make a test for this method inside UserHibernateDao:
@Transactional
public User create(final String firstName, final String lastName, final String email,
final String password, final String phoneNumber, String role) {
final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE));
em.persist(user);
return user;
}
In order to test this method I need to autowire the variables in my test but I get a null pointer exception in request and entity manager as if the variables where never initialized.
TestConfig.java
@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence"})
@Configuration
@EnableTransactionManagement
public class TestConfig {
@Mock
@Autowired
private UserHibernateDao userHibernateDao;
@Bean
@Autowired
public UserHibernateDao getUserHibernateDao() {
return userHibernateDao;
}
@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setPackagesToScan("ar.edu.itba.paw.models");
factoryBean.setDataSource(dataSource());
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
final Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "none");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("format_sql", "true");
factoryBean.setJpaProperties(properties);
return factoryBean;
}
@Bean
@Autowired
public DataSource dataSource() {
final SimpleDriverDataSource ds = new SimpleDriverDataSource();
ds.setDriverClass(JDBCDriver.class);
ds.setUrl("jdbc:hsqldb:mem:paw");
ds.setUsername("ha");
ds.setPassword("");
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
}
UserJDBCDaoTest (ignore the JDBC name):
@ContextConfiguration(classes = TestConfig.class)
@Sql("classpath:userScript.sql")
@Transactional
public class UserJdbcDaoTest {
private static final long USERID = 1;
private static final long NONEXISTENTUSERID = -1;
private static final String FIRSTNAME = "TestFirstName";
private static final String NEWFIRSTNAME = "TestNewFirstName";
private static final String LASTNAME = "TestLastName";
private static final String EMAIL = "test1@mail.com";
private static final String PASSWORD = "TestPassword";
private static final String PHONENUMBER = "1522334455";
private static final String ROLE = "role";
private User user;
@Autowired
private UserHibernateDao userHibernateDao;
@Autowired
@Before
public void setUp() {
this.userHibernateDao = new UserHibernateDao();
}
@Test
public void testCreateUser() {
//JdbcTestUtils.deleteFromTables(jdbcTemplate, "users");
user = Mockito.mock(User.class);
when(user.getFirstName()).thenReturn(FIRSTNAME);
when(user.getLastName()).thenReturn(LASTNAME);
when(user.getEmail()).thenReturn(EMAIL);
when(user.getPassword()).thenReturn(PASSWORD);
when(user.getPhoneNumber()).thenReturn(PHONENUMBER);
when(user.getRole()).thenReturn(ROLE);
User u = userHibernateDao.create(FIRSTNAME, LASTNAME, EMAIL, PASSWORD, PHONENUMBER, ROLE);
Assert.assertNotNull(u);
Assert.assertEquals(u.getFirstName(), user.getFirstName());
Assert.assertEquals(u.getLastName(), user.getLastName());
Assert.assertEquals(EMAIL, user.getEmail());
Assert.assertEquals(PASSWORD, user.getPassword());
Assert.assertEquals(PHONENUMBER, user.getPhoneNumber());
Assert.assertEquals(ROLE, user.getRole());
//Assert.assertEquals(1, JdbcTestUtils.countRowsInTable(jdbcTemplate, "users"));
}
}
java hibernate testing mockito
Which line is the null pointer exception on?
– David Lavender
Nov 14 '18 at 13:54
@DavidLavender at the line where it says:final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE))
– Stackoverflowed
Nov 14 '18 at 14:12
@DavidLavender since the request is null, it ends up throwing an exception.
– Stackoverflowed
Nov 14 '18 at 14:13
add a comment |
I'm trying to make a test for this method inside UserHibernateDao:
@Transactional
public User create(final String firstName, final String lastName, final String email,
final String password, final String phoneNumber, String role) {
final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE));
em.persist(user);
return user;
}
In order to test this method I need to autowire the variables in my test but I get a null pointer exception in request and entity manager as if the variables where never initialized.
TestConfig.java
@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence"})
@Configuration
@EnableTransactionManagement
public class TestConfig {
@Mock
@Autowired
private UserHibernateDao userHibernateDao;
@Bean
@Autowired
public UserHibernateDao getUserHibernateDao() {
return userHibernateDao;
}
@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setPackagesToScan("ar.edu.itba.paw.models");
factoryBean.setDataSource(dataSource());
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
final Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "none");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("format_sql", "true");
factoryBean.setJpaProperties(properties);
return factoryBean;
}
@Bean
@Autowired
public DataSource dataSource() {
final SimpleDriverDataSource ds = new SimpleDriverDataSource();
ds.setDriverClass(JDBCDriver.class);
ds.setUrl("jdbc:hsqldb:mem:paw");
ds.setUsername("ha");
ds.setPassword("");
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
}
UserJDBCDaoTest (ignore the JDBC name):
@ContextConfiguration(classes = TestConfig.class)
@Sql("classpath:userScript.sql")
@Transactional
public class UserJdbcDaoTest {
private static final long USERID = 1;
private static final long NONEXISTENTUSERID = -1;
private static final String FIRSTNAME = "TestFirstName";
private static final String NEWFIRSTNAME = "TestNewFirstName";
private static final String LASTNAME = "TestLastName";
private static final String EMAIL = "test1@mail.com";
private static final String PASSWORD = "TestPassword";
private static final String PHONENUMBER = "1522334455";
private static final String ROLE = "role";
private User user;
@Autowired
private UserHibernateDao userHibernateDao;
@Autowired
@Before
public void setUp() {
this.userHibernateDao = new UserHibernateDao();
}
@Test
public void testCreateUser() {
//JdbcTestUtils.deleteFromTables(jdbcTemplate, "users");
user = Mockito.mock(User.class);
when(user.getFirstName()).thenReturn(FIRSTNAME);
when(user.getLastName()).thenReturn(LASTNAME);
when(user.getEmail()).thenReturn(EMAIL);
when(user.getPassword()).thenReturn(PASSWORD);
when(user.getPhoneNumber()).thenReturn(PHONENUMBER);
when(user.getRole()).thenReturn(ROLE);
User u = userHibernateDao.create(FIRSTNAME, LASTNAME, EMAIL, PASSWORD, PHONENUMBER, ROLE);
Assert.assertNotNull(u);
Assert.assertEquals(u.getFirstName(), user.getFirstName());
Assert.assertEquals(u.getLastName(), user.getLastName());
Assert.assertEquals(EMAIL, user.getEmail());
Assert.assertEquals(PASSWORD, user.getPassword());
Assert.assertEquals(PHONENUMBER, user.getPhoneNumber());
Assert.assertEquals(ROLE, user.getRole());
//Assert.assertEquals(1, JdbcTestUtils.countRowsInTable(jdbcTemplate, "users"));
}
}
java hibernate testing mockito
I'm trying to make a test for this method inside UserHibernateDao:
@Transactional
public User create(final String firstName, final String lastName, final String email,
final String password, final String phoneNumber, String role) {
final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE));
em.persist(user);
return user;
}
In order to test this method I need to autowire the variables in my test but I get a null pointer exception in request and entity manager as if the variables where never initialized.
TestConfig.java
@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence"})
@Configuration
@EnableTransactionManagement
public class TestConfig {
@Mock
@Autowired
private UserHibernateDao userHibernateDao;
@Bean
@Autowired
public UserHibernateDao getUserHibernateDao() {
return userHibernateDao;
}
@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setPackagesToScan("ar.edu.itba.paw.models");
factoryBean.setDataSource(dataSource());
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
final Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "none");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("format_sql", "true");
factoryBean.setJpaProperties(properties);
return factoryBean;
}
@Bean
@Autowired
public DataSource dataSource() {
final SimpleDriverDataSource ds = new SimpleDriverDataSource();
ds.setDriverClass(JDBCDriver.class);
ds.setUrl("jdbc:hsqldb:mem:paw");
ds.setUsername("ha");
ds.setPassword("");
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
}
UserJDBCDaoTest (ignore the JDBC name):
@ContextConfiguration(classes = TestConfig.class)
@Sql("classpath:userScript.sql")
@Transactional
public class UserJdbcDaoTest {
private static final long USERID = 1;
private static final long NONEXISTENTUSERID = -1;
private static final String FIRSTNAME = "TestFirstName";
private static final String NEWFIRSTNAME = "TestNewFirstName";
private static final String LASTNAME = "TestLastName";
private static final String EMAIL = "test1@mail.com";
private static final String PASSWORD = "TestPassword";
private static final String PHONENUMBER = "1522334455";
private static final String ROLE = "role";
private User user;
@Autowired
private UserHibernateDao userHibernateDao;
@Autowired
@Before
public void setUp() {
this.userHibernateDao = new UserHibernateDao();
}
@Test
public void testCreateUser() {
//JdbcTestUtils.deleteFromTables(jdbcTemplate, "users");
user = Mockito.mock(User.class);
when(user.getFirstName()).thenReturn(FIRSTNAME);
when(user.getLastName()).thenReturn(LASTNAME);
when(user.getEmail()).thenReturn(EMAIL);
when(user.getPassword()).thenReturn(PASSWORD);
when(user.getPhoneNumber()).thenReturn(PHONENUMBER);
when(user.getRole()).thenReturn(ROLE);
User u = userHibernateDao.create(FIRSTNAME, LASTNAME, EMAIL, PASSWORD, PHONENUMBER, ROLE);
Assert.assertNotNull(u);
Assert.assertEquals(u.getFirstName(), user.getFirstName());
Assert.assertEquals(u.getLastName(), user.getLastName());
Assert.assertEquals(EMAIL, user.getEmail());
Assert.assertEquals(PASSWORD, user.getPassword());
Assert.assertEquals(PHONENUMBER, user.getPhoneNumber());
Assert.assertEquals(ROLE, user.getRole());
//Assert.assertEquals(1, JdbcTestUtils.countRowsInTable(jdbcTemplate, "users"));
}
}
java hibernate testing mockito
java hibernate testing mockito
asked Nov 14 '18 at 13:45
StackoverflowedStackoverflowed
16310
16310
Which line is the null pointer exception on?
– David Lavender
Nov 14 '18 at 13:54
@DavidLavender at the line where it says:final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE))
– Stackoverflowed
Nov 14 '18 at 14:12
@DavidLavender since the request is null, it ends up throwing an exception.
– Stackoverflowed
Nov 14 '18 at 14:13
add a comment |
Which line is the null pointer exception on?
– David Lavender
Nov 14 '18 at 13:54
@DavidLavender at the line where it says:final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE))
– Stackoverflowed
Nov 14 '18 at 14:12
@DavidLavender since the request is null, it ends up throwing an exception.
– Stackoverflowed
Nov 14 '18 at 14:13
Which line is the null pointer exception on?
– David Lavender
Nov 14 '18 at 13:54
Which line is the null pointer exception on?
– David Lavender
Nov 14 '18 at 13:54
@DavidLavender at the line where it says:
final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE))
– Stackoverflowed
Nov 14 '18 at 14:12
@DavidLavender at the line where it says:
final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE))
– Stackoverflowed
Nov 14 '18 at 14:12
@DavidLavender since the request is null, it ends up throwing an exception.
– Stackoverflowed
Nov 14 '18 at 14:13
@DavidLavender since the request is null, it ends up throwing an exception.
– Stackoverflowed
Nov 14 '18 at 14:13
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301694%2fnull-pointer-request-when-trying-to-test-with-hibernate-and-mockito%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301694%2fnull-pointer-request-when-trying-to-test-with-hibernate-and-mockito%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which line is the null pointer exception on?
– David Lavender
Nov 14 '18 at 13:54
@DavidLavender at the line where it says:
final User user = new User(firstName, lastName, email, password, phoneNumber, role, request.getHeader("Accept-Language").substring(0, MAX_LANGUAJE))
– Stackoverflowed
Nov 14 '18 at 14:12
@DavidLavender since the request is null, it ends up throwing an exception.
– Stackoverflowed
Nov 14 '18 at 14:13