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
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
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.
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.