0

I been trying for the past 3 hours to try to get a single MyTest.java file to run. But no matter what I try, Maven seems to want to skip over my test and tell me BUILD SUCCESS. But I wasn't try to build anything. I was just trying to "Maven Test".

When I run MyTest.java by itself, it runs fine , but for some reason Maven wants to skip over it.

This is what I wrote.

package SeleniumT;

import org.testng.annotations.Test;

public class MyTest {

    @Test
    public void login() {
        //return "sample";
        System.out.println("########This is my first Maven project#######");
    }
}

My console output keeps saying there is nothing to compile, which makes no sense.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-demo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.287 s
[INFO] Finished at: 2018-02-15T17:27:11-05:00
[INFO] Final Memory: 10M/245M
[INFO] ------------------------------------------------------------------------

My POM file has all the dependencies required to run this. I don't have Maven Surefire plugin I think, but that shouldn't cause a issue, I am assuming.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SeleniumT</groupId>
  <artifactId>maven-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.43.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.8</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

What could be causing Maven to skip my simple Java test?

Maven folder structure

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Bekind
  • 55
  • 1
  • 13
  • Where is the class in the file system? take a read of https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html – Taylor Feb 15 '18 at 22:47
  • Thanks for your response Taylor. I took a look at the guide. But I am not sure what you mean by class in the file system. When you say "file system", do you mean the pom.xml file? I didn't know I had to add the name of each test in the pom.xml. – Bekind Feb 15 '18 at 22:50
  • What folder is the class in? – Taylor Feb 15 '18 at 22:50
  • It's in the [ src/test/java ] folder. I have uploaded a picture of the folder structure. I thought that was the right folder. – Bekind Feb 15 '18 at 23:05
  • What are you typing on the command line? – MivaScott Feb 15 '18 at 23:12
  • Thanks for your response MivaScott. I am not using a command prompt . Unfortunately, I am not that advanced yet. I am right clicking on "maven-demo" workspace and selecting "Run as" and selecting "Maven test". – Bekind Feb 15 '18 at 23:21
  • 1
    Dont' you get an error in Eclipse regarding your test class's file name and class name? [If your (public) class is called `MyTest` the file name has to be `MyTest.java`](https://stackoverflow.com/q/2134784/1744774). The message "`Nothing to compile - all classes are up to date`" is OK since Maven recognizes if something has been changed in your source files since the last successful build. If not, no (useless) compile is performed and this message is printed. – Gerold Broser Feb 15 '18 at 23:47
  • [The `test` goal of the Surefire plugin is bound to Maven's `test` phase by default.](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings). So, if you invoke the `test` phase (with `mvn test` or via an Eclipse Run Configuration) `surefire:test` is run automatically. – Gerold Broser Feb 15 '18 at 23:58
  • Thanks for your response Gerold. I was able to get it to work. But now I am facing a new challenge. I copied and pasted the exact same tests multiple times . But for some reason it's not running all the tests, unless I restart "eclipse". I have a good machine with 16gb ram and i7 processor. Why wouldn't it run all the tests? – Bekind Feb 16 '18 at 00:15
  • Ignore the last part. It wasn't running because the test name didn't end with the word "test". Now it does. Thanks everyone! – Bekind Feb 16 '18 at 00:43

1 Answers1

1

First of all, I noticed your java file is called apptest.java but your actual public class definition is named MyTest. Rename the java file to MyTest.java. Such a difference usually raises compilations error, what makes me believe you Eclipse project is not set to Build automatically.

If that's not the problem, maybe the Maven Test option you have invoked is not invoking test goal. Concerning the "Nothing to compile" message, I'd suggest you to always perform a clean so maven get rid of previously compiled classes and build it from scratch again.

Summing up: right click on the project > Run As > Maven Build... > Type clean test on the Goals field > Click Run. Check if something has changed.

If you succed, the next time you need to run this again, this run configuration you've just created will be already listed on the Run As > Maven build menu.

Rafael Odon
  • 1,211
  • 14
  • 20
  • Sorry, I missed your last comment where It seems you found the solution yourself. I hope my answer helps someone else if it get here for other reasons. :') – Rafael Odon Feb 16 '18 at 10:19