Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How do I test a Servlet using static void main(String[] args) ?

 
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I find it a chore to test run my servlet webApp and with the debugger always not working etc I have to spend considerable amount(sometime 1 months !!!) of time whenever it breaks down.

Thus, I would like to know if it is possible to test my servlet without the jsp etc... I just put in the parameters myself manually.

Hope someone can give me some simple example.

Tks.
 
tangara goh
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far, I managed to find this tutorial - https://www.tutorialspoint.com/jdbc/jdbc-insert-records.htm but then I am getting the following error :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at testSubjectIdInserts.testWork.main(testWork.java:27)

Hope someone can tell me why? Tks
 
Saloon Keeper
Posts: 7169
165
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Web apps need a servlet environment to run; a command line will not do. For unit tests involving servlet code you may get away with mock objects.

But let's take a step back: how can trying to debug an issue with a web app take a whole month? If the debugger built into the IDE (assuming you're using one) is too cumbersome, try a standalone one like JSwat. I've been using that to debug apps running on Tomcat, and it works fine.

The JDBC issue means the JDBC driver is not in the classpath. You need to include the appropriate jar file in whichever way is appropriate for however you run the code. If that's on the command line, something like "java -cp .;mysql-connector-java-5.1.47.jar JDBCExample" might do the trick.
 
Saloon Keeper
Posts: 24536
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are actual servlet test frameworks. Or you can launch a webapp server with debugging enabled and attach a Java debugger to it.

The best way to test a servlet, however, is to move as much of the code as possible into POJOs and test the POJOs. Stuff that requires actual access to HttpSession, Servlet Request and Response objects, JNDI, and other JEE facilities can't be tested that way, but at least you can test your business logic without the slow cumbersome business of launching a servlet framework.

And servlets should not be obtaining JDBC drivers in any case. Connection pools are more efficient and more portable.
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is also an anti-pattern to be being too much work in the servlet itself. If you delegate responsibilities to other classes that do not depend on the servlet environment to run, they are not only more testable, it's just better design.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using maven? You can use cargo and start tomcat and your app by running your integration tests against your running application.

You should be doing something like this anyway. Are you writing integration tests?

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic