Maven doesn't find org.junit even though it's in the dependencies

前端 未结 3 1539
孤城傲影
孤城傲影 2021-02-05 03:36

I wanted to add a test to my small project (please note I removed some bits from the code & changed package names, so if there\'s any error you see regarding this it might b

相关标签:
3条回答
  • 2021-02-05 03:53

    Just putting image to @matts Answer, so its easy for other to correct the problems.

    <sourceDirectory>src</sourceDirectory>
    

    just delete or comment this line present in pom.xml

    <!--  <sourceDirectory>src</sourceDirectory> !-->
    

    For explanation please go through @matt's answer

    0 讨论(0)
  • 2021-02-05 03:59

    You shouldn't override your <sourceDirectory> setting in the POM's <build> element unless you have a good reason to. This attribute determines where Maven looks for non-test code. The default value for this attribute is src/main/java. The <testSourceDirectory> attribute sets the path to test code (this defaults to src/test/java. By setting the <sourceDirectory> to simply src, Maven considers that entire directory to contain main application code. Since the src directory contains src/test/java, Maven then tries to compile your test code as part of the main application.

    This is significant because when compiling the main application (during the compile phase), Maven omits dependencies with test scope. Test code is compiled in a separate phase (the test-compile phase) after the main compile.

    So since Maven tried to compile your test code as part of the main application, it omitted the junit dependency, and they weren't available on the classpath. The solution here is to simply not specify the <sourceDirectory> element in the POM.

    0 讨论(0)
  • 2021-02-05 03:59

    By default , maven looks at these folders for java and test classes respectively - src/main/java and src/test/java

    When the src is specified with the test classes under source and the scope for junit dependency in pom.xml is mentioned as test - org.unit will not be found by maven.

    0 讨论(0)
提交回复
热议问题