Starting a simple JUnit / Maven project in IntelliJ Idea - java

I'm trying to find the quickest way to get a simple JUnit test up and running with IntelliJ IDEA and Maven. This is what I do:
File > New Project
Maven > Create from Archetype > org.apache.maven.archetypes:maven-archetype-quickstart
Give it a unique GroupId and ArtifactId ("testingmaven")
Give it a project name ("testingmaven")
Click Finish
At this point, I get the following file structure:
project
|_src
|_main
|_java
|_testingmaven
|_App.java
|_test
|_java
|_testingmaven
|_AppTest.java
But I can't run anything. When I put a simple JUnit test in AppTest.java and click "Build > Run" it forces me to update configurations. It doesn't seem to recognise my files as Java.
I just want to be able to start a Maven project, write a simple JUnit test, and run the test.
I'm coming from Ruby, so I know I'm just not used to package organisation and IDE configuration.
Am I missing something?
Is it because of the archetype I'm starting with?
When I tried to start a Maven project without an archetype, I didn't get pom.xml.
Is there a better archetype for my purposes?

The fasters way for creation Maven project is generating it from a console -> import to IDE.
for this purpose you can use maven-archetype-quickstart archetype.
Just execute following command:
mvn archetype:generate -DgroupId={com.mycompany.app} -DartifactId={my-app-name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
You have to replace {...} params with your data, like project name and packages.
maven-archetype-quickstart automatically adds junit to pom file and creates test stub. By default, junit version is 3. My suggestion is to update it to 4.
After this you can easily import this project with Intellij Idea:
File > New > Project from Existing Sources > select pom.xml (double click) > Next ... > Finish
Also don't forget about formatting shortcut Ctrl+Alt+L
Useful link:
How to create a Java project with Maven

Steps if you've already created your maven project with IntelliJ without archetype:
Place cursor somewhere inside your class
Press <Alt>+<Insert>
Select Test...
Choose the methods you want tested and any other options
Source
Notes: in my case, I chose JUnit5. I had to import those modules into the project afterwards (the contextual help offered by pressing <Alt>+<Enter> did the trick).

Related

How to run a main method method without building the WHOLE maven project in idea?

I have a maven project with many demo classes with their own main methods, they don't dependent on each other. I used to be able to run a class by right clicking on its main method, and select Run MyClass.main(). But now instead of running it, it started to build the maven project as a whole.
Is there a way to "fix" this?
IDE performs tasks that are set in Before launch section of the Run/Debug Configuration. By default the Build set is set there - which will build the all the sources which deletd on the code you are trying to run.
You can remove the Build step from there and build the module alone from the module context menu of this module. See compile module for more description.
Use Build, no error check step in Before launch section.
Check that in the settings the "Delegate IDE build..." is not selected:

How to import GAE java project (with maven) in Eclipse?

I'm reading this: https://cloud.google.com/java/getting-started/using-forms
and i'd like to import this project in Eclipse from 0, but i'm getting a lot of errors. (Generally any GAE projects).
What are the steps to do this?
Create new Java project, run Maven (?) and so on...
I need to use Mars 2 and have installed jdk 1.7 and 1.8.
THX.
(PS: It works using the guide. So i can run the app on cmd)
Update: errors after AndrĂ¡s Kerekes'solutions
Try to follow these steps:
git clone the whole Getting Started Java repository
run mvn eclipse:eclipse in the top level directory of the cloned repository
import the projects into Eclipse (including the one at the top level: the project is called getting-started-java, you should see it in the Project Explorer) using File > Import... > Maven > Existing Maven Projects
You may see an error dialog about Maven errors, in the Action field, click on the cell and select to option to install the m2e connector for the JDT compiler. This will install a plugin into your Eclipse to bridge M2Eclipse and the JDT, you'll need to restart Eclipse once it finishes.
You may still see Maven errors like Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (execution: default-compile, phase: compile), which is OK. You dependencies should be set for the project and there should be no compilation error.
Ensure that the project uses JDK8 as it depends on classes that are only available since Java 8 (like java.util.function.Function)
You may want to install the Cloud Tools for Eclipse plugin for additional GAE support.

Run Gradle test and not Junit test in IntelliJ IDEA 15 when choosing configuration type to run with

I updated to Intellij 15.02 from 14 and wanted to run my tests using Gradle not JUnit, but I'm not getting the options anymore like the image you see here.
I want to be able to choose configuration type.
The thing that is happening now when I run a test is that it runs it as a JUnit test by default.
I have imported the gradle project with gradle by selecting the gradle file in the project.
I have deleted each configuration entry at the top so everything is clean and empty when running a new test with Spock.
My current "fix" is to manually create a new config entry for the gradle test. Intellij is not intelligent enough to create that same Junit test as a Gralde test.
I'm confused.
I found the solution.
They have changed the way you select this option in the new version of intellij 15.02.
You have to select Gradle Test Runner.
Please take a look:
In modern versions of IntelliJ (I have Ultimate 2019.2.3) here's what you need to update:
Update from "Gradle" to "IntelliJ IDEA"

maven - using local source instead of external dependency

If someone could help me out here it would save me a lot of time.
I maintain an open source library that gets pushed out to a sonatype repository. I make changes to that library a few times a day and push it out to the 1.0_snapshot build using mvn deploy.
Let's call it project1
I work constantly in another project that uses that library let's call it project2.
Right now, whenever i make changes to project 1 or 2, i need to first build and deploy project 1 to the repo, then build project 2 so it downloads a fresh copy of project1.jar
Project2 has Project1 as a dependency in a pom.xml:
<dependency>
<groupId>com.group</groupId>
<artifactId>project1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
in order to build in a way where all of my changes can be tested, I have to do something like this:
mvn -f ./project1/pom.xml clean deploy
mvn -U -f ./project2/pom.xml clean package
this uploads my project1.jar to sonatype, then project2 downloads the new snapshot and builds it.
This is a simplified picture of what i'm doing on a larger scale, where my compiles take 5 minutes of up and downloads.
Question: What is the proper way to use maven so it knows to use the source of project1 in a dependency in project 2?
IDE:
install m2e in eclipse
import your both projects into workspace
from consumer project (right click > maven > enable workspace resolution)
this will put project2's classes in classpath from its target/classes instead of the actual jar
native straight maven:
You can create a maven project tree, if this two are open source project going through same build cycle it must have one already, if they are not related but related for your usecase then you can temporarily create a maven tree on top of these 2 projects and build the top parent it will build from bottom up in one command
it will find the leaf project, build it install it in maven's cache and now while building projectA it will refer it from maven's cache so no need to deploy to sonatype
You can also point project2 to build using offline mode :
mvn -o package
Then you drop the upload part of the project1 build to the remote repo.
Check the following link: Intro to repositories and How do I configure Maven for offline development?

Configuration of Hudson in eclipse

I have a eclipse Dynamic web project java base and use maven for creating project .But i want to build the project in Hudson for testing purpose .
So please anyone can give me steps to configure Hudson for building project in eclipse.
Thanks
0) Read a guide about Hudson an CI.
1) Create a new taks on hudson based on maven project.
2) Configure the task to checkout the code from your svn/git.
3) Configure the mvn step to execute "mvn test".
4) Check your results.

Categories