Intellij : Adding 'JUnit' to context menu when creating new file - intellij-idea

Currently in Intellij if I try to create a new file via "New > whatever", I don't see any option to choose a JUnit file. I dug deeper and found out the "Edit File Templates...".
While you can add a variety of files from there to the context menu, there is no ability to choose JUnit.
If you browse over to "Code" tab you can see various JUnit templates. However I have no idea if these are just code generation templates instead of file generation templates. And besides, there is no option to port them to the "Files" tab.
The closest SO thread describing this issue is this and it failed to match my needs.
Mind you I am relatively new to Intellij and I come from eclipse.
P.S: I am aware you can generate tests from existing classes. I am currently trying to implement TDD so that does not answer my needs.

While I don't have an answer for this exact question, I think there's an easier way to handle the underlying requirements (easily creating tests):
Navigate to the class you want to test and press ALT+ENTER. In the context menu, pick "Create Test":
After you choose it, you'll get a dialog with several options including the framework to use (e.g., JUnit 5, JUnit 5, TestNG), the methods you want to generate test stubs for, etc:

Related

How to locate Intellji IDEA "Generate Test" related code?

Right click on a open Java file, we can get Generate item:
Then click Test and go on, we can generate target Unit Test:
I want to develop a new Intellji IDEA plugin base on upper build-in 'Test generator' feature, I want to check related IDEA source code, I just guss may be in some .jar in com.jetbrains.ideaIC library, and fail to locate it for a long time:
I have no better idea about how to locate the code.
Could you tell me where is the related source code, or how to better locate it?
Thanks for your idea!!!
This is the related package in the source code https://github.com/JetBrains/intellij-community/tree/0e2aa4030ee763c9b0c828f0b5119f4cdcc66f35/java/java-impl/src/com/intellij/testIntegration/createTest

Method overview in IntelliJ

In IntelliJ, is it possible to show an overview of all methods (and maybe class variables) of the currently opened file? For instance, in Eclipse, the default settings comprise an overview as desired.
Have you tried "View | File Structure"?

Intellij IDEA Plugin to read and print the contents in a IDE editor in console

I am new to Intellij Idea plugin development using gradle! I am hoping to develop a simple plugin to read the contents of java class and print it in the console(Toolwindow) in a live manner(i.e when I type a new word in the java class it should print the work in the console even if the class is saved or not)
Currently I am refering to the Intellij plugin architecture and components in https://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_components.html. I came across concepts such as editor panes and all But I have no idea how to read the contents in IDE editor(current java file)! How can I do it?
You can grab the raw text of an editor window:
Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
editor.getDocument().getText();
If you want to get some structure from the contents of the editor window, you can use the PsiFile API:
PsiFile psi = anActionEvent.getData(CommonDataKeys.PSI_FILE);
The PsiFile API lets you walk through a file in whatever language(s) make sense. For example, for Java files there is a PsiJavaFile interface that knows about Java specific features like package name, imports, etc.
http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/psi_files.html
Lastly, to print a message you can try normal System.out.print() or you can use the ConsoleView class to work with the IntelliJ console tool views:
TextConsoleBuilderFactory.getInstance()
.createBuilder(anActionEvent.getProject())
.getConsole()
.print("Hello", ConsoleViewContentType.NORMAL_OUTPUT);
One note: All of the above code assumes you're working with an ActionEvent. You might want to check out the TypedActionHandler interface to get notified when the editor text changes:
http://www.jetbrains.org/intellij/sdk/docs/tutorials/editor_basics/editor_events.html#handling-keystrokes-in-the-editor

Using file templates in IntelliJ

I'm quite confused when it comes to file templates in IntelliJ. Basically, when I right click anything and hover my mouse over to New, I can see the usual file templates like Java Class, HTML File and so on. I downloaded Scala plugin and I can see Scala templates (Scala Class, Scala Object, etc.) if I go to Edit File Templates, but I can't actually use/see them in the New menu. Is there a way to modify what I see in the New menu? I'd like to be able to create Scala classes.
Go to Settings -> Menus and Toolbars
There you will see many type of menus. You need edit the menu you want the files to be displayed. Most Probably "Project View Popup Menu".
Click on "Add After" button on right. There you choose the file form plug-ins or other category to be displayed.
I added "Show Bytecode" to meny well it does not make sense here. You can add files from scala plugin like that.
You can restore to Defaults if you mess up easily by clicking on
"Restore All Defaults" button.

No coverage in 'all classes in scope' in Intellij

In Intellij IDEA 14.1.5 Community edition, I imported maven to get coverage from jacoco.exec file. Steps followed
right click on imported module.
select Analyze-->Show Converage Data.
provided valid jacoc.exec file and click 'show selected'
Instead of coverage i'm getting error as no coverage in 'all classes in scope'
can anybody suggest what is wrong?
I had the same thing happen to me.
I was able to fix this by going to "Edit Configurations", to the "Code Coverage" tab.
I'm not sure what caused it, but the wrong package namespace was listed there. I updated the entry there and my subsequent test run with code coverage succeeded.
It happens if your test class and class to test are in different package structures.
My test class was in:
com.tools.api
Class to be tested was in:
com.tools.ws
Once I've corrected the pattern as com.tools.* in the code coverage tab, I was able to see coverage results.
I had the same problem and found solution here.
In the Code Coverage tab, define the following options:
Specify the scope to measure code coverage for. Do one of the following:
To specify a class, click the Add Class button.
To specify a package, click the Add Package button.
I was able to fix this issue by:
Open Edit Configurations menu
Click on Modify Options > Coverage settings > Specify classes and packages
In the new box that appears, click on the plus to add a package
Select a high level package from the project