• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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

 
Ranch Hand
Posts: 468
  • Mark post as helpful
  • send pies
  • 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: 468
  • Mark post as helpful
  • send pies
  • 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: 5132
135
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Bartender
Posts: 20125
103
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Marshal
Posts: 67197
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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?

 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!