Integration testing of APIs in Spring-Boot( Java) with Junit | For Beginners
Shipping your product without testing is like buying a car without turning on the engine.
2. Writing Controller test for Spring Boot API.
3. Running on a random port
4. Removing any change made to the database after test completion.
5. Creating the API endpoint
6. Creating the School Object
7. Sending the request and getting the response
8. Checking if the response is equal to the object created
9. Run the tests
What have I learned about testing as a full-stack developer
#testing
#python
#javascript
#webdev
December 16, 2019
15 mins read
Building RESTful APIs with Java Spring Boot framework | For Beginners
#API
#java
#springboot
#technology
October 28, 2019
13 mins read
test
libraries to the pom.xml
, so that the Maven can install them for us. Here is the GitHub Repo that contains the code for this tutorial.
Here is the code to add testing libraries to pom.xml
...
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
...
Using separate config for testing purposes
We don’t want to add the data to the same database as of the production one. So, we will start by creating a separate configuration for handling the test cases. Also, its always a good practice to have separate configurations anyway. Create a new resource file,src/main/resources/application-test.properties
and add the following data to it.
Note: We will have to create the database in our local system to make it work.
Now that we have a separate configuration file, we have to export it as an App configuration so that we can use it in the tests itself.
We share the every post update using our telegram bot,
powered by GoWeasel
src/main/java/io/singh1114/springboottut/AppConfigurationTest.java
Writing Controller test for Spring Boot API.
Now since we are going to test the application from end to end, let’s create a controller test. Add the following code tosrc/test/java/io/singh1114/springboottut/SchoolControllerTest.java
Let’s go through the file and understand this.
@ActiveProfiles
, is used to let the framework know that we are going to use the given profile. As we have already declared the test
Profile in the Configuration
file, we can use it here directly.
@RunWith
and @SpringBootTest
is used to start a simple server to carry out the tests using the SpringBootFramework
.
Running on a random port
WithSpringBootTest.WebEnvironment.RANDOM_PORT
we are telling the framework to launch the application on a Random PORT.
Removing any change made to the database after test completion.
@Transactional
will help us to clean the database once tests are completed. We don’t want to keep adding stuff to the database and want to clean it after every test run.
Wrapping something with Transaction
is a very general programming concept which is present in almost every Database. According to this, if you run any number of SQL commands inside the transaction and if it fails on any step, the whole transaction will be reverted.
This is fairly useful when you are running related commands, for example, reducing the inventory size and adding stuff to someone brought list.
The same concept is used here, the only hack is that anything you run inside @Transactional
is rolled back in the end.
Read more about this on Spring boot logs.
Creating the API endpoint
@LocalServerPort
private int port;
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
Creating the School Object
@Autowired
private SchoolRepository repository;
...
School testSchool = new School(1, "First Location", "Mr. Ranvir", "California");
repository.save(testSchool);
...
We share the every post update using our telegram bot,
powered by GoWeasel
Sending the request and getting the response
TestRestTemplate restTemplate = new TestRestTemplate();
HttpHeaders headers = new HttpHeaders();
...
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<String> response = restTemplate.exchange(
createURLWithPort("/schools"),
HttpMethod.GET, entity, String.class);
...
Checking if the response is equal to the object created
JSONAssert.assertEquals(expected, response.getBody(), false);
Run the tests
Use can use the following command to run the tests.mvn test
Create Maven based Spring Boot CI pipeline using CircleCI
#java
#springboot
#testing
May 30, 2020
4 mins read
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!