我正在尝试在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;
}
为了测试此方法,我需要在测试中自动连接变量,但是在请求和实体管理器中会得到一个空指针异常,就像从未初始化过的变量一样。
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(忽略JDBC名称):
@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"));
}
}