Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is IntelliJ
3.
What is Android Studio
4.
Creating and Running Tests in IntelliJ
5.
Running Applications in IntelliJ 
5.1.
Direct Way
5.2.
Customizable way
6.
Frameworks in IntelliJ for Testing
6.1.
Cucumber
6.2.
JUnit
6.3.
TestNG
6.4.
Selenium
6.5.
Spock
7.
Testing Tools Used in Android Studio
7.1.
JUnit
7.2.
Espresso
7.3.
Monkey
7.4.
App Crawler
8.
Frequently Asked Questions
8.1.
How do I open a project in IntelliJ IDEA?
8.2.
How do I run a Java program in IntelliJ IDEA?
8.3.
How do I debug a Java program in IntelliJ IDEA?
8.4.
How can I create a new file or folder in IntelliJ IDEA?
8.5.
How do I install new plugins in IntelliJ IDEA?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

What are the various tools used in IntelliJ Idea and Android Studio for Testing?

Author Amit Singh
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Have you ever worked on Java Programming language? Have you done testing in IntelliJ or Android Studio?

What are the various tools used in IntelliJ Idea and Android Studio for Testing?

In this article, we will learn about various testing tools used on IntelliJ IDEA and Android Studio. We will create and run some custom tests. We will learn about the frameworks that we can use for testing. Let's dive into the article to learn more about the topic.

What is IntelliJ

IntelliJ IDEA is a software tool that aids developers in writing code more effectively. It is primarily used for Java. Still, you can use it for other programming languages. It offers features such as code suggestions, finding and fixing bugs, and integration with other tools like version control systems. Additionally, it has a user-friendly interface that is easy to navigate and use.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Android Studio

Android Studio is a software tool used to develop apps for the android operating system. It has features like a code editor, a visual layout editor, debugging tools, and emulators for testing on different devices. It also provides built-in support for the Google Cloud Platform, allowing developers to integrate cloud services into their apps easily. It's easy to use, has templates and samples to help users to get started quickly, and enables developers to create, test, and release high-quality apps for mobile devices.

Creating and Running Tests in IntelliJ

Testing is essential to the software development life cycle (SDLC). You must test your code against different test cases. IntelliJ allows you to test your code as well.

We will help you understand the testing in IntelliJ. We will create and run some basic tests. We will follow the below steps.

We will start by creating a new project. 

project

The second step is to add JUnit to the project. 

  • We will add JUnit 4.13 to the project. Click on file -> Project Structure.
project
  • Click on the Libraries tab. Search for JUnit 4.13 and add it to the project.
searching

We will add a folder to hold the tests. 

  1. Again, open the Project Structure. Click on the Modules tab. 
     
  2. Right-click on the project. Select the "New Directory" option. Name the new directory "test."  
     
  3. Select the "test" folder and mark it as a test source folder.
project

We will make a source file.

  • Right-click on the "src" folder. Select New-> Java Class. We will name it the "MyClass" file.
file
  • We have created a simple method, "result," in the class. It will return a quotient of a division.

MyClass.java

package com.shadow.junit.basic;

public class MyClass {
    public int result(int a, int b) {
        if(b==0) {
            throw new IllegalArgumentException("B cannot be zero!!");
        }
        // We are intentionally doing it wrong.
        return a * b;
    }
}

We will create a test file.

  • Click on "MyClass" in the editor. Press Alt+Enter. Select the "Create Test" option.
test
  • Select Junit 4 as Testing Library. Check the "Generate test methods for." Click the "OK" button.
test

"MyClassTest.java" is created in the test folder. We have made two tests in the test file.

  1. To find the correct result after the calculation.
     
  2. Throw the divide by zero exception.
     

MyClassTest.java

package com.shadow.junit.basic;
import org.junit.Test;
import static org.junit.Assert.*;

public class MyClassTest {

    @Test
    public void testResult() {
        MyClass tester = new MyClass();
        assertEquals("10 / 5 must be 2",2, tester.result(10, 5));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testException() {
        MyClass tester = new MyClass();
        tester.result(10,0);
    }
}

We will run the test file.

  • Right-click on the play button on the editor. Click on "Run MyClassTest."
run test
  • The test has failed. We have intentionally put the wrong code to generate failure.
test result

Changing the Code to Pass the Test

  1. We will open the MyClass.java file.
     
  2. We will change the content to correct it.
     

MyClass.java

package com.shadow.junit.basic;
public class MyClass {
    public int result(int a, int b) {
        if(b==0) {
            throw new IllegalArgumentException("B cannot be zero!!");
        }
        return a / b;
    }
}

We will run the code after correcting it. 

We will rerun the test. The test has passed.

test result

Running Applications in IntelliJ 

You can use IntelliJ IDEA to run the application. You must set up an “SDK” (Software Development Kit) to use in your project. 

There are two ways to run applications using your IntelliJ idea.

  1. Direct Way: It includes running the applications directly from the editor or the current file options.
     
