您已经编写了一个测试(junit)案例,用于从/向MYSQL db创建和获取User实体。 Read功能正常,但Create功能不起作用。我都没有收到任何错误日志。以下是我的代码:
package com.learn.springConfig;
//importes
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.learn.persistence.dao")
@ComponentScan("com.learn.persistence")
@PropertySource({ "classpath:persistence-mysql.properties" })
public class PersistenceJpaConfig {
@Autowired
Environment env;
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.username"));
dataSource.setPassword(env.getProperty("jdbc.password"));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPackagesToScan(new String[] { "com.learn.persistence.model" });
final JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
entityManager.setJpaVendorAdapter(jpaVendorAdapter);
entityManager.setJpaProperties(additionalProperties());
return entityManager;
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
properties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql", "false"));
properties.setProperty("jpa.generateDdl", env.getProperty("jpa.generateDdl", "true"));
return properties;
}
@Bean
public JpaTransactionManager transactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
用户实体:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "USER_ID")
private Long userId;
private String email;
@NotNull
@Size(max = 255)
private String name;
@Nullable
private boolean locked;
@NotNull
@Size(max = 255)
private String password;
//setters getters
}
Jpa用户存储库:
package com.learn.persistence.dao;
public interface UserJpaRepository extends CrudRepository<User, Long>{
}
单元测试案例:
@ContextConfiguration(classes = {PersistenceJpaConfig.class, ContextConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestJPA {
@Autowired
private UserJpaRepository userJpaRepository;
@Test
@Transactional
public void insertUsers() {
User user1 = new User("puspender@fake.com","puspender", false, "12345");
System.out.println("saving user");
assertNotNull(userJpaRepository.save(user1));
}
@Test
@Transactional
public void findAllUsers() {
Iterable<User> users = userJpaRepository.findAll();
for(User user : users) {
System.out.println(user.getName()+"\t"+ user.isLocked());
}
}
}
findAllUsers()
为我带来了所有用户,但insertUsers()
未插入用户
任何提示,为什么会出现这种情况?
答案 0 :(得分:0)
“我没有收到任何错误日志”,所以你的测试正在通过?但您保存的 user1 未显示在findAll()
?
默认情况下,测试是事务性的,在每次测试结束时回滚。
因此,在您的第二次测试中findAllUsers
user1 不再存在。