Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running tests using maven displays classpath problems

I am attempting to run some unit tests on my spring web app using Maven. The app installs and runs fine without tests.

I receive this error in my surefire test report :

java.lang.NoClassDefFoundError: javax/servlet/ServletException

The test itself looks like this :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:C:/myProjects/myWebapp/src/main/webapp/WEB-INF/applicationContext-test.xml"})
@Transactional
public class MyTest {
...

Previously I was receiving this error, however I removed any reference to the security package from test app context. And got this latest error.

Both problems are caused by my classpath not being set correctly ? The maven dependencies do not seem to be included when testing ? How can I change this, or what else am I missing ?

like image 872
NimChimpsky Avatar asked May 13 '26 22:05

NimChimpsky


1 Answers

Try this.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>test</scope>
</dependency>

Your servlet container will have this but some of your tests must need it. You may need to adjust the version.

The scope is important. You don't want to bundle this jar into your war but you want it available to your tests.

like image 92
James DW Avatar answered May 15 '26 11:05

James DW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!