ChatGPT解决这个技术问题 Extra ChatGPT

Configuring IntelliJ IDEA for unit testing with JUnit

I decided to try out IntelliJ this morning via the trial version and installed the JUnit plugin. I made a new Java project and I want to write a test case for it.

How do I add the junit.jar file to my project? (I actually want to add it to every java project, now and forever more - is there a way of doing that?).

you could accept one as the answer, and I would prefer the one from @CrazyCoder...
In fact none of the answers actually answers the question "How do I add the junit.jar to my project?", yet 71 and 104 votes... SO never stops amazing me.

H
Hulk1991

If you already have a test class, but missing the JUnit library dependency, please refer to Configuring Libraries for Unit Testing documentation section. Pressing Alt+Enter on the red code should give you an intention action to add the missing jar.

However, IDEA offers much more. If you don't have a test class yet and want to create one for any of the source classes, see instructions below.

You can use the Create Test intention action by pressing Alt+Enter while standing on the name of your class inside the editor or by using Ctrl+Shift+T keyboard shortcut.

A dialog appears where you select what testing framework to use and press Fix button for the first time to add the required library jars to the module dependencies. You can also select methods to create the test stubs for.

https://i.stack.imgur.com/1pnVQ.png

https://i.stack.imgur.com/kWulJ.png

You can find more details in the Testing help section of the on-line documentation.


Ah k, thank you. The link to "Testing help section" led me to putting the cursor on the unresolved @Test in my test file and now my life is complete!
Doesn't cmd-shift-T mean I have to start with the class under test instead of the test itself?
First link does not work. Here's the correct one: site2.jetbrains.com/idea/webhelp/…
@Bob +1 for pointing at the flow breach. But is making MyClass first, then immediately create the corresponding MyClassTest with Ctrl+Shift+T; is that necessarily that bad? Making MyClassTest implies that there will be a MyClass class one test later, right?
I
Igor Konoplyanko

Press Ctrl+Shift+T in the code editor. It will show you popup with suggestion to create a test.

Mac OS: ⌘ Cmd+Shift+T


If you want to have "strong" answer please scroll down. This is quick and short answer, below is more detailed answer.
The answer can be quick and short and still describe what the menu does and which option the OP should consider choosing.
You don't know what ctr-shft-t maps to.
on mac, the mapping gives me thumbnails. pretty sure that's not the right one.
G
Gayan Weerakutti

One way of doing this is to do add junit.jar to your $CLASSPATH as an external dependency.

https://i.stack.imgur.com/0UiY8.gif

So to do that, go to project structure, and then add JUnit as one of the libraries as shown in the gif.

In the 'Choose Modules' prompt choose only the modules that you'd need JUnit for.


You are a rock star, thank you for the gif showing what to do!
Much prefer this answer.
i
irudyak

If you already have test classes you may:

1) Put a cursor on a class declaration and press Alt + Enter. In the dialogue choose JUnit and press Fix. This is a standard way to create test classes in IntelliJ.

2) Alternatively you may add JUnit jars manually (download from site or take from IntelliJ files).


When I do this it gives me the error: Gradle sync failed: The newly created daemon process has a different context than expected. Any tips on how I could fix this?
The question is : how exactly do you add JUnit jars manually ??
I suppose that after all this time, we still have no idea on how to add junit manually?
This duplicates earlier previous answers.
R
Robert Lujo

In my case (IntelliJ 2020-02, Kotlin dev) JUnit library was already included by Create project wizard. I needed to enable JUnit plugin:

https://i.stack.imgur.com/r1UzP.png

to get green Run test icons next to each test class and method:

https://i.stack.imgur.com/5GQo9.png

and CTRL+Shift+R will run test under caret, and CTRL+shift+D to debug.