I'm trying to use Micronaut-jms to integrate ActiveMQ Artemis into my application. Everything worked perfectly until I started writing unit tests.
The issue I'm facing is that since my test's are annotated with #MicronautTest when I run a test the whole application boots up.
All would be fine except the application will look for ActiveMQ Artemis, and as it is embedded and has not started yet, the application will throw an error and the test will fail.
Here is a demo application.
How can I ensure that the embedded ActiveMQ Artemis is available for the application?
The test itself:
#MicronautTest
class DemoControllerSpec extends Specification {
#Shared
ActiveMQServer server
void setup() {
Configuration config = new ConfigurationImpl()
config.addAcceptorConfiguration("in-vm", "vm://0")
//config.addAcceptorConfiguration("tcp", "tcp://127.0.0.1:61616")
server = new ActiveMQServerImpl(config)
server.start()
}
void 'is server active'() {
expect:
server.active
}
}
Related
Our application is using sparkjava http://sparkjava.com/ as the REST framework. The jetty server is embedded in the application (sparkjava default). We are also using spring for dependency injection.
For providing AD authentication, we need to integrate the waffle's NegotiateSecurityFilter.
As per waffle documentation and several online resources including stackoverflow, a DelegatingFilterProxy is required with the name springSecurityFilterChain.
But since we are not using spring MVC, I have to add it as follows:
ServletContextHandler sparkContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
sparkContext.addFilter(new FilterHolder( new DelegatingFilterProxy( "springSecurityFilterChain" ) ),"/*", EnumSet.allOf( DispatcherType.class ));
And since a ContextLoaderListener does not already exist, need to add in the following manner:
sparkContext.addEventListener( new ContextLoaderListener() );
But it gives the error "Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader" at time of server startup.
Please let me know a solution in this scenario if you have successfully integrated spring-security DelegatingFilterProxy with embedded jetty and sparkjava (without using spring MVC).
This is how I achieved it finally:
In the main method where I have access to the sparkContext:
ServletContextHandler sparkContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
sparkContext.setContextPath("/");
sparkContext.addServlet(DefaultServlet.class, "/*");
addSPNEGOFilter(sparkContext);
And the implementing methods are as:
private void addSPNEGOFilter(ServletContextHandler sparkContext) {
final ServletHandler handler = new ServletHandler();
final FilterHolder fh = handler.addFilterWithMapping(NegotiateSecurityFilter.class, <SPNEGO_FILTER_PATH>,
EnumSet.allOf(DispatcherType.class));
setNegotiateFilterParams(fh);
sparkContext.addFilter(fh, <SPNEGO_FILTER_PATH>, EnumSet.allOf(DispatcherType.class));
}
Add the following properties to the holder:
private static void setNegotiateFilterParams(final FilterHolder fh) {
fh.setInitParameter("principalFormat", "fqn");
fh.setInitParameter("roleFormat", "both");
fh.setInitParameter("allowGuestLogin", "false");
fh.setInitParameter("impersonate", "false");
fh.setInitParameter("securityFilterProviders",
"waffle.servlet.spi.NegotiateSecurityFilterProvider");
fh.setInitParameter("waffle.servlet.spi.NegotiateSecurityFilterProvider/protocols", "Negotiate");
}
I am attempting to create some UI tests using SpecsFor MVC, I am coming at this from a new user's point of view in terms of testing so could be easily missing something obvious.
The site I'm testing against already uses it's own test DB so I do not need to create one. when I build and debug normally on this site it also starts up a couple of WCF projects which we use for service layer interactions. Presumably I'll need to start these in the specs config but have not got that far yet.
I've followed the documentation and have created this method:
protected override void AfterConfigurationApplied()
{
var config = new SpecsForMvcConfig();
config.UseIISExpress()
.With(Project.Named("TestSite"))
.UsePort(55555)
.CleanupPublishedFiles()
.UseMSBuildExecutableAt(#"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe");
config.BuildRoutesUsing(MvcApplication.RegisterRoutes);
config.UseBrowser(BrowserDriver.Chrome);
_host = new SpecsForIntegrationHost(config);
_host.Start();
}
The routing of the site is set in the Global.asax hence the setting in the above method.
I also have this very basic test in place to just see if I can get it working:
protected override void When()
{
SUT.NavigateTo<HomeController>(u => u.Index());
SUT.FindLinkTo<HomeController>(u => u.About())
.Click();
}
When I debug the tests it successfully starts Chrome but hangs for ages and eventually fails with this error:
OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:49924/session/bd15d6a15395b4ca204437c340639501/element timed out after 60 seconds.
I'm not sure where that port number or session etc are coming from.
If I'm running my web project normally (outside of this whole testing project) I see a URL like this:
https://localhost:55555
I would have thought I'd see something similar for these tests cases? It doesn't really matter in any case because they're not working.
Am I missing some element of the config? Do I need to set up the WCF layer to run as well?
I am having problems integration testing my Grails service because the service under test is not being injected in to my test. I have followed the advice from answers to questions else where on Stackoverflow but as yet can not get my service injected. The following class is under /<project_root>/test/integration/com/example:
package com.example
import grails.test.GrailsUnitTestCase
class MyServiceIntegrationTest extends GroovyTestCase {
MyService service;
public void testService() {
assert service != null
}
}
I have tried executing both from the command-line (grails test-app) and from within IDEA both result in the same failure, namely service is null
This is Grails 1.3.6
Any suggestions on how to get my integration test working please?
Autowiring works the same way in integration tests as in other parts of the framework, so you need to make sure the property is named like the service except with the appropriate unCamelCase.
class MyServiceIntegrationTest extends GroovyTestCase {
def myService
}
Assuming your service is an object named MyService.
If I run neo4j in server mode so it is accessible using the REST API, can I access the same neo4j instance with EmbeddedGraphDatabase-class?
I am thinking of a production setup where a Java-app using EmbeddedGraphDatabase is driving the logic, but other clients might navigate the data with REST in readonly mode.
What you are describing is a server plugin or extension. That way you expose your database via the REST API but at the same time you can access the embedded graph db hihgly performant from your custom plugin/extension code.
In your custom code you can get a GraphDatabaseService injected on which you operate.
You deploy your custom extensions as jars with your neo4j-server and have client code operate over a domain oriented restful API with it.
// extension sample
#Path( "/helloworld" )
public class HelloWorldResource {
private final GraphDatabaseService database;
public HelloWorldResource( #Context GraphDatabaseService database) {
this.database = database;
}
#GET
#Produces( MediaType.TEXT_PLAIN )
#Path( "/{nodeId}" )
public Response hello( #PathParam( "nodeId" ) long nodeId ) {
// Do stuff with the database
return Response.status( Status.OK ).entity(
( "Hello World, nodeId=" + nodeId).getBytes() ).build();
}
}
Docs for writing plugins and extensions.
We need to be sure that our web application and mobile client are communicating correctly.
There is two-side communications from the server (Rails application with rspec testing) to the mobile client (Ruby application, has mspec testing framework) and from the mobile client to the server.
So to be sure that the synchronization mechanism is working as expected we need to test the following things:
Server prepares the data correctly.
Mobile client requests and gets
correct data.
Mobile client
prepares
the data to be sent to the server
correctly.
Server recieves and
parses the correct data from the
mobile client.
Servers sends
response to mobile client that
everything is ok.
Mobile client
should carry out appropriate actions
on the device.
How to test this in isolation?
As for all tests, don't plan for the unexpected. Start with what you know. The unexpected will rear it's ugly head soon enough to tell you what else you should test.
What you have is actually simple to test if you break it apart. Here is my approach:
public final static String SERVER_DATA = "Prepared data from the server";
#Test
public void testServerPreparesDataCorrectly() throws Exception {
... usual setup ...
String actual = server.handleRequest( CLIENT_REQUEST );
assertEquals( SERVER_DATA, actual );
}
public final static String CLIENT_REQUEST = "...";
#Test
public void testClientRequest() throws Exception {
... usual setup ...
String actual = client.getRequestData(...);
assertEquals( CLIENT_REQUEST, actual );
}
#Test
public void testClientResponseProcessing() throws Exception {
... usual setup ...
client.parseServerResponse( SERVER_DATA );
... verify client state ...
}
and so on. The basic idea is to put the input and output of each process step into a constant, then run the code which implements the process step for each expected input and validate the output. Where most outputs are also inputs for other tests.
If something changes, you update the inputs/outputs accordingly. Run the tests. And the failures will tell you which process steps you have to update.