-1

I am struggling to run unit tests or main methods in IntelliJ via the 'Run' button. (Project was set up via maven)

Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.andrew.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <java.version>1.8</java.version>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Here is my project directory:

├── my-app.iml
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── andrew
│   │               ├── app
│   │               └── Hello.java
│   └── test
│       └── java
│           └── com
│               └── andrew
│                   ├── app
│                   └── HelloTest.java
└── target
    ├── classes
    │   └── com
    │       └── andrew
    │           └── Hello.class
    ├── generated-sources
    │   └── annotations
    └── maven-status
        └── maven-compiler-plugin
            └── compile
                └── default-compile
                    ├── createdFiles.lst
                    └── inputFiles.lst

I have tried the followings:

  • Setting Project's compiler output to {project directory}/target
  • Invalidate cache and restart, rebuild
  • Running java com.andrew.Hello in ./target/classes returns Hello World
  • Set ./src/main/java as Source

But I still can't fix the issue.

creampiedonut
  • 327
  • 1
  • 5
  • 17

4 Answers4

1

Solution

Within Project Structure -> compiler output, I have orignally stated /home/andrew/IdeaProjects/Maven:The-Definitive-Guide/my-app/classes

I have to change it to: /home/andrew/IdeaProjects/Maven_The_Definitive_Guide/my-app/classes

creampiedonut
  • 327
  • 1
  • 5
  • 17
0

You need to make sure of a couple of things here....

Check to make sure all of your test methods are Annotated with {@Test} - see the below image I have added also.

Once they are all annotated you should be able to right click on the file and an option should be to run test, after you do this - the run button automatically adds the latest run configuration to be the last thing you ran, so it should show up there for the Run button

Java Unit Test Example Intellij

0

For my case, I add a Maven run command in IntelliJ Run - Edit Configuration, and set command line parameter as clean install spring-boot:run, it works as run command ./mvnw clean install spring-boot:run in Mac terminal.

Yao Li
  • 2,071
  • 1
  • 25
  • 24
-1

Right click in the Project View on Hello -> "Run Hello.main()" should work for you.

  • Another possible solution is to add the maven exec plugin to your pom and execute this target https://stackoverflow.com/questions/1089285/maven-run-project https://stackoverflow.com/questions/42540018/run-a-maven-project-using-intellij-idea – Oliver Tazl Jun 14 '18 at 13:35
  • Yes, that works but is there a way to use the run button in IntelliJ to run the main class? – creampiedonut Jun 14 '18 at 13:41
  • To run the project with the run button you have either do the right click thing in the first comment or define the maven target "exec:java" as configuration. Once executed the configuration will be stored and the run button should act as desired – Oliver Tazl Jun 14 '18 at 14:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173155/discussion-between-oliver-tazl-and-creampiedonut). – Oliver Tazl Jun 14 '18 at 14:07