Reputation: 379
This is for some high level Web/Rest tests of my whole system that are being powered by Jetty/Spring.
What I am trying to do is to have a completely self contained test that,
What I am trying to do is have the whole thing running in a single JVM, i.e. I'm starting a org.eclipse.jetty.server.Server.
This is because its easy, and avoids the test having outside dependencies (like starting Jetty)
This is all cool, but I hate that I have to check the DB manually ... I want to use my DAO (or maybe service layer) classes so I'm not re-writing DB code.
Since my DAO's have already been started by Spring in the Jetty instance in the same JVM as the test case, I want to grab that ApplicationContext and then pull my DAO beans out.
I'm a bit stuck getting the ApplicationContext since the test isn't a "Spring" test per say.
People got any ideas on how to do this?
Upvotes: 2
Views: 1942
Reputation: 32417
@Ralph's answer is the best - use a separate spring context for the tests. Ideally, you should refactor out all the DAO beans and their dependencies into a separate dao-beans.xml
that you can include in your main app context and your test context..
Alternatively, you can use Spring Remoting to export the DAO beans from the Jetty server via RMI or HTTP by adding a ServiceExporter bean to your (real) app context
<bean name="daoExporter"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<ref bean="dao" />
</property>
<property name="serviceInterface" value="com.example.IDao" />
</bean>
and a custom Spring remoting servlet in web.xml
<servlet>
<servlet-name>daoServiceExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>daoServiceExporter</servlet-name>
<url-pattern>/remoting/dao</url-pattern>
</servlet-mapping>
then importing it into your test context
<bean id="dao" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceInterface" value="com.example.IDao" />
<property name="serviceUrl" value="http://localhost:8080/remoting/dao"/>
</bean>
This is only a better answer if e.g. it takes a long time for your Spring context to load, or maybe you have some beans that must remain singletons.
Upvotes: 2
Reputation: 120811
I think it is not possible to access the spring context in jetty if it is start normally, may it could work if you use some embedded server.
But I think there are much simpler solutions:
Upvotes: 2