Alright. Now that we have a set of repository classes. I believe it’s a good time to introduce unit testing. Remember, unit testing is different compared to integration testing. The unit testing deals with a module like category while integration deals with a set of modules like the catalog. For example, an order module interacting with the inventory module.
Before we start creating our tests, I would like to check the following points:
LoadDatabase class should not load or save the categories by commenting on the initDatabase method.
Set the application properties to use PostgreSQL.
[open application.properties]
To make the development and testing easier when switching database, we will be introducing a new maven profile. [Open pom.xml] Notice that we created a new development-test profile which contains the h2 dependency. Whereas, the default profile development contains all the PostgreSQL specific dependencies as well as Liquibase.
Creating our very first test. As a default, we will have the contextLoads test. [open TerawarehouseCatalogApplicationTests class] What’s with the annotations?
@RunWith(SpringRunner.class) is a bridge between JUnit and Spring. It enables us to use Spring features inside JUnit tests. SpringRunner provides support for loading the ApplicationContext and enables auto-wiring.
@SpringBootTest autoconfigures the application context by loading the default configuration from the class annotated with @SpringBootApplication when it is not annotated with configuration modifiers like @EnableJpaRepositories, @EntityScan, @ComponentScan, @ContextConfiguration, etc.
[open H2JpaConfig class] have a look at this class, how it overrides the configuration.
@TestPropertySource instead of using the application.properties file in the main folder, we will be overriding it with an H2 specific configuration.
[open application-integration.properties file] Finally, it’s now time to present our very first repository unit test for the category.
[open CategoryRepositoryIntegrationTest]
Again we have the @RunWith(SpringRunner.class), expect this in all your test classes.
@DataJpaTest normally use for a JPA specific test. It autoconfigures our data source and entity manager base on the property source which we specified before.
@DirtiesContext it’s possible for us to modify the state of our database and it’s possible that we will need to set up its initial content every test. Then it's better to supply a new context before the method. Otherwise, since we are in read-only mode just remove this annotation.
Now, it’s time to execute our test. Note that you need maven to run it. Here’s the configuration.
[show screenshot of the maven build configuration]
Next, we will start creating our REST API. See you in our next video.
- Database Initialization Using Script
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactThere are times when we want to use a SQL script to generate the schema and populate the database. Personally, I use...
- Database Initialization Using Code
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactThere are various ways in which we can initialize the database. Mostly for testing purposes I find setting hibernate.ddl-auto...
- Working With Spring Data Repositories
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactJust like what we did with the entities the same approach can be used when creating the repository classes. And that...
- Create A Restful Web Service In Spring
Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-cataloghttps://github.com/terawarehouse/terawarehouse-reactTo prepare the project from future development, we will be creating a multi-maven project layout. Unfortunately,...
- Catalog And Sellout Management System Using Spring
A catalog and sales order management system is commonly used by companies to manage their catalog and capture their sales data into a centralized database so that a real-time accurate report can be generated instantly. This application can trigger notification...