Reputation: 479
I am new to junit, I have a repository as follows:
@Repository
public interface ChartRepository extends JpaRepository<Chart, Integer>{
}
and My Chart Entity class is follows:
@Entity
@Table(name = "Chart")
public class Chart {
@Column(name = "ENT_ID")
private String entID;
@Column(name = "ent_NAME")
private String entName;
@Column(name = "ent_PRODUCER_ID")
private String entProducerId;
@Id
@Column(name = "ent_Rc_ID")
@SequenceGenerator(name = "ent_RC_ID_SEQ", sequenceName="ent_RC_ID_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ent_RC_ID_SEQ")
private Integer entReceiveId;
@Column(name = "JOB_ID")
private Integer jobId;
@Column(name = "CREATE_DT")
private Timestamp createdDate;
//getters and Setters
}
Now, Can we able to write test cases for the repository class. If so how can we do that.Can anyone please suggest me with some code samples.
Upvotes: 1
Views: 11770
Reputation: 1124
I would strongly recomment to use any in-memory DB to test you JPA repository and dont use mock test framework like Mockito, EasyMock, etc. As in Dao layer, there should not be any business logic to mock. it should be simple read/write operation.
I use h2database for this.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
classes = {DatabaseConfig.class},
loader = AnnotationConfigContextLoader.class)
public class ChartRepositoryTest {
@Autowired
private ChartRepository cartRepository;
@Test
public void testfind() {
// do find , insert and validate the response
}
}
testCompile('com.h2database:h2:1.4.196')
This is wha database config file looks like
@Configuration
@EnableJpaRepositories(basePackages = "com.mypackage.repository")
@PropertySource("application-test.properties")
@EnableTransactionManagement
public class DatabaseConfig {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.mypackage.v2" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "create");
properties.setProperty("hibernate.dialect","org.hibernate.dialect.H2Dialect");
return properties;
}
}
Upvotes: 0
Reputation: 316
You can create a @DataJpaTest
and @Autowire
your repository into it. For example:
@RunWith(SpringRunner.class)
@DataJpaTest
public class MyJpaTest {
@Autowired
private ChartRepository chartRepository;
@Test
public void myTest() {
...
}
}
See this for more: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test
Upvotes: 1