Search code examples
javamavenjunit

Running maven clean test (junit) is uanable to find compiled class


New to java land and maven as well as junit. I have a very simple maven project with structure like

│   .classpath
│   .project
│   LICENSE.txt
│   NOTICE.txt
│   pom.xml
│   README.txt
├───site
└───src
    ├───main
    │   ├───java
    │   │   └───com
    │   │       └───daydreamer
    │   │           └───app
    │   │                   TestApp.java
    └───test
        ├───java
        │   └───com
        │       └───daydreamer
        │           └───app
        │                   TestAppTest.java

and pom.xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<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>com.daydreamer.app</groupId>
  <artifactId>testpkg</artifactId>
  <version>1</version>

  <properties>
    <maven.compiler.release>11</maven.compiler.release>
    <junit.version>5.8.2</junit.version>
  </properties>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.22.2</version>
      </plugin>
    </plugins>
  </build>

  <dependencies>

    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <version>1.8.2</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

</project>

main source code TestApp.java as follows, please pay attention to the package statement.

package com.daydreamer.app;

public class TestApp {

    // Fields
    private int a;

    // Constructor
    public TestApp(int a) {
        this.a = a;
    }

    // Getters
    public int getA() {
        return this.a;
    }

    // Setters

    // String represntation
    @Override
    public String toString() {
        String strRepr = "TestApp {" + this.a + "}";
        return strRepr;
    }
}

and when I run mvn clean compile, this works perfectly and I am able to locate compiled class at ./target/classes/com/daydreamer/app/TestApp.class.

The issue arises when I am running tests. My TestAppTest.java looks like below,

import com.daydreamer.app.TestApp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


public class TestAppTest {

    @Test
    void getATest0() {
        var expected = 1;
        var actual = TestApp(1).getA();
        Assertions.assertEquals(actual, expected);
    }

    @Test
    void toStringTest1() {
        var expected = "1";
        var actual = TestApp(1).toString();
        Assertions.assertEquals(actual, expected);
    }
}

When I run mvn clean test it raises exception

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:testCompile (default-testCompile) on project testpkg: Compilation failure: Compilation failure:
[ERROR] ./src/test/java/com/daydreamer/app/TestAppTest.java:[11,22] cannot find symbol
[ERROR]   symbol:   method TestApp(int)
[ERROR]   location: class TestAppTest
[ERROR] ./src/test/java/com/daydreamer/app/TestAppTest.java:[18,22] cannot find symbol
[ERROR]   symbol:   method TestApp(int)
[ERROR]   location: class TestAppTest
[ERROR] -> [Help 1]
[ERROR]

Also, my LSP displays the declared package "" does not match the expected package "com.daydreamer.app".

and I am not able to resolve the issues. I checked main source file, test file and pom.xml. Fix package names, remove package etc. etc. but I am unable to resolve the issue.

Any help would be appreciated.

Cheers, DD


Solution

  • Your creating an instance of TestApp without the 'new' keyword. Initiate your variables like this:

    var actual = new TestApp(1).toString();
    var actual = new TestApp(1).getA();
    

    After making this change make sure to rebuild your project.

    And just a small note, the first value in assetEquals should be 'expected' and second 'actual'.

    Assertions.assertEquals(expected, actual);