Google guice module configuration for unit testing - java

I'm trying to use google guice for dependency injection however I can't seem to wire everything togheter.
In my web.xml I defined the guiceFilter and the guiceListener like so:
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>backend.listener.GuiceConfigListener</listener-class>
</listener>
the config listener is basicly pretty simple:
#Override
protected Injector getInjector(){
return Guice.createInjector(new ServletModule(), new ArtsModule());
}
and the ArtsModule at this moment just has one binding like so:
#Override
protected void configure(){
bind(ArtsDAO.class).to(ArtsDAOGae.class);
}
I then continue to do a field injection of the ArtsDao in a service class:
#Inject
private ArtsDAO artsDAO;
But when I try to build my project (which is a maven build) I get a NPE on the artsDAO field, this most likely happens because the unit tests aren't running in a web environment.
Can anyone advice me on how to configure the guice bidings so that they are picked up during unit testing?
Thanks

Pip,
this is not trivial task but definitely you can achieve what you want.
First of all have a look at Tadedon project at https://code.google.com/p/tadedon
especially tadedon-guice-servlet-mock.
You will need something like fake container for your test. My fake container contains also Apache Shiro integration so you can throw it out, It looks like:
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.xemantic.tadedon.guice.servlet.mock.FakeServletContainer;
import com.xemantic.tadedon.guice.servlet.mock.FakeServletContainerModule;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.SubjectThreadState;
import org.apache.shiro.web.subject.WebSubject;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Arrays;
public class FakeTestContainerInit {
private final FakeServletContainer servletContainer;
private final Injector internalInjector;
private Subject internalSubject;
public FakeTestContainerInit() {
this(new Module[] {});
}
public FakeTestContainerInit(Module... modules) {
super();
modules = Arrays.copyOf(modules, modules.length + 1);
modules[modules.length-1] = new FakeServletContainerModule();
internalInjector = Guice.createInjector(modules);
servletContainer = internalInjector.getInstance(FakeServletContainer.class);
}
public void start() throws ServletException, IOException {
this.start(true);
}
public void start(boolean initializeSecurityContext) throws ServletException, IOException {
getServletContainer().start();
MockHttpServletRequest request = servletContainer.newRequest("GET","/");
MockHttpServletResponse response = new MockHttpServletResponse();
if(initializeSecurityContext) {
SecurityManager scm = internalInjector.getInstance(SecurityManager.class);
internalSubject = new WebSubject.Builder(scm, request, response).buildWebSubject();
SubjectThreadState sts = new SubjectThreadState(internalSubject);
sts.bind();
} else { internalSubject = null; }
getServletContainer().service(request, response);
}
public void stop() {
servletContainer.stop();
}
public FakeServletContainer getServletContainer() {
return servletContainer;
}
public <T> T getInstance(final Class<T> type) throws IOException, ServletException {
return getServletContainer().getInstance(type);
}
public <T> T getInstance(final Key<T> key) throws IOException, ServletException {
return getServletContainer().getInstance(key);
}
public Subject getSubject() {
return internalSubject;
}
}
Dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-guice</artifactId>
</dependency>
<dependency>
<groupId>com.xemantic.tadedon</groupId>
<artifactId>tadedon-guice-servlet-mock</artifactId>
</dependency>
and Apache Shiro you won't need:
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
</dependency>
All you need to do, is create FakeTestContainerInit and call start() and stop() method. Also all object creations have to be done via FakeTestContainerInit.getInstance method inside tests.
Well, I used it to test Vaadin application so I did not need sending requests and checking responses. So, this one you will need to implement. It can be done via getServletContainer().service(request, response);. But i think you will figure out. Hope it will help you.

Related

Vaadin with Apache CXF SOAP service

