Run Java Spring application from eclipse - java

I'm working on a project at work that runs on the spring framework and requires a connection to an oracle database at all times. When I want to test a new method I have to stop my server, rebuild, start the server, then launch my application.
My question is is there any way to run my application without having to launch it every time? I'm okay with having to restart the server but I'm trying to eliminate launching the application every time.
Cheers.

What you seek are integration tests. You need to break your application into individual pieces and test their functionality against the oracle database bit by bit. These little pieces can be tested by certain testing technologies such as the popular JUnit.
All the pieces that you need should only depend on the data source and whatever other collaborating beans that are needed. Break your bean definitions apart such that they are small and only depend on very few beans, tracking back to the data source bean. You can then use JUnit (or whatever testing technology you'd like) and Spring's testing annotations to make small application contexts
See this section of the Spring reference manual for more information:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html
When you have tests, you don't actually run the application - you run a part of it to verify its behavior individually. You can then add tests that test the pieces together, and eventually your confidence in the application will rise.

Related

Spring IOC Bootstraping is it possible

My application is a standalone Java Application which uses Spring IoC. Bootstraping the application with ApplicationContext loads lets say 35 services in memory which are then used through out the JVM lifecycle instance of my application. This bootstraping requires about 6-7 minutes, which makes unit testing difficult.
This is the sequence of steps I have to do, which I am trying to avoid and still use Spring:
Bootstrap (Spring) and then actual business logic code.
Test code.
After testing I find something I want to change, which means I have to stop JVM, modify my code, start it up again, at this point Spring again takes about 6-7 minutes bootstraping the application.
How can test my modified code in the same JVM instance without being forced to restart the JVM?
There is no easy solution to this
I know three possible ways to avoid your problem:
Try mocking parts of your services. Especially on junit tests this is recommended praxis. In IOC ready architecture it should be easy. On manual testing you can use fake services.
Switch to OSgI architecture. This will allow you to start, stop and redeploy single services. But you will have to use an OSgI container and it will take a multiple of 6-7 minutes to switch.
Try something like JRebel which promises to eliminate problems of hot deployment (after 14 days evaluation you will have to buy it). But many times extended hot deploment wont help because you will have to clean up your application context.

Automating complete testing of Java EE web application

I have a doubt. Say I have a web application which is big and relies on Java/Java EE (JSP/Servlets).
Every time before a drop we test each and every functionality on GUI so that everything is working properly. Previously it was easy but now as the number of modules has increased exponentially, manually testing each and every GUI with required functionality is no more a feasible option.
I am on lookout for tools in which I can write my entire test case say about 1000 and then just run it once before the drop and it will list down all the test cases that have failed.
The tool preferably must be free to download and use it.
I dont know whether using
Arquilian
or
JUnit
in this regard will help or not but automating testing before the drop is really needed..
Please guide.
Use Junit together with a mock framework i.e Mockito to test units (service methods)
Use Arquillian to test on an integration level ( how different services, modules work together )
Use a database testing tool (i.e dbunit) to test your database / persistence layer)
Use Selenium to test your frontend
Test as much as possible.
Use Jenkins and Sonar to track your build process and your quality of tests and code
You should always test your application on different level. There is not just one solution.
Use unit testing to test small pieces of your application and to make refactoring as easy as possible.
Use integration test to check your modules still work together as expected.
Use GUI testing to check if your customers can work with your software.
If its relevant, think about performance testing (i.e. jmeter )
Definitively Selenium. Couple it with maven cause you will probably need to package your project specifically for testing purpose. Moreover maven allow you to launch a container during the integration-test phase and to close it automatically at the end. You can also configure this as a nightly build on jenkins / hudson so you will be quicly notified of any regression.

Is there any general integration test framework for console java application?

For example, I am going to write an application. Its core requirement is simple. It keeps scanning a folder. If new files arrive, it will transfer it to a FTP site. It will be written in JAVA as a console application.
I am wondering if there is integration test framework, like open source one, for this kind of application? It should be capable of testing the following sameple test cases.
Generate files, then check if these files are transferred to FTP.
If FTP is down, can the application report corresponding warning logs?
etc.
Thanks in advance.
JUnit combined with some mocking framework, like Mockito should get you a fair bit on your way. If you want to take it further, for instance having actual instances running of external service, you can use JUnit combined with, for instance, Cargo to start up Java EE containers as part of your test. Light-weight, in-memory databases like HSQLDB is often used as placeholder databases for integration tests.
I've heard some people using JBoss Arquillian, but haven't tried it myself. It's more for remote testing of Java EE components though, so might not fit your exact requirement.

