Categories
5G Network
Agile
Amazon EC2
Android
Angular
Ansible
Arduino
Artificial Intelligence
Augmented Reality
AWS
Azure
Big Data
Blockchain
BootStrap
Cache Teachniques
Cassandra
Commercial Insurance
C#
C++
Cloud
CD
CI
Cyber Security
Data Handling
Data using R
Data Science
DBMS
Design-Pattern
DevOps
ECMAScript
Fortify
Ethical Hacking
Framework
GIT
GIT Slack
Gradle
Hadoop
HBase
HDFS
Hibernate
Hive
HTML
Image Processing
IOT
JavaScript
Java
Jenkins
Jira
JUnit
Kibana
Linux
Machine Learning
MangoDB
MVC
NGINX
Onsen UI
Oracle
PHP
Python
QTP
R Language
Regression Analysis
React JS
Robotic
Salesforce
SAP
Selenium
Service Discovery
Service Now
SOAP UI
Spark SQL
Testing
TOGAF
Research Method
Virtual Reality
Vue.js
Home
Recent Q&A
Feedback
Ask a Question
Explain about the JUNIT
Home
>
JUnit
>
Explain about the JUNIT
May 18, 2020
in
JUnit
Q: JUnit is an open source testing framework which is used to write and run repeatable automated tests, so that we can be ensured that our code works as expected. JUnit is widely used in industry and can be used as stand alone Java program (from the command line) or within an IDE such as Eclipse.
JUnit provides:
• Assertions for testing expected results.
• Test features for sharing common test data.
• Test suites for easily organizing and running tests.
• Graphical and textual test runners. JUnit is used to test:
• an entire object
• part of an object - a method or some interacting methods
• interaction between several objects
JUnit Simple Example using Eclipse
In this section we will see a simple JUnit example. First we will present the class we would like to test:
Calculate.java package com.javacodegeeks.junit;
public class Calculate { public int sum(int var1, int var2)
{
System.out.println("Adding values: " + var1 + " + " + var2); return var1 + var2;
}
}
In the above source code, we can notice that the class has one public method named sum(), which gets as inputs two integers, adds them and returns the result. So, we will test this method. For this purpose, we will create another class including methods that will test each one of the methods of the previous class (in this case, we have only one method to be tested). This is the most JUnit Tutorial 3 / 26 common way of usage. Of course, if a method is very complex and extended, we can have more than one test methods for this complex method. The details of creating test cases will be presented in the next sections. Below, there is the code of the class named CalculateTest.java, which has the role of our test class:
CalculateTest.java package com.javacodegeeks.junit; import static org.junit.Assert.*;
import org.junit.Test; public class CalculateTest { Calculate calculation = new Calculate(); int sum = calculation.sum(2, 5); int testSum = 7;
@Test public void testSum() { System.out.println("@Test sum(): " + sum + " = " + testSum); assertEquals(sum, testSum); } }
Let’s explain the above code. Firstly, we can see that there is a @Test annotation above the testSum() method. This annotation indicates that the public void method to which it is attached can be run as a test case. Hence, the testSum() method is the method that will test the sum() public method. We can also observe a method called assertEquals(sum, testsum). The method assertEquals ([String message], object expected, object actual) takes as inputs two objects and asserts that the two objects are equal. If we run the test class, by right-clicking in the test class and select Run As → Junit Test, the program output will look like that: Adding values: 2 + 5 @Test sum(): 7 = 7 To see the actual result of a JUnit test, Eclipse IDE provides a JUnit window which shows the results of the tests. In this case where the test succeeds, the JUnit window does not show any errors or failures, as we can see in the image below: JUnit Tutorial 4 / 26 Figure 2.1: JUnit window after a successful test. Now, if we change this line of code: int testSum = 10; so that the integers to be tested are not equal, the output will be: Adding values: 2 + 5 @Test sum(): 7 = 10 And in the JUnit window, an error will appear and this message will be displayed: java.lang.AssertionError: expected: but was: at com.javacodegeeks.junit.CalculateTest.testSum(CalculateTest.java:16)
#junit
0
Answers
Click here to read more about Loan/Mortgage
Click here to read more about Insurance
Facebook
Twitter
LinkedIn
Related questions
0
votes
Q: Explain about JUnit - API
May 19, 2020
in
JUnit
#junit
0
votes
Q: Which of the following is correct about Test Suite in JUnit?
May 18, 2020
in
JUnit
#junit
0
votes
Q: Which of the following is correct about Test Runner in JUnit?
May 18, 2020
in
JUnit
#junit
0
votes
Q: Which of the following tools provides JUnit integration?
May 18, 2020
in
JUnit
#junit
0
votes
Q: Which of the following annotation tells JUnit that the public void method to which it is attached can be run as a test case?
May 18, 2020
in
JUnit
#junit
0
votes
Q: What is JUnit - Using Assertion
May 19, 2020
in
JUnit
#junit
...