im using Struts 1.2.x and Ibatis 2.x version for development, so i finish yesterday and now i want to perform test this is my first time trying to work with JUnit, I already make test but in JavaApp not running on server, so how can I simulate or generate mocks with server behavior, and wich mocks are recommended for Struts and Ibatis built-in Environment?
for example how can i set accerts for login screen?
I know about StrutsTestCase im using it, and about Cactus are for containers and mocking for non containers scenaries, i want to view a demo using struts and ibatis cause I dont know how to retrieve data from mapping. Thanks
sorry about my English
Thanks in advance!
Have you looked at HttpUnit? I tried it for Servlet testing, completely different from Struts Actions and such - I know, but it had some decent tutorials.
You also might want to look at StrutsTestCase for JUnit. That project should be a sufficient start for unit testing struts. It also mentions Cactus, which is a framework for testing web applications on the server side.
Actually it all depends on how far you want to go with testing. You probably should have started with writing a test first, Test Driven Design you know ;) It just works. Not that I do it all the time...
Related
I have a java/jersey api that is called from the front end. I need to write tests for the java code. How the code is written is:
1. The api call executes the resource method, this calls a separate method that gets data from db and returns to the resource method. This then returns a javax.ws.rs.core.Response to the client.
This is going to be my first time writing tests, so please answer considering I know nothing. What is the best way to start here? And what types of tests should I write. Unit tests are what I’m aiming for here.
Now I have done a lot of research here and I’m leaning towards using JUnit + Mockito to do this. But how do I check for the data in a Response object?
And how should I check the other file that is getting data from db? I found out DBUnit that can do that, but do I need it?
Another framework I came across was Rest Assured. Do I need to include that also? Or can the same things be done with JUnit/Mockito?
I just want some direction from people who have tested out jersey api’s. And want to know what is the most common way to do this.
I do not think there is a best way to do this, what you need to test is often subjective and dependent on the context.
However, you can structure your code in a way that the most important is tested easily and what's left (integration) can be done later / with different tools.
What I suggest here is to follow the principles of the hexagonal architecture. The idea is to keep at the center of your application and without any kind of dependencies (imports ...) to any framework (jaxrs, jpa, etc.) all business rules. These rules can be easily designed with TDD. You will then have very short running tests. It may be necessary to use Mockito to mock implementations of SPI interfaces.
In a second time, you can use this "core" by wiring adapters to the outer world (HTTP, databases, AMQP, etc.), using API and implementing SPI interfaces.
If you want to test these adapters, you exit the scope of unit-tests, and write integration-tests. Integration with a framework, a protocol, anything really.
This kind of tests can use a wide variety of tools, from a framework-related mock (like Jersey test framework), in-memory database (like H2), to fully operational middleware instance using tools like testcontainers.
What is important to remember when writing integration-tests is they are slow in regards of unit-tests. In order to keep a feedback-loop as short as possible, you will want to limit the number of integration-tests to a minimum.
Hoping this will help you!
What sort of a framework can be used to test a jsp web application?
All java classes and the servrlets needs to be tested too. What would be the best approach and related frameworks that can be used in this regard?
Update
Any other suggestions?
For the Java part I still recommend JUnit as Unit-Test-Framework.
For the test of the web part one can think about using a web test framework like Selenium. However, this part is more difficult to automate during (for example) a nightly build.
You can test your application front end with selinium webdriver automation testing.
you can test your java code with junit framework or
testNg now a days becoming more popular in the market
JUnit was selected as the testing framework for java components. Still looking for a method to test application front end?
There is a tool called JspTester (http://jsptester.com) which integrates as a servlet to your webapp, and allows to inject values directly to beans, model objects, request parameters and session variables. Useful when there is a need to test the JSPs in isolation from business logic or MVC controllers.
We are currently developing a trade project with Richfaces 3.0, Seam 2.2 and JBOSS 6.0. And we are using Selenium for our GUI tests. I just want to know if there is any possibilty to access beans in selenium tests.
No, Selenium and Seam beans are a completely different level of abstraction. Seam beans are running inside a JBoss server while Selenium works on top of a web browser.
If you want to somehow control the application from the inside, you must provide some interface for these beans that is accessible via Selenium test. For instance you might expose some operations as web services or JMX beans and access them from Selenium test suite if written in Java.
Note however that this is not the best practice - Selenium tests should only work on user interface (end-to-end) level. Try to setup your application only via the user interface rather than manually accessing application internals.
UPDATE: If you have some common setup (like users, products, etc.), insert them in your database as part of your common deployment infrastructure. Then you can have a single test for creating/accepting a user, adding product, etc. and then simply reuse the common users already existing in the database.
There's nothing technically stopping you from allowing selenium visibility to your beans and call methods on them directly. However, it won't be a good design practice. Selenium is for testing the behaviour of your applications (through GUI, mostly) and should not be concerned with bean level. Maybe if you give us a use case of why you may need to do so it may make sense?
In the past I have exposed web services and JDBC tests via selenium as a shortcut for QA people to test certain parts of the application, if that's what you are talking about, but it may be best to use it via web services as Tomasz mentioned.
When asking this I answered myself: use a jsp.
But ok, what about a lightweight, easy to use framework?
It would be perfect if this framework had Eclipse plugin,
so that I could generate all code in one click and simply fill one method body (populate it with the data), then in one click
create a war file using a wizard
and deploy it on Tomcat.
Are there such frameworks?
p.s. I use spring, but I think there are ways to integrate it into every framework.
Take a look at Spring Roo.
If you only want to list a few DB records, I would just implement it dirty with a simple JSP with scriptlets. But you could also use the JSP SQL Tag library for that.
A simple one method body controller that needs that forwards to a view can most easily be writen with the Stripes framework that forwards to a JSP view (see wikipedia code example for how easy this can be done).
Generating nice tables in JSP can quickly be done by the popular Display Tag library.
If your not afraid to learn something new take a look at Grails
One of the big advantages of Grails is that its a full stack environment. Meaning that out of the box you get everything you need for your web app development
Test Servlet container (Tomcat)
Test database (hypersonic)
ORM (hibernate)
etc.....
Once installed just run
> grails create-app
Then you have a complete runnable application. (Auto bootstraps your project creation and config)
What if you don't want to start a separate project for grails but instead sneak it into an existing webapp?
I have to build an admin interface/crud for some new entities and thought it would be a perfect way to learn grails.
I'm trying to make one application with a Grails app and a Spring app.
I've tried to sneak the Grails App into the Spring one, but this is "impossible". It's easier to sneak the Spring app into the Grails app. Grails knows what Spring is, but Spring has no idea of what Grails is.
In this article you can find useful information about how to use your hibernate mapping files or annotations in Grails, so you don't have to remap everything. Also you can use all your java clases (put them into src/java). You can put the beans defined in the ApplicationContext.xml in conf/spring/resources.xml. You can leave them in ApplicationContext, but I've had some problems.
I don't have ended the job (almost) and it looks good.
It would be hard to "sneak it in" unless the existing app has the correct dir structure that maps exactly to how grails likes it - after all, convention over config is where the power of grails comes from.
You can try doing the admin interface as a "seperate" app to the original/existing spring app, and map the existing database to the grails domain objects. though i m not sure how you would run them side by side easily without more information on the existing app. It is possible definitely though.
I agree that building your admin interface is a good exercise to learn Grails, and also agree with the previous answer that Grails is difficult if not impossible to integrate with an existing Spring application. You could probably get it done, but the headache would not be worth it.
Grails is built on top of Hibernate for its ORM, so if you're already using Hibernate with this Spring app you can work this to your advantage. It's not too difficult to configure a Grails app to use pre-existing Hibernate models, and this is explained well in Grails documentation.
So, I'd recommend building up your admin console as an independent Grails app but make use of the Hibernate models you already have, if in fact you've used Hibernate.