I am new to Vaadin, just generated the application in Vaadin web site and built it locally. Then I added Apache CXF SOAP service to it, but I am unable to use the Tomcat that Vaadin is using, but instead I load SOAP in Jetty using:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
<scope>compile</scope>
</dependency>
My Vaadin application is:
#SpringBootApplication
#Theme(value = "iciclient", variant = Lumo.DARK)
#PWA(name = "ICI Client", shortName = "ICI Client", offlineResources = {"images/logo.png"})
public class Application extends SpringBootServletInitializer implements AppShellConfigurator {
public static void main(String[] args) {
LaunchUtil.launchBrowserInDevelopmentMode(SpringApplication.run(Application.class, args));
try {
System.out.println("Starting IciEventClient");
Object implementor = new IciEventServiceSoap12Impl();
String address = "http://localhost:8081/ici/IciEventService";
Endpoint.publish(address, implementor);
// http://localhost:8081/ici/IciEventService?WSDL
} catch (Exception e) {
e.printStackTrace();
}
}
}
While this works, I would like to get rid of separate Jetty dependency and run the SOAP service in Vaadin Tomcat (localhost:8080).
Should be simple but I can't figure out how to do it.
I think that it needs a separate servlet and route, but I don't know how to add them.
There is no web.xml in the Vaadin application, for example.
I am not familiar with Apache CXF, but based on CXF docs and the sample project I think I got it to work.
I downloaded a new Vaadin 14/Java 8 project from start.vaadin.com, and did the following:
Added the dependency
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
Created a web service
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService
public class Test {
#WebMethod
public String test() {
return "This works";
}
}
Exposed it as a bean in my Application class
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.vaadin.artur.helpers.LaunchUtil;
import org.vaadin.erik.endpoint.Test;
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
LaunchUtil.launchBrowserInDevelopmentMode(SpringApplication.run(Application.class, args));
}
#Bean
public Endpoint test(Bus bus) {
EndpointImpl endpoint = new EndpointImpl(bus, new Test());
endpoint.publish("/Test");
return endpoint;
}
}
That was it! At least I can now list the service definition at http://localhost:8080/services/Test?wsdl
The first documentation link lists some configurations you can do, for example to change the /services path. The example project shows how to configure Spring actuator metrics if that is something you need.
You might want to create a separate #Configuration-annotated class for all your service #Bean definitions.
If you don't want to use the starter dependency, this Baeldung article looks promising.

ServletContextListener isn't invoked when using com.sun.net.httpserver.HttpServer

I use com.sun.net.httpserver.HttpServer in integration tests. It does its job but I've noticed that ServletContextListener's methods aren't invoked during tests.
When I deploy the app to the real Tomcat server I can see its methods being called.
Below is the listener class:
package abc;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
#WebListener
public class StartupListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("########################################");
}
#Override
public void contextDestroyed(ServletContextEvent event) {
System.out.println("########################################");
}
}
Here is how I start the HttpServer in the test:
#BeforeClass
public static void startServer() {
URI uri = UriBuilder.fromUri("http://localhost/").port(8080).build();
// Create an HTTP server listening at port 8080
try {
server = HttpServer.create(new InetSocketAddress(uri.getPort()), 0);
} catch (IOException e) {
e.printStackTrace();
fail();
}
// Create a handler wrapping the JAX-RS application
HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new ApplicationConfig(), HttpHandler.class);
// Map JAX-RS handler to the server root
server.createContext(uri.getPath(), handler);
// Start the server
server.start();
}
Maven dependency:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
I removed #WebListener and inserted
metadata-complete="false"
and
<listener>
<listener-class>abc.StartupListener</listener-class>
</listener>
in web.xml but it didn't help
What is the problem? Is there anything that needs to be setup?
What is the problem?
HttpServer is not a servlet container and does not know about servlet api.
Is there anything that needs to be setup?
You need to setup a servlet container like jetty, which is frequently used for unit testing java based web applications.

Vertx trying to launch embedded server

I am trying to launch an initial test of a Vertx.io server, but I get the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/vertx/java/core/Handler
Code:
package com.company;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.Handler;
import org.vertx.java.core.Vertx;
import org.vertx.java.core.VertxFactory;
import org.vertx.java.core.http.HttpServerRequest;
import java.io.IOException;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
private Object shutdownLock = new Object();
public Main() throws IOException, InterruptedException {
start(1234);
keepServerFromShuttingDown();
}
private void keepServerFromShuttingDown() throws InterruptedException {
synchronized (shutdownLock) {
shutdownLock.wait();
}
log.info("Shutting down");
}
public void start(int port) {
Vertx vertx = VertxFactory.newVertx();
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
#Override
public void handle(HttpServerRequest request) {
}
}).listen(port);
}
public static void main(String[] args) throws IOException, InterruptedException {
new Main();
}
}
pom.xml:
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-platform</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
It looks like a basic CLASSPATH issue where it is not able to find Vertx classes while executing your program. Please check if the vertx libraries are indeed a part of your CLASSPATH.
Though unrelated, but if you are checking out Vertx for some new projects, I highly recommend version 3.0 and you could start with this simple maven project example

How to enable neo4j webadmin when using spring-data-neo4j?