Oracle DB testing strategy

At work we are trying to simplify an application that was coded with an overkill use of Spring Remoting. This is how it works today:
(Controllers) Spring MVC -> Spring Remoting -> Hibernate
Everything is deployed in a single machine, Spring Remoting is not needed (never will be needed) and adds complexity to code maintenance. We want it out.
How to ensure everything works after our changes? Today we have 0% code coverage! We thought on creating integration tests to our controllers so when we remove Spring Remoting they should behave exactly the same. We thought on using a mix of Spring Test framework in conjunction with DBUnit to bring up Oracle up to a known state every test cycle.
Has anyone tried a similar solution? What are the drawbacks? Would you suggest any better alternative?
It always depends on the ratio effort / benefit that you are willing to take. You can get an almost 100% code coverage if you are really diligent and thorough. But that might be overkill too, especially when it comes to maintaining those tests. But your idea is good. I've done this a couple of times before with medium applications. This is what you should do:
Be sure that you have a well known test data set in the database at the beginning of every test in the test suite (you mentioned that yourself)
Since you're using Hibernate, you might also try using HSQLDB as a substitute for Oracle. That way, your tests will run a lot faster.
Create lots of independent little test cases covering most of your most valued functionality. You can always allow yourself to have some minor bugs in some remote and unimportant corners of the application.
Make sure those test cases all run before the refactoring.
Make sure you have a reference system that will not be touched by the refactoring, in order to be able to add new test cases, in case you think of something only later
Start refactoring, and while refactoring run all relevant tests that could be broken by the current refactoring step. Run the complete test suite once a night using tools such as jenkins.
That should work. If your application is a web application, then I can only recommend selenium. It has a nice jenkins integration and you can create hundreds of test cases by just clicking through your application in the browser (those clicks are recorded and a Java/Groovy/other language test script is generated).
In our Spring MVC / Hibernate (using v3.4) web app we use an Oracle database for integration testing.
To ensure that our database is in a known state each time the test suites are run, we set the following property in our test suite's persistence.xml:
<property name="hibernate.hbm2ddl.auto" value="create"/>
This ensures that the db schema is created each time our tests are run based on the Hibernate annotations in our classes. To populate our database with a know data set, we added a file named import.sql to our classpath with the appropriate SQL inserts. If you have the above property set, Hibernate will run the statements in import.sql on your database after creating the schema.

Run JUnit Tests through web page

We would like to have a set of tests as part of our web application. The tests will be used for analyzing the health status of the application, so a support person or a scheduler can run the test to see if the application itself and various required remote systems are available.
I have seen this being done using some kind of webbased JUnit frontend, it allowed to run tests and reported the results as HTML. This would be great because the developers know JUnit, but I couldn't find the library in the intertubes.
Where can I find a library doing this?
You can use some free services to verify the availability of your system. Here are two that I've used:
mon.itor.us
pingdom
Another thing you can take a look at is JMeter, but it does not have a web UI.
Original answer:
Perhaps you mean functional tests (that can be run through JUnit). Take a look at
Selenium - it's web functional testing tool.
(Note that these are not unit tests. They don't test individual units of the code. Furthermore unit tests are executed at build time, not at runtime.)
Bozho is correct, these are not unit tests but I have done something similar. At my company I am not the one that ultimately deploys these things to our test environment or production environment. During development I create a couple of servlets that test things like it can get a valid database connection, it can hit our AD server etc. It than basically prints out a message and indicates success or failure.
That way when I have the code deployed to one of our environments, I can have the person deploying it hit the URL and make sure everything comes back OK. When I get ready to do the final deployment I just remove the servlet config.
If you already have a set of tests composed and ready to run, then Hudson can run those tests on a schedule and report on the results.
Update: If you're looking for a tool to check your servers and applications every few minutes for availability check out Nagios.
Maybe you mean some kind of acceptance test tool. If so, have a look at Fitnesse.
What you're probably looking for is CruiseControl.Net - it combines with NUnit/JUnit etc to make an automated testing framework with HTML reporting tools and a tray app for your desktop as well. I actually just downloaded it again an hour ago for a new role - it's really good.
It can be used to run anything from unit tests to getting files from source control, to kicking off compiler builds or rebooting servers (when used with NAnt - a .Net build tool).
You should look for a Continous Integration tool like Jenkin.

Categories