开发者

How to easily test java classes?

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 s开发者_如何学Gotart 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,


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


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.


Eclipse has built in support for JUnit, which is the defacto unit testing tool for Java. If you need support for Servlets or whatever web framework the application is built in, there are frameworks that simulate the web server for you. ServletTester being one of them.


JUnit is Good, In General

I agree that JUnit and mockito are excellent starting points. I absolutely love mockito. Personally, I believe it serves as an example of how all frameworks should be--it's easy to use, quick to learn and highly functional.

However, I would recommend that you look into just debugging the server, directly through eclipse.


Debugging is Better (in this case)

Basically, you can set tomcat to break during execution and step through your code. While in debug mode, in eclipse, you can easily inspect variables, run expressions and jump through the call stack. When it comes to actually fixing a problem, debugging is the way to go. I think it would be the best method to determine exactly why you're getting different results with similar code.


Quick Link and Help

Here is a helpful blog post on how to setup tomcat for debugging inside Eclipse. I wrote it to address working with BlazeDS but the same basic steps apply to any Tomcat installation. If you have any questions, let me know and I could try to assist.

Hope that helps in some way,

  • gMale


[Note: I'm the OP]

Thanks to all for your help. Much insightful info in there. It's hard to mark one as better than others.

I realized that I can simply throw a main() in any class and type Shift+Alt+X , J to execute that main() in Eclipse. Although very dirty, this solution is also very quick and simple to feed a method or 2 with some input data and print some output to the console.

Obviously, it's not as powerful as JUnit but it's certainly faster to get started with, especially in my environment where all I need is make a couple of ad-hoc tests and then discard the whole stuff.

Thanks again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