AD SPACE AVAILABLE · BRAND PARTNERS WELCOME

How to Use JetBrains AI Assistant in IntelliJ IDEA to Explain Complex Code and Generate Unit Tests

365Finds Editorial
Indexed 11,430 Wiki articles

Put 365 Wiki to work with your AI

Copy the prompt below, paste it into the AI you already use, then ask your question.

Download full Skill

Try it with the prompt first. If you use 365 Wiki often, install the full 365Wiki Skill so you do not have to paste it again. No regular AI? Gemini or Microsoft Copilot on the web will work.

365Wiki Skill

Developers working on unfamiliar codebases or legacy services often spend hours deciphering nested logic, irregular expressions, and edge cases before writing corresponding test coverage. In IntelliJ IDEA, JetBrains AI Assistant embeds generative intelligence directly into the IDE editor and compiler toolchain, allowing engineers to unpack complex algorithms and generate complete unit test suites without copying snippets into browser-based chatbots.

Because the assistant operates with deep awareness of your project’s abstract syntax tree (AST), dependencies, and directory conventions, it can analyze entire call hierarchies and automatically position test files within designated test sources roots (src/test/java or src/test/kotlin). Here is how to configure the assistant, navigate code explanation workflows, and generate reliable unit tests.


1. Prerequisites and Environment Setup

Before triggering AI actions in IntelliJ IDEA, ensure that your environment meets the plugin and licensing requirements:

  1. IDE Version: JetBrains AI Assistant requires IntelliJ IDEA version 2023.3 or later (or 2024.1.1+ for Community Edition).
  2. Plugin Activation: The AI Assistant plugin is not enabled by default. Open Settings | Plugins | Marketplace, search for AI Assistant, and click Install.
  3. Licensing: Access requires an active JetBrains AI subscription (Free, Pro, Ultimate, or Enterprise) or an authorized third-party model connection (Bring Your Own Key / BYOK). If you are a new user, you can activate an initial 30-day AI Pro trial directly through the JetBrains AI widget in the main window toolbar.

Once activated, the AI Assistant tool window and context actions become available across all project files.


2. Explaining Complex Code in the Editor

When auditing dense algorithms, multi-threaded pipelines, or unfamiliar third-party libraries, developers can trigger context-aware explanations directly from the code editor:

Step-by-Step Invocation

  1. Highlight the Target Code: In your editor window, select the method, loop, or block of logic you want to inspect.
  2. Open the Action Menu: Right-click the selection to open the context menu, or press Alt+Enter (Windows/Linux) or Option+Enter (macOS).
  3. Select Explain Code: Choose AI Actions | Explain Code.
  4. Inspect the AI Chat Response: The AI Chat tool window opens instantly with a structured breakdown detailing the selected code's purpose, algorithmic flow, time complexity considerations, and potential edge-case traps.

Verifying Context Attachments

Unlike external LLMs that only see pasted text, JetBrains AI Assistant evaluates surrounding classes, imported types, and project configuration. At the top of the explanation response in the AI Chat window, click the Attachments dropdown. This view reveals every project file, symbol, and context token the assistant referenced to generate the explanation.

Specialized Injected Language Explanation

IntelliJ IDEA automatically detects language injections inside string literals. When your caret sits on a complex regular expression, SQL query, or cron format string, the context menu adapts dynamically:


3. Generating Unit Tests with AI Assistant

Writing test cases manually for boundary conditions, nullability checks, and unexpected exceptions is repetitive. JetBrains AI Assistant automates test scaffolding while adhering to your project's existing testing frameworks (such as JUnit 4/5, TestNG, or Mockito).

Step-by-Step Test Generation Workflow

  1. Target the Method or Class: Place your cursor anywhere inside the method declaration or class body that requires testing.
  2. Trigger Test Creation: Press Alt+Enter (or right-click) and select AI Actions | Generate Unit Tests.
  3. Review in AI Diff: Rather than directly injecting untested code into your project tree, IntelliJ IDEA launches a dedicated AI Diff split tab. You can compare the proposed test suite side-by-side with your project context, inspecting method signatures, imports, and assertions.

Refining Test Output

While in the AI Diff interface, developers have several controls to fine-tune results before saving:

Accepting and Placing Test Files

When the tests meet your criteria, click Accept all:


4. Diagnosing Runtime Errors and Failed Tests

When executing unit tests or running an application in IntelliJ IDEA, test failures and uncaught exceptions frequently produce long stack traces in the Run or Debug console tool window.

Instead of manually parsing logs:

  1. Locate the error message or assertion failure in the console.
  2. Click the Explain with AI button positioned beside the exception, or right-click the log line and choose Explain Error Message with AI.
  3. The AI Chat window diagnoses the root cause (such as an off-by-one index error or unmocked service dependency) and generates a concrete patch.
  4. Click Apply to write the suggested patch directly into the affected source file, or click Insert Snippet at Caret to test it locally.

5. Developer Best Practices and Precautions

To achieve the best balance of speed and code quality when using JetBrains AI Assistant:


References