Writing Simple Unit-Tests with JUnit

In the Loklak Server project, we use a number of automation tools like the build testing tool ‘TravisCI’, automated code reviewing tool ‘Codacy’, and ‘Gemnasium’. We are also using JUnit, a java-based unit-testing framework for writing automated Unit-Tests for java projects. It can be used to test methods to check their behaviour whenever there is any change in implementation. These unit-tests are handy and are coded specifically for the project. In the Loklak Server project it is used to test the web-scrapers. Generally JUnit is used to check if there is no change in behaviour of the methods, but in this project, it also helps in keeping check if the website code has been modified, affecting the data that is scraped. Let’s start with basics, first by setting up, writing a simple Unit-Tests and then Test-Runners. Here we will refer how unit tests have been implemented in Loklak Server to familiarize with the JUnit Framework. Setting-UP Setting up JUnit with gradle is easy, You have to do just 2 things:- 1) Add JUnit dependency in build.gradle Dependencies { . . . . . .<other compile groups>. . . compile group: 'com.twitter', name: 'jsr166e', version: '1.1.0' compile group: 'com.vividsolutions', name: 'jts', version: '1.13' compile group: 'junit', name: 'junit', version: '4.12' compile group: 'org.apache.logging.log4j', name: 'log4j-1.2-api', version: '2.6.2' compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2' . . . . . . }   2) Add source for 'test' task from where tests are built (like here). Save all tests in test directory and keep its internal directory structure identical to src directory structure. Now set the path in build.gradle so that they can be compiled. sourceSets.test.java.srcDirs = ['test']   Writing Unit-Tests In JUnit FrameWork a Unit-Test is a method that tests a particular behaviour of a section of code. Test methods are identified by annotation @Test. Unit-Test implements methods of source files to test their behaviour. This can be done by fetching the output and comparing it with expected outputs. The following test tests if twitter url that is created is valid or not that is to be scraped. /** * This unit-test tests twitter url creation */ @Test public void testPrepareSearchURL() { String url; String[] query = {"fossasia", "from:loklak_test", "spacex since:2017-04-03 until:2017-04-05"}; String[] filter = {"video", "image", "video,image", "abc,video"}; String[] out_url = { "https://twitter.com/search?f=tweets&vertical=default&q=fossasia&src=typd", "https://twitter.com/search?f=tweets&vertical=default&q=from%3Aloklak_test&src=typd", "and other output url strings to be matched…..." }; // checking simple urls for (int i = 0; i < query.length; i++) { url = TwitterScraper.prepareSearchURL(query[i], ""); //compare urls with urls created assertThat(out_url[i], is(url)); } // checking urls having filters for (int i = 0; i < filter.length; i++) { url = TwitterScraper.prepareSearchURL(query[0], filter[i]); //compare urls with urls created assertThat(out_url[i+3], is(url)); } }   Testing the implementation of code is useless as it will either make code more difficult to change or tests useless  . So be cautious while writing tests and keep difference between Implementation and Behaviour in mind. This is the perfect example for a simple Unit-Test. As we see there are some points,…

Continue ReadingWriting Simple Unit-Tests with JUnit

Join the FOSSASIA Community Network

The FOSSASIA.net project is an initiative to bring the Asian Free and Open Source Software and OpenTech community together and foster cooperation. We make it easy to find communities and projects on a map with links to the websites, social media channels, and automatically updated info from community blogs. Communities store their information decentrally anywhere on the web - on their server, a blog, or git service. FOSSASIA.net collects information through a stardardized API file on the web. You can generate the API file with the FOSSASIA API generator on our website. Then you add your community to the FOSSASIA network website. Simply add the link of your API file to our list of communities on github. FAQ How does it work? You need to provide a file for our API and add the file to our communities repository. Then fill in the link to your API on our directory on github. That is it! Where can I store my file? We prefer that you store your file in our github repository of communities: https://github.com/fossasia/fossasia-communities. It is also possible to store it anywhere on the web, where it is publicly available, e.g. in a blog or CMS page as an attachment. However the github repo is the preferred method currently. How about updating my file? If you need to update your links, just upload a new file in the same location. How do I start? To make it easier, we created a FOSSASIA API Generator, where you can simply fill in your data here: http://api.fossasia.org/generator When you are finished copy the info from the form on the top right into an empty file or use the download button. How do I find Geolocation data of my community? You can find this data from OpenStreetmap. OpenStreetmap shows maps using a URL of the form: www.openstreetmap.org/#map=[Zoom Level]/[Latitude]/[Longitude] So go to www.openstreetmap.org and zoom to the area of your community. For example, if you are zoom into Mumbai, you will get the URL http://www.openstreetmap.org/#map=11/19.0715/72.9499 Then 19.0715 will be the latitude and 72.9499 the longitude. What is an API? In computer programming, an application programming interface (API) is a set of routines, protocols, and tools for building software applications. An API expresses a software component in terms of its operations, inputs, outputs, and underlying types. An API defines functionalities that are independent of their respective implementations (e.g. It does not matter what CMS is used), which allows definitions and implementations to vary without compromising each other. Compare: en.wikipedia.org/wiki/Application_programming_interface Links * Website http://fossasia.net * Map http://fossasia.net/map/map.html * FOSSASIA Community Network Tweets https://twitter.com/fantwk

Continue ReadingJoin the FOSSASIA Community Network