I am bootstrapping a new project from the Accessing Neo4j Data with REST example. The example uses an embedded database rather than a standalone neo4j server, but I would like to use the Neo4J webadmin interface for visualisation of my data. How do I enable the webadmin interface starting from this configuration?
(They got WrappingNeoServerBootstrapper working in use WrappingNeoServerBootstrapper with spring-data-neo4j but a lot of knowledge is omitted from the answer, e.g. it is not even mentioned where to place to the configuration. Being new to POMs, Spring Boot and Neo4j I therefore can't make use of that answer.)
The example you are using needs some tweaking to enable the Neo4j browser. I started from a different example, the Accessing Data with Neo4j example and it worked well.
You will need to do the following:
Change the version on your spring boot pom to 1.2.1.Release:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>
Add dependencies for Neo4jServer:
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.1.5</version>
<classifier>static-web</classifier>
</dependency>
Implement the Spring Boot command line runner in your Application.class:
public class Application extends Neo4jConfiguration implements CommandLineRunner{
Autowire a reference to your GraphDatabaseService in your Application.class:
#Autowired
GraphDatabaseService db;
#Override the run method from CommanLineRunner in your Application.class:
#Override
public void run(String... strings) throws Exception {
// used for Neo4j browser
try {
WrappingNeoServerBootstrapper neoServerBootstrapper;
GraphDatabaseAPI api = (GraphDatabaseAPI) db;
ServerConfigurator config = new ServerConfigurator(api);
config.configuration()
.addProperty(Configurator.WEBSERVER_ADDRESS_PROPERTY_KEY, "127.0.0.1");
config.configuration()
.addProperty(Configurator.WEBSERVER_PORT_PROPERTY_KEY, "8686");
neoServerBootstrapper = new WrappingNeoServerBootstrapper(api, config);
neoServerBootstrapper.start();
} catch(Exception e) {
//handle appropriately
}
// end of Neo4j browser config
}
When you are all done, your Application.class should look like this:
package hello;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.server.WrappingNeoServerBootstrapper;
import org.neo4j.server.configuration.Configurator;
import org.neo4j.server.configuration.ServerConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
#Configuration
#EnableNeo4jRepositories
#Import(RepositoryRestMvcConfiguration.class)
#EnableAutoConfiguration
public class Application extends Neo4jConfiguration implements CommandLineRunner{
public Application() {
setBasePackage("hello");
}
#Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase("target/hello.db");
}
#Autowired
GraphDatabaseService db;
#Override
public void run(String... strings) throws Exception {
// used for Neo4j browser
try {
WrappingNeoServerBootstrapper neoServerBootstrapper;
GraphDatabaseAPI api = (GraphDatabaseAPI) db;
ServerConfigurator config = new ServerConfigurator(api);
config.configuration()
.addProperty(Configurator.WEBSERVER_ADDRESS_PROPERTY_KEY, "127.0. 0.1");
config.configuration()
.addProperty(Configurator.WEBSERVER_PORT_PROPERTY_KEY, "8686");
neoServerBootstrapper = new WrappingNeoServerBootstrapper(api, config);
neoServerBootstrapper.start();
} catch(Exception e) {
//handle appropriately
}
// end of Neo4j browser config
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The Neo4j browser will be available on the host and port configured in your run() method.

java.lang.NoClassDefFoundError: Could not initialize class javax.mail.internet.InternetAddress

I am trying to send an email using the JavaMail API. Here is my code on the servlet:
package com.lsp.web;
import com.lsp.service.Mailer;
import javax.ejb.EJB;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
#WebServlet(name = "contact", urlPatterns = {"/contact"})
public class ContactServlet extends SpringInjectedServlet {
#EJB
private Mailer emailBean;
#Override
public void init() throws ServletException {
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String customerEmail = req.getParameter("email");
String subject = req.getParameter("subject");
String body = req.getParameter("message");
String error = null;
String succMess = null;
try {
javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();
emailBean.send(customerEmail, subject, body);
req.setAttribute("succMessage", succMess);
req.getRequestDispatcher("sent.jsp").forward(req, resp);
} catch (javax.mail.internet.AddressException ae) {
error = "您指出的邮箱地址不存在";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
catch (MessagingException mex) {
error = "发送失败";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
}
}
At the line where I check for the user address where:
javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();
I got an exception.
java.lang.NoClassDefFoundError: Could not initialize class javax.mail.internet.InternetAddress
In pom.xml, I added these lines:
<!--JavaMail API-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.1</version>
</dependency>
<!--EJB-->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
</dependency>
I am using Tomcat.
Could someone tell me why this happens and how I can solve the issue.
Thank you.
Please see: https://stackoverflow.com/a/28935760/1128668 You have included the mail-api.jar in your project. That's the API specification only. The fix is to replace this:
<!-- DO NOT USE - it's just the API, not an implementation -->
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
with the reference implementation of that api:
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
I know it has sun in the package name, but that's the latest version.
You get a java.lang.NoClassDefFoundError, which means that the JVM can't initialise the class, not that it can't find the class which would be a ClassNotFoundException. A NoClassDefFoundError can be caused by a ClassNotFoundException but that need not be the case.
In order to find the cause, stop the server, delete the log and start again. Then reproduce the error and try to find the first Exception and its cause in your log file. If you are lucky this is the cause for the NoClassDefFoundError.
You also might indicate in your question which server you are using. It might make a difference how to solve the error.
Adding the dependency at build time does nothing to make the dependency available to Tomcat at runtime. You need to add the javax.mail.jar file to the WEB-INF/lib directory of your application, or to Tomcat's lib directory.
Of course, you wouldn't have this problem if you were using a full Java EE application server instead of Tomcat... :-)

Categories

Resources