  2. Customizable way: You can modify the configurations per your need before running the applications.
     

Which way to use it depends on two questions: 

  1. Are you passing the parameters to your program or not?
     
  2. Do you want to perform some actions before starting the application?
     

Direct Way

You can use the direct way if you don't require passing parameters or specific actions. You can either run from the editor or run the current file.

If you want to run directly from the editor, you can use the following steps:

  1. You can click on the play button near the declaration of the class.
     
  2. Select the "Run 'MyClass.main()'" option.
run

Your class must have a main() function.

If you want to run the "current file," you can use the following steps:

  1. Open the file you want to run in the editor.
     
  2. Click on the play button next to the option 'Current File' option on the toolbar.
run

You can try other options to run the file. 

  1. Click on the Current File option.
     
  2. A submenu will appear. Click on the Current file with a right arrow.
     
  3. It will show you different options. Select as per your need.
current

Customizable way

You can add the VM options to pass the parameters in your program. You can also use the run/debug configuration option to customize the program startup.

If you want to customize the configuration, you can use the following steps:

  • Click on the play button near the declaration of the class. 
     
  • Select the Modify Run Configuration option.
configuration
  • An "Edit Run Configuration" dialog box will appear. You can modify the configurations, such as parameters.
     
  • Click on OK to confirm the changes.
run
  • Finally, you can run by clicking on the play button or pressing Shift+F10.

Frameworks in IntelliJ for Testing

There are many tools available in the market for testing in IntelliJ. Following are some of the popular tools used in IntelliJ for testing.

Cucumber

Cucumber is a framework for testing. It allows you to write scenarios and features in a language, i.e., readable by humans. IntelliJ IDEA supports Cucumber. 

logo

It has the following features:

  • Highlighting the Syntax and Errors.
     
  • Navigate between steps and step definitions (java method with annotation).
     
  • A dedicated quick fix to help you create the step definitions.
     
  • Completion of the Code.
     
  • You can use other languages in feature files. A feature file is a file with a description of steps and scenarios.

JUnit

JUnit is a tool for testing the code, i.e., written in Java. It helps developers ensure their programs work correctly by allowing them to write and run the tests. JUnit is easy to use and works well with other Java tools. It is also open-source and commonly used in both, industry and education. The latest version, JUnit 5, was released in 2017. 

logo

It has several useful features, such as:

  • Annotation-based: Tests are easy to write and run with JUnit's annotation system.
     
  • Assertions: JUnit has a wide range of assertions for checking the results of tests.
     
  • Integration: It can be integrated with other Java tools, such as IDEs and build tools.
     
  • Test runners: Tests can be run from the command line or within a build environment.
     
  • Parameterized tests: A single test can be run multiple times with different input values.

TestNG

TestNG is a testing tool for Java, similar to JUnit. It is designed to cover a wide range of test types, including unit, functional, and end-to-end tests. TestNG is easy to use and works well with other Java tools. It is also powerful and flexible, providing features such as data-driven testing and parallel execution. It is a popular choice for Java developers. It is more advanced than JUnit. 

logo

It has several useful features, such as:

  • Annotation-based: Tests are easy to write and run with TestNG's annotation system.
     
  • Assertions: TestNG has a wide range of assertions for checking the results of tests.
     
  • Data-driven testing: It allows test methods to be run multiple times with different input values.
     
  • Parallel test execution: Tests can run parallelly, speeding up the testing process.
     
  • Flexible configuration: You can configure the tests through XML or annotations.
     
  • Grouping of test methods: It allows you to group test methods, making it easy to run specific tests.

Selenium

Selenium is a tool for automating web browsers and testing websites. You can use it with multiple programming languages. Developers often use it in combination with other testing frameworks. Selenium allows you to script interactions with a web page, such as clicking links and filling out forms and can be integrated with build tools. It has a large and supportive community and supports multiple browsers. Selenium WebDriver allows you to interact with a web page, and Selenium Grid allows running tests in parallel on various machines and browsers. 

logo

It has features such as:

  • Supporting multiple browsers: Selenium works with Chrome, Firefox, Safari, Edge, and Internet Explorer
    .
  • Supporting numerous programming languages: You can use Selenium with Java, C#, Python, JavaScript, and Ruby.
     
  • Allowing for programmatic interaction with web pages through the WebDriver API: Selenium will enable you to simulate user actions such as clicking links and filling out forms.
     
  • Supporting parallel test execution: Selenium Grid allows you to run tests simultaneously on multiple machines and browsers.
     
  • Being able to be integrated with other testing frameworks and build tools: You can integrate Selenium with JUnit, TestNG, Maven and Gradle.
     
  • Having a large community: Selenium has many resources and support available from its active community.

Spock

Spock is a testing framework for Java and Groovy applications. Its design makes writing tests easy and understandable, with clear and concise syntax. It allows for data-driven testing and powerful assertions and supports parallel test execution. It also can integrate with other testing frameworks, such as JUnit and TestNG. Additionally, it allows writing test cases in Groovy, which makes it more readable and expressive.

logo

Its features include:

