Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily test java classes?

Tags:

java

eclipse

We have an existing Tomcat/Linux/Servlet/Java web server but we no longer have the developer. We have C#/Windows code (desktop app) that does the same kind of computation/data manipulation as the web site. I'm investigating why the two give slightly different results.

To achieve this, I would like to write a small java test exe that calls the code with specific input data and spits out the output + some debug info.

My main problem is, since I'm total newbie regarding Java: Where do I start to write that exe and call the existing classes (instead of having Tomcat somehow calls these classes) ?

I have a Ubuntu VM with Eclipse installed. The Java source code is installed and compiles.

My question mostly relates about how to setup a exe program project in Eclipse and setup the links necessery to call these exsting classes. It's not about the Java language itself (I know C#, which after all was inspired by Java)

TIA,

like image 941
Serge Wautier Avatar asked Dec 01 '25 17:12

Serge Wautier


2 Answers

You should be fine just doing some JUnit (pretty much the same as NUnit) tests.

You can add a new package with references to the other classes packages' and write the tests for them.

For more info you can check some JUnit docs

If you need to test your servlets a neat way is with Jetty + Junit. More info on the subject can be found here

like image 80
mcabral Avatar answered Dec 03 '25 07:12

mcabral


You do not create exe's in java (although its not impossible) what you do is create Jar files. You then execute these jar files. What you will want is JUnit testing framework.

You can then write your tests in eclipse and execute them in eclipse. The tests will run like a standalone application. You can also write your own application which outputs test results to logging if you'd prefer using a standalone application with a main method. For a a common logging solution see log4j which is really easy to use. Debugging mode in eclipse is great for interrogating the data if you think something isn't working correctly.

On a final note you might also find using mockito useful which means you can mock up objects with set functionality. For example creating mock HTTPrequests or respones.

like image 31
DeliveryNinja Avatar answered Dec 03 '25 07:12

DeliveryNinja



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!