Spring Repository Unit Testing

Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-catalog https://github.com/terawarehouse/terawarehouse-react Al...


Slack Discussion

Repositories
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.

Ja ne.

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: Spring Repository Unit Testing
Spring Repository Unit Testing
https://i.ytimg.com/vi/KLxQzabYluc/0.jpg
https://i.ytimg.com/vi/KLxQzabYluc/0.jpg
toztech
https://toztech.blogspot.com/2019/06/spring-repository-unit-testing.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2019/06/spring-repository-unit-testing.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content