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

pom.xml

pom.xml

... </dependency> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-test </artifactId> </dependency> <dependency> ...

Using separate config for testing purposes

src/main/resources/application-test.properties

Get Updates on Telegram We share the every post update using our telegram bot, Get Updates on Telegram powered by GoWeasel

src/main/java/io/singh1114/springboottut/AppConfigurationTest.java

Writing Controller test for Spring Boot API.

src/test/java/io/singh1114/springboottut/SchoolControllerTest.java

@ActiveProfiles

test

Configuration

@RunWith

@SpringBootTest

SpringBootFramework

Running on a random port

SpringBootTest.WebEnvironment.RANDOM_PORT

Removing any change made to the database after test completion.

@Transactional

Transaction

@Transactional

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 ); ...

Get Updates on Telegram We share the every post update using our telegram bot, Get Updates on Telegram 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

mvn test

Create Maven based Spring Boot CI pipeline using CircleCI #java #springboot #testing



May 30, 2020 4 mins read



Shipping your product without testing is like buying a car without turning on the engine.We have to run a few tests again and again during the development of the product and if those tests are not automated, we will have a hard time testing our application.Here is a post that will help you to understand the importance of writing tests?This post is in continuation of the last post we shared related to Spring boot in which we talked about writing Rest APIs in Spring Boot.Please go through that post if you want to learn how to create rest APIs using Spring Boot. In this post, we are going to discuss how we can test our spring boot application from start to end without any mocking.First, we have to add thelibraries to the, 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 toWe 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,and add the following data to it.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.Add the following code to the file,Now since we are going to test the application from end to end, let’s create a controller test. Add the following code toLet’s go through the file and understand this., is used to let the framework know that we are going to use the given profile. As we have already declared theProfile in thefile, we can use it here directly.andis used to start a simple server to carry out the tests using theWithwe are telling the framework to launch the application on a Random PORT.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 withis 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 insideis rolled back in the end. Read more about this on Spring boot logs These are used to create the URL for a given endpoint to hit.This code is used to create the object in the database.Here we are checking if the body of the response is equal to the expected response.Use can use the following command to run the tests.Adding tests to your code is never enough, you have to run those tests after each code push. CircleCI is a tool which help you to build continuous integration pipelines. Read the following post to know more.I hope you liked the post. Please share your views in the comment section below. Also, please Subscribe if you want to read more such posts.