  • A simple and easy-to-use syntax that makes it easy to understand the intent of a test and its expected outcome.
     
  • The ability to run tests multiple times with different input values using data-driven testing.
     
  • A powerful assertion mechanism to easily check the results of tests.
     
  • The ability to run tests in parallel to speed up the test execution time.
     
  • The ability to integrate with other testing frameworks to make it easy to integrate with existing test suites.
     
  • The ability to write test cases in Groovy for more readability and expressiveness.
     
  • Clear and readable error messages to help diagnose and fix issues.

Testing Tools Used in Android Studio

There are many tools available in the market for android app testing. Following are some of the popular tools used in Android Studio for testing.

JUnit

JUnit is a popular testing framework used in Java and Android app development. It helps to write and run tests on specific code units, like classes and methods, to check their functionality. JUnit makes it easy to catch bugs early with its automated testing process. The framework includes annotations and assertions to organize and validate tests. It's straightforward and well-supported, making it a common choice for Java and Android testing.

logo

Here are the key features of JUnit:

  1. Testing designed for Java and Android code units.
     
  2. Streamlined testing through automation.
     
  3. Annotation-based structure for tests.
     
  4. Verification of expected results using assertions.
     
  5. User-friendly and easy to implement.
     
  6. Widespread use in Java and Android development.
     
  7. Robust support and maintenance by the community.

Espresso

The Espresso is a tool for testing Android apps in Android Studio. It helps developers automate testing how the app interacts with the user. Espresso tests can be run on real devices or emulators and simulate user actions such as clicking buttons and typing. The tool ensures the test waits for the app's interface to be ready before moving on. Espresso is fast, reliable, and widely used by the Android development community.

logo

Some of the features of Espresso include:

  1. Automated UI testing.
     
  2. Synchronization with app UI.
     
  3. Support for testing on real devices or emulators.
     
  4. Ability to interact with app UI elements, such as buttons and text fields.
     
  5. Fast execution.
     
  6. Easy integration with Android Studio.
     
  7. Reliable and widely used in the Android development community.

Monkey

The Monkey is a testing tool for Android apps that helps identify bugs by randomly generating user events like clicks and swipes. It simulates random user actions to find app performance and behavior problems. The tool is run from the command line and provides a fast way to generate many events. It helps ensure the app can handle unexpected user inputs, leading to a more stable app. You can include it in automated testing for an even more efficient bug detection process.

logo

Here are the key features of Monkey:

  1. Testing tool for Android apps.
     
  2. Stress-testing through random user event generation.
     
  3. It simulates unexpected user inputs for bug detection.
     
  4. Run from the command line for quick event generation.
     
  5. It improves overall app stability.
     
  6. You can include it in the automated testing process.

App Crawler

App Crawler is a testing tool for mobile apps that automates the navigation process. It identifies UI problems and functionality issues, such as broken links, by testing a variety of use cases. The tool can also detect performance issues through large event generation. By using App Crawler, developers can improve the quality of their app by fixing problems early in the development phase.

App Crawler

Here are the key features of App Crawler:

  1. An automated navigation tool for mobile app testing.
     
  2. It identifies UI and functionality issues, such as broken links.
     
  3. It tests a wide range of use cases for comprehensive coverage.
     
  4. It detects performance problems through large event generation.
     
  5. Improves app quality by finding and fixing issues early in development.

Frequently Asked Questions

How do I open a project in IntelliJ IDEA?

You can use the "Open" or "Import" option on the welcome screen. You can also use the "File" menu -> "Open" or "Import" to open or import a project.

How do I run a Java program in IntelliJ IDEA?

You can right-click on the class with the main method and select "Run." You can also use the "Run" menu and select "Run" to start the program.

How do I debug a Java program in IntelliJ IDEA?

You can set breakpoints in the code by clicking on the "left gutter" of the "editor." Then use the "Run" menu and select "Debug" to start the program in debug mode.

How can I create a new file or folder in IntelliJ IDEA?

You can use the "File" menu -> "New." Choose the appropriate option, such as "File" or "Folder." It will create a new file or folder.

How do I install new plugins in IntelliJ IDEA?

You can use the "Settings" or "Preferences" menu. Navigate to the "Plugins" section and then use the "Browse Repositories" button to search and install new plugins.

Conclusion

So that's the end of the article. 

After reading about the testing tools for IntelliJ and Android Studio, Are you interested in reading/exploring more articles on Testing? Don't worry; Coding Ninjas has you covered.

Check out some articles on testing, like black box testing and different functional testing types.

However, if you want to give your work an edge over the competition, you might choose to enroll in one of our premium courses.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Merry Learning!

Previous article
Rest Client in VS Code, PyCharm and IntelliJ
Next article
TestNG in Eclipse
Live masterclass