Powermockito mock operation on hierarchy level class - junit4

My app has hierarchy level class objects as follows.
Package com.sample.folder1;
Public class ParentClass{ }
Package com.sample.folder2;
Public class Childclass1 extends ParentClass{ }
Unit piece under test package:
#Test
Public void testMocking{
Childclass1 obj = Powermockito.mock(Chilclass1.class);
}
When I execute above junit in eclipse it throws
"VerifyError: Inconsistent stackmap frames....."
Please suggest mocking on hierarchy classes on same and different packages.

Try to add -noverify (Java 8) or -XX:-UseSplitVerifier (Java 7) as vm parameter.

Related

Annotations on file classes

In my current code (Java), I'm doing some custom annotation processing using class-level Java annotations i.e. the annotations are #java.lang.annotation.Target({ElementType.TYPE}).
The target classes contain only static utility methods, so I used file-scoped functions in Kotlin. How do I add these annotations to the generated Kt class?
In Java:
// Utils.java
package com.example;
#MyCustomAspect
public void Utils {
public static void doStuff() {
System.out.println("Hello";
}
}
Now in Kotlin:
// Utils.kt
package com.example;
// ??? #MyCustomAspect ???
fun doStuff() {
System.out.println("Hello";
}
You can use AnnotationTarget.FILE to allow for Kotlin defined annotation to target the Kt class generated from a .kt file. Java defined annotation with target ElementType.TYPE can also be used to target Kotlin file class:
#file:MyCustomAspect
package org.example
#Target(AnnotationTarget.FILE)
annotation class MyCustomAspect
fun doStuff(){
}

EclipseContext get beans of type

I have classes:
#Creatable
#Singleton
public class Sample1 implements ISample {
}
#Creatable
#Singleton
public class Sample2 implements ISample {
}
How can i get all beans of type ISample from EclipseContext?
The Eclipse context does not support anything like this. All you can do is look for objects with a specific name (or specific class which is just converted to a name).
Additionally objects declared using #Creatable are not even created and added to the context until something actually uses them.

Dagger 2 module dependency graph: bound multiple times

I'm new to Dagger 2, trying to port a (quite) complex application to it.
We have several dependencies on 'common' libraries (shared with other projects). Those 'common' libraries sometimes depend on other 'common' libraries. Each library exposes a module.
An example:
#Module
public class JsonModule {
#Provides
public Mapper provideMapper(ObjectMapper objectMapper) {
return new DefaultMapper(objectMapper);
}
#Provides
public ObjectMapper provideObjectMapper() {
return ObjectMapperFactory.build();
}
}
Our HttpModule depends on the JsonModule:
#Module(includes = {JsonModule.class})
public class HttpModule {
public HttpHelper provideHttpHelper(ObjectMapper objectMapper) {
return new DefaultHttpHelper(objectMapper);
}
}
Finally in my application, I depend on both these modules:
#Module(includes = {JsonModule.class, HttpModule.class})
public class MyAppModule {
public Service1 provideService1(ObjectMapper objectMapper) {
return new DefaultService1(objectMapper);
}
public Service2 provideService2(Mapper mappper) {
return new DefaultService2(mappper);
}
}
I then have 1 component that depends on my MyAppModule:
#Component(modules = MyAppModule.class)
#Singleton
public interface MyAppComponent {
public Service2 service2();
}
Unfortunately, when I compile the project, I get a Dagger compiler error:
[ERROR] com.company.json.Mapper is bound multiple times:
[ERROR] #Provides com.company.json.Mapper com.company.json.JsonModule.provideMapper(com.company.json.ObjectMapper)
[ERROR] #Provides com.company.json.Mapper com.company.json.JsonModule.provideMapper(com.company.json.ObjectMapper)
What am I doing wrong? Is it wrong to depend on a module twice in the same dependency graph?
In your MyAppModule you shouldn't include JsonModule, it is included by dagger implicitly. When including HttpModule dagger will automatically include all modules which HttpModule includes (in your case that is JsonModule).
It seems like the problem is related to our project's situation:
the common projects combine Groovy and Java
the common projects are built using Gradle
the application project combines Groovy and Java
the application project was built using Maven and the groovy-eclipse-compiler
Basicly: I blame the groovy-eclipse-compiler for now. I converted the project to Gradle, and everything works now.

Cannot use java value as parameter to java annotation on scala function

I have 3 java files: HW.java, myAnn.java, and Constants.java in package myApp.
Constants.java:
public final class Constants {
public static final String WORLD ="World";
}
myAnn.java:
public #interface myAnn {
java.lang.String name() default "";
}
HW.java:
class HW {
#myAnn(name = Constants.WORLD)
public static void main(String[] args){
System.out.println("Hi "+ Constants.WORLD);
}
}
My app compiles and runs fine as shown above, but I want to migrate HW.java to scala as
HelloWorld.scala:
object HelloWorld {
#myAnn(name = Constants.WORLD)
def main(args: Array[String]) {
println("Hello " + Constants.WORLD)
}
}
When I try to compile this, I get
error: annotation argument needs to be a constant; found:
Constants.WORLD #myAnn(name = Constants.WORLD)
If I remove the annotation then HelloWorld compiles and executes as expected.
Why can I use Constants.WORLD as a parameter to an annotation from a java program, but not from a scala program? Is there something I can modify in Constants.java to allow it to be used from either java or scala? I can't modify MyAnn.java, and I can't migrate Constants.java yet.
It is a bug that only shows up when feeding the java source files into the scala compiler, see issue SI-2764. The example works when compiling the java files first using javac and then pointing scalac's classpath to the generated classfiles.

How to call Grails from Scala

Is there a way to call a method in a Grails service, from a Scala class that is running on the same JVM?
I have seen something similar done from Groovy/Griffon but cannot figure out how to accomplish that in Grails. (http://www.jroller.com/aalmiray/entry/griffon_groovy_scala_working_together)
Basically, one of my Grails controllers calls some Scala code, which should return some values asynchronously. So, I guess, the only way to return those values is by calling back a method in a Grails service.
I found a way of doing it, inspired by the link in the question above, and one of the FAQs in the Grails website.
On the Scala side:
Declare an object similar to the following:
package scalaCallback
object ScalaCallback{
var cback: {def callback(example: String)} = null
def setCallback(cb: {def callback(example: String)}){
cback = cb
}
def invokeCallback(example: String){
if(callback != null) cback.callback(example)
}
}
On the Grails side:
Create a class in src/groovy similar to the following:
package groovyCallback
import org.codehaus.groovy.grails.commons.ApplicationHolder
class GroovyCallback{
private GroovyCallback() {}
private static final INSTANCE = new GroovyCallback()
static getInstance(){ return INSTANCE }
void callback(String example){
ApplicationHolder.application.mainContext.yourService.yourMethod(example)
}
}
In your BootStrap.groovy init add the following:
scalaCallback.cback = groovyCallback.GroovyCallback.getInstance()
When you call invokeCallback("example") in Scala, it will call yourService.yourMethod("example")
Note: the jar file with your Scala class should be in the lib folder of you Grails application
Your Grails service is a Spring bean. #Autowire the service into your Scala class (it will need to be a bean/#Component) and call the method.
EDIT - added example:
For example (using Java, not Scala but the approach is exactly the same):
Java code calling service:
package grailstest;
#Component
public class ServiceInjectionTester {
#Autowired TestService testService;
public String testTheService() {
return testService.serviceMethod();
}
}
Service:
class TestService {
String serviceMethod() {
return "success"
}
}
In Config.groovy:
grails.spring.bean.packages = [ "grailstest" ]
You can also wire your Java/Scala bean into your Grails classes:
class TestController {
#Autowired
ServiceInjectionTester serviceInjectionTester
def index = {
render(text: serviceInjectionTester.testTheService())
}
}
References:
Grails Reference 8.4 - Using Services from Java
Spring: The Foundation for Grails