Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Unit Testing in Java How to Test Assertions

Why doesnt JUnit work with IntelliJs autoimport/import on the fly?

I noticed that when writing tests and using the test methods, I had to manually import things such as import static org.junit.Assert.*;. Usually, with auto-import on, I can write the method and have IntelliJ import it automatically. Why doesnt this seem to work with JUnit methods?

1 Answer

Good question orbyt! (I had the same thought..)

As it turns out (after doing a little research...)...

"The libraries for JUnit and TestNG are shipped with IntelliJ IDEA, but are not included in the classpath of your project or module by default. "

per this JetBrains page:

https://www.jetbrains.com/idea/help/configuring-testing-libraries.html

I know what you might be thinking:

"What! That's crazy!"

"Why aren't they included by default?"

"Are these people JetBrains or No Brains!?"

But I guess maybe it has something to do with giving the option for developers to set things up in their own custom way..

Personally I would definitely have appreciated a dialog/prompt at some point saying:

"You appear to be trying to use JUnit with IntelliJ -would you like to bring up a dialog to configure classpath now?"

(and of course I'm going to say yes and hope at least there's a little bit of auto-population in at least some of the fields in that dialog box that comes up...)


I might also recommend a quick read through of this page:

https://www.jetbrains.com/idea/help/testing.html

I'll also through in a few other links I found while researching (that might be helpful):

https://devnet.jetbrains.com/message/3360381;jsessionid=FB7D7882DEE2CE89533B87018F6186AC

http://stackoverflow.com/questions/22105264/running-tests-on-intellij-class-not-found


By the way there is mention of a "Fix" button at just past the 4:00 time point in the "Setting up a project for test" video, but this didn't work for me. I also didn't get an option for selecting JUnit4 in the list.

I ended up spending several hours reconstructing my whole IntelliJ setup to support the way Craig had was using it with JUnit (as opposed to the way I normally use IntelliJ, which is highly customized with my own plugins).


I really think Craig Dennis should have included this JetBrains TDD tutorial as a link in your teacher's notes:

https://www.jetbrains.com/idea/help/tutorial-test-driven-development.html


By the way --here's the full list of Testing Frameworks that IntelliJ supports:

1.) JUnit 2.) TestNG 3.) Groovy JUnit 4.) FlexUnit 5.) PHPUnit 6.) MXUnit 7.) Cucumber

....that might also be useful for Craig Dennis to include as link (in the Teacher's Notes):

https://www.jetbrains.com/idea/help/testing-frameworks.html


To anyone who ends up reading this forum thread I also might recommend trying out some of the Unit Testing plugins:

https://plugins.jetbrains.com/category/index?pr=idea&category_id=86

Of course Jetbrains has an OpenAPI, so you are free to develop your own IntelliJ plugins:

http://www.jetbrains.org/intellij/sdk/docs/index.html

http://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure.html

http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started.html

I am, of course, waiting (with bated breath) to see the JetBrains plug-in development course appear on the TeamTreehouse Roadmap any day now... :wink: