nus-cs2103-AY2122S1 / forum

19 stars 2 forks source link

Getting "java: package org.junit.jupiter.api does not exist" error when running junit test #88

Closed yyhangz closed 3 years ago

yyhangz commented 3 years ago

Edit: Did not realize that #87 was created in the same time period as when I was opening this issue, and is referring to the same problem. Do note that the issues are similar but with the slight difference that I am adding Junit 5.7.0 instead.

Edit 2: CLOSING COMMENTS: To anyone who is finding this thread in the closed issues section, do refer to #86 and #87 as well, as others have shared alternative solutions to a similar problem on those threads.

After following the steps mentioned in https://se-education.org/guides/tutorials/junit.html for JUnit testing, I am unable to run DukeTest, and getting a java: package org.junit.jupiter.api does not exist error instead.

The things I have tried so far:

My development environment is as follows:

Here's a log of my build attempt when running the DukeTest class shown in https://se-education.org/guides/tutorials/junit.html:

Running Ant tasks...
Running 'before' tasks
Checking sources
Parsing java... [tests of test]
java: package org.junit.jupiter.api does not exist
java: package org.junit.jupiter.api does not exist
java: static import only from classes and interfaces
java: cannot find symbol
  symbol:   class Test
  location: class DukeTest
java: cannot find symbol
  symbol:   method assertEquals(int,int)
  location: class DukeTest
Checking dependencies... [tests of test]
Dependency analysis found 0 affected files
Errors occurred while compiling module 'tests of test'
javac 11.0.8 was used to compile java sources
Finished, saving caches...
Module 'test' was fully rebuilt due to project configuration/dependencies changes
Compilation failed: errors: 5; warnings: 0

This is what my Project Structure -> Dependencies window looks like image

Any suggestions will be very much appreciated!

onnwards commented 3 years ago

Hey @yyhangz, do check out my comment on #87 here and see if it works for you!

Jh123x commented 3 years ago

Hi @yyhangz I'm from your senior batch, just dropping by to help with tech issues.

Can you try to add this under the dependencies section of your build.gradle file.

dependencies {
    testCompile 'junit:junit:5.5.0'
    compile 'junit:junit:5.5.0'
}

Source: here

If that does not work maybe try to open Module Settings for my project and manually add jar Dependencies junit.jar (which are stored in the IntelliJ installation lib directory.)

Btw can I check if your tests folder is marked as test source?

damithc commented 3 years ago

Can you try to add this under the dependencies section of your build.gradle file.

@Jh123x this batch hasn't started using Gradle yet.

Jh123x commented 3 years ago

Can you try to add this under the dependencies section of your build.gradle file.

@Jh123x this batch hasn't started using Gradle yet.

Oh ok let me try cloning is repo and trying

Jh123x commented 3 years ago

Did you mark the test directory as Test Sources Root What is the version of junit you are using? (Managed to get it working on the master branch of your repo)

yyhangz commented 3 years ago

Hi @yyhangz I'm from your senior batch, just dropping by to help with tech issues.

Can you try to add this under the dependencies section of your build.gradle file.

dependencies {
    testCompile 'junit:junit:5.5.0'
    compile 'junit:junit:5.5.0'
}

Source: here

If that does not work maybe try to open Module Settings for my project and manually add jar Dependencies junit.jar (which are stored in the IntelliJ installation lib directory.)

Btw can I check if your tests folder is marked as test source?

Hi @Jh123x thanks for the reply. I have marked the java folder under [project root]/src/test/java as the test source rather than the test folder, as per the steps of the guide provided to our batch.

I understand that you are cloning my repo and trying it out on your local env. Thanks for the help, I really appreciate it! I have yet to push anything regarding Junit testing to my master branch. But to summarize the steps I took, from my latest github commit, I basically followed the steps of the guide https://se-education.org/guides/tutorials/junit.html, right until step 7.2.

According to the Intellij Hints, I am adding Junit 5.7.0.

yyhangz commented 3 years ago

What is the version of junit you are using? (Managed to get it working on the master branch of your repo)

@Jh123x Do you mind if I ask about your local development environment details?

Jh123x commented 3 years ago

What is the version of junit you are using? (Managed to get it working on the master branch of your repo)

@Jh123x Do you mind if I ask about your local development environment details?

Yep sure ask away

yyhangz commented 3 years ago

What is the version of junit you are using? (Managed to get it working on the master branch of your repo)

@Jh123x Do you mind if I ask about your local development environment details?

Yep sure ask away

Thanks! Specifically, your OS, intellij version, java version and the Junit version.

Jh123x commented 3 years ago

What is the version of junit you are using? (Managed to get it working on the master branch of your repo)

@Jh123x Do you mind if I ask about your local development environment details?

Yep sure ask away

Thanks! Specifically, your OS, intellij version, java version and the Junit version.

OS: Windows 10 Intellij Version: 2021.2.1 Java Version: 11.0.12 Junit Version: 5.7.0

yyhangz commented 3 years ago

Hey @yyhangz, do check out my comment on #87 here and see if it works for you!

Hi @onnwards, thanks for the reply. I have tried your suggestion but I am facing the same issue that @felissaf mentioned WRT the suggested solution that you linked.

Jh123x commented 3 years ago

Can I see your ip.iml file?

yyhangz commented 3 years ago

Can I see your ip.iml file?

Sure. I don't think github comments allow .iml file types, so I shall just paste the contents below.


<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>```
Jh123x commented 3 years ago

Can I see your ip.iml file?

Sure. I don't think github comments allow .iml file types, so I shall just paste the contents below.

<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>```

Can you try adding the src/test as the test source root within your intellij and re add the classpath for Junit?

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
    </content>
    <orderEntry type="jdk" jdkName="11" jdkType="JavaSDK" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library" scope="TEST">
      <library name="JUnit5.7.0">
        <CLASSES>
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.7.0/junit-jupiter-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.7.0/junit-jupiter-api-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.7.0/junit-platform-commons-1.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.7.0/junit-jupiter-params-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.7.0/junit-jupiter-engine-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.7.0/junit-platform-engine-1.7.0.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>

This is the one that i got after cloning yours

yyhangz commented 3 years ago

Can I see your ip.iml file?

Sure. I don't think github comments allow .iml file types, so I shall just paste the contents below.

<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>

Can you try adding the src/test as the test source root within your intellij and re add the classpath for Junit?

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
    </content>
    <orderEntry type="jdk" jdkName="11" jdkType="JavaSDK" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library" scope="TEST">
      <library name="JUnit5.7.0">
        <CLASSES>
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.7.0/junit-jupiter-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.7.0/junit-jupiter-api-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.7.0/junit-platform-commons-1.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.7.0/junit-jupiter-params-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.7.0/junit-jupiter-engine-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.7.0/junit-platform-engine-1.7.0.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>

This is the one that i got after cloning yours

So should I remove the [project root]/src/test/java directory as test source and mark [project root/]src/test as test source instead?

Jh123x commented 3 years ago

Can I see your ip.iml file?

Sure. I don't think github comments allow .iml file types, so I shall just paste the contents below.

<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>```

Can you try adding the src/test as the test source root within your intellij and re add the classpath for Junit?

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
    </content>
    <orderEntry type="jdk" jdkName="11" jdkType="JavaSDK" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library" scope="TEST">
      <library name="JUnit5.7.0">
        <CLASSES>
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.7.0/junit-jupiter-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.7.0/junit-jupiter-api-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.7.0/junit-platform-commons-1.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.7.0/junit-jupiter-params-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.7.0/junit-jupiter-engine-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.7.0/junit-platform-engine-1.7.0.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>

This is the one that i got after cloning yours

So should I remove the [project root]/src/test/java directory as test source and mark [project root/]src/test as test source instead?

Maybe make a backup of your current one and try using the .iml file i got?

yyhangz commented 3 years ago

@Jh123x I have tried using the .iml file that you provided but I am still getiing the same errors.

@damithc some seniors have commented on gradle dependencies. Would it be possible for me (and others who might be facing the same issue) to push forward the use of gradle to see if we can resolve this issue this way, and if so, how should we do it?

Jh123x commented 3 years ago

@Jh123x I have tried using the .iml file that you provided but I am still getiing the same errors.

@damithc some seniors have commented on gradle dependencies. Would it be possible for me (and others who might be facing the same issue) to push forward the use of gradle to see if we can resolve this issue this way, and if so, how should we do it?

Try pasting in <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" /> and replace the library part (This one)

      <library>
        <CLASSES>
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>

with

      <library name="JUnit5.7.0">
        <CLASSES>
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.7.0/junit-jupiter-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.7.0/junit-jupiter-api-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.7.0/junit-platform-commons-1.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.7.0/junit-jupiter-params-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.7.0/junit-jupiter-engine-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.7.0/junit-platform-engine-1.7.0.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>

Instead of replacing the whole thing?

felissaf commented 3 years ago

Hi @Jh123x @yyhangz I was the one who opened issue #87 and I managed to solve the issue by copy pasting the junit part to my .iml file! I hope this works for you as well

damithc commented 3 years ago

@damithc some seniors have commented on gradle dependencies. Would it be possible for me (and others who might be facing the same issue) to push forward the use of gradle to see if we can resolve this issue this way, and if so, how should we do it?

@yyhangz If you are willing to try Gradle early, you can follow the A-Gradle instructions given in https://nus-cs2103-ay2122s1.github.io/website/schedule/week4/project.html#4-add-increments-as-branches-a-gradle-a-checkstyle-level-10-a-varargs

yyhangz commented 3 years ago

CLOSING COMMENTS: To anyone who is finding this thread in the closed issues section, do refer to #86 and #87 as well, as others have shared alternative solutions to a similar problem on those threads.

@Jh123x I have tried using the .iml file that you provided but I am still getiing the same errors. @damithc some seniors have commented on gradle dependencies. Would it be possible for me (and others who might be facing the same issue) to push forward the use of gradle to see if we can resolve this issue this way, and if so, how should we do it?

Try pasting in <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" /> and replace the library part (This one)

      <library>
        <CLASSES>
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>

with

      <library name="JUnit5.7.0">
        <CLASSES>
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.7.0/junit-jupiter-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.7.0/junit-jupiter-api-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.7.0/junit-platform-commons-1.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.7.0/junit-jupiter-params-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.7.0/junit-jupiter-engine-5.7.0.jar!/" />
          <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.7.0/junit-platform-engine-1.7.0.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>

Instead of replacing the whole thing?

@Jh123x replacing the section of ip.iml that you highlighted did not work for me. However, you mentioned that you were developing on windows 10 natively while I was using WSL. I cloned the repo onto the native windows env and worked on that repo instead, which solved the issue for me.

Thanks for the help so far, I really appreciate it!