Java EE integration test with Spock - jboss

I have JBoss + Java EE application. I would like to run integration test against it using Spock.
So I claim it's possible to run tests after context started:
public class ExampleContextListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
// run Spock
}
}
Please, correct me if I am wrong. Point me to example.

Your question is too broad and cannot be covered here. You have a lot of reading ahead
First take a look at Arquillian
http://arquillian.org/
Then at the Spock extension
https://github.com/arquillian/arquillian-testrunner-spock

Related

ClassNotFoundException for a test class in Eclipse maven project

I create the simplest maven project in eclipse. I add one JUnit test in src/test/java. Then I create a simple application (in src/main/java) that tries to do Class.forName("package.MyTestClass");. The class is not found even though the eclipse project is defined to export the src/test/java as a source folder.
What is going on? How can I fix this?
I wanted to do the following with TestNG
public static void main(String[] args) throws ClassNotFoundException {
// Class.forName("package.MyTestClass");
TestNG.main(new String[]{"testng.xml"});
}
It fails because it cannot find the test classes with:
[TestNG] [ERROR] Cannot find class in classpath: package.MyTestClass
If I have understood you correctly, your project is like this
/src/main/java/Main.java (or whatever you have called it)
/src/test/java/package/MyTest.java
In your Main class you are trying to create an instance of a test class. This will not work. The classes in your main directory are available to the classes in your test directory, not the other way around, which makes sense. Your test classes must know about your application to test it, but your application should not know about the test classes

Spring Roo with GWT BigDecimal serialization

I use STS 3.3.0 with roo 1.2.4. this one use GWT 2.5.0
When I database reverse engineer my database some fields are typed "BigDecimal", mainly amount on financial accounts. When I want to build using mvn gwt:run , I got a build failure due to the following error:
[ERROR] BigDecimalBox cannot be resolved to a type
After search in google I've found that GWT manage the BigDecimal since 2.1.
Any Clue?
Weird, there is no class with that name in the namespace you point (com.google.gwt.user.client.ui.ValueBox) in fact there is no BigDecimalBox in the gwt-2.5.1 library.
Implement it in your project and change imports to match the namespace you use for it.
public class BigDecimalBox extends ValueBox<BigDecimal> {
public BigDecimalBox() {
super(Document.get().createTextInputElement(), BigDecimalRenderer.instance(),
BigDecimalParser.instance());
}
}

GlassFish Error: JAX-RS EJB support is disabled

I am trying a simple app using Jersey JAX-RS + EJB 3.1 on GlassFish 3.1.2.2. All seemed to look pretty well with Jersey REST on GlassFish until I added EJB. When deploying the war file, I got this error.
SEVERE: Error when configuring to use the EJB interceptor binding API. JAX-RS EJB support is disabled.
Anyone who has encountered this before? Is there a configuration in GlassFish to fix this?
My EJB is a simple pojo with #Singleton and #PostConstruct annotation.
#Singleton
public class PurchaseBean {
private String name;
#PostConstruct
public void init() {
System.out.println("Initializing PurchaseBean");
setName("Purchase Singleton EJB");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Looks like this is an Eclipse issue. I did the deployment using Eclipse with the GlassFish adapter. Restarting Eclipse solved it.
Solution: Shutdown GlassFish, inside Eclipse go to Project->Clean(select project) and then start GlassFish again.
I don't know why but it worked. :)
Working solution or the ones who run the Glassfish standalone: Restart the Glassfish. Glassfish version number 4.1.2

Google Guice 3 and OSGi (Eclipse equinox) problem,

I have trouble running Guice 3 within an OSGi container.
Following is a simple test I wrote to test if Guice work well with OSGi.
A simple guice module like:
public class Module extends AbstractModule {
#Override
protected void configure() {
bind(IInterface.class).to(IImplement.class);
}
}
The IInterface and IImplement are both very trivial.
The OSGi activator like this:
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Injector inj = Guice.createInjector(new Module());
IInterface e = inj.getInstance(IInterface.class);
e.sayHello();
}
In Eclipse, I made a target contains all the Guice Jars, and to make guice resolve itself, I made two additional bundle for the aopalliance.jar and javax.injector.jar
However, this simple test fail to load the test bundle, gives me error message complaining cannot find a guice class cannot be found:
Exception in guicetest.Activator.start() of bundle guicetest
Caused by: java.lang.NoClassDefFoundError: com/google/inject/binder/AnnotatedBindingBuilder
at guicetest.guice.Module.configure(Module.java:11)
I hope I have made the problem clear. Can anyone show me how to resolve this problem?
Ah, after just posting the question I found the root of the problem. I didn't specify the com.google.inject.binder package, which the problematical class resides, in the test bundle's Import-Packages. Although the Module doesn't import directly that package, it looks it is still necessary to specify all the indirect dependent packages as well.

UnsupportedPointcutPrimitiveException on simple AOP example

I try to run a simple aop example in this site. I have spring aop and aspectj, aspectjweaver jars:
#Aspect
public class StringAspect {
#Pointcut("call(* String.toLowerCase())")
public void toLowerCasePointcut() {}
#Around("toLowerCasePointcut()")
public String toLowerCaseAroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
String text = ((String) joinPoint.getTarget()).toUpperCase();
return text;
}
}
When I run this example in Test.java like "AaBbCc".toLowerCase(), I get this exception;
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean ... Initialization of bean failed; nested exception is org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression call(* String.toLowerCase()) contains unsupported pointcut primitive 'call'
Spring AOP doesnt contain "call", but why aspectj weaving is not working, ,do you have an idea? Thank you.
edit:
In my spring config file I only have bean definition of #aspect annotated class and <aop:aspectj-autoproxy />.
my jars are : spring-aop-3.0.5, aopalliance, aspectjrt1.6.8, aspectjweaver1.5.0
Have you tried to use the AspectJ Eclipse plugin to do the weaving? (It is also included in SpringSource Tool Suite)
If you have some aspect configuration in your Spring configuration. Try to remove it and just enable AspectJ nature on the project. Also remove all AspectJ jar files and only use those that is attached automatically by the plugin.
With this setup it works for me at least.
Updated: Weaving the aspect advice into code
You get an exception from the Spring container because of your call pointcut. But you want AspectJ weavingweave the aspect. Then you need to use either compile-time or load-time weaving. Compile-time weaving is the simplest alternative ant the alternative offered by the plugin.
You can look at the AspectJ compiler as an advanced Java compiler that also supports AspectJ. So you can run your compiled code anywhere.
Also, you do not need the plugin to compile. You can for example compile with an Ant task as I have showed here.
But the easiest alternative is to use the plugin. This also gives you extra help which I have described briefly here.
I hope this helps!