Powermock of static class gives error: java.lang.NoClassDefFoundError: Could not initialize class XXX - static

Problem
Could not initialize class ...
...javax.xml.transform.FactoryFinder (in our case).
In the article, where we found the solution, it was the class SessionFactory.
Class Under Test
We wanted to write a test for a utils class with static members.
We got the error when trying to create a Mock of a class, which contained a new statement as an initialization of a static field.
public class ClassUnderTest{
private static JavaType javaType = new JavaType();
// ...
}
Test Class
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassUnderTest.class)
public class TestForClassUnderTest {
#Test
public void testCase() {
PowerMockito.mockStatic(ClassUnderTest.class);

Solution
The solution was adding another class level annotation to the test class:
#SuppressStaticInitializationFor("com.example.package.util.ClassUnderTest")
Note, that you have to give the package path and no .class at the end. Unlike #PrepareFor.
Thanks to this article: http://www.gitshah.com/2010/06/how-to-suppress-static-initializers.html
Test Class with Solution
//...
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassUnderTest.class)
#SuppressStaticInitializationFor("com.example.package.util.ClassUnderTest") // <-- this is it :)
public class TestForClassUnderTest {
#Test
public void testCase() {
PowerMockito.mockStatic(ClassUnderTest.class);
//...
}
}

Related

Building an aspect with the same annotation can be on the class or the method and can be both

I'm creating a custom Metric Aspect so that I don't have to do METRIC.aboutTo("save", "order") in the code. My organization uses custom metric libraries.
So, I want to create a custom metric annotation and aspect to do this for me.
I'm looking to be able to add the annotation #PostgressMetric to both the class and the method. It would look something like this:
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.TYPE, ElementType.METHOD})
public #interface PostgressMetric {
String value() default "";
String table() default "";
}
If the metric is on the class, then I want to use those values as "defaults". So you could have something like this:
#Repository
#PostgressMetric(table = "order")
public class OrderRepository {
#PostgressMetric("save")
public void save(Order order) {
}
}
This is also a valid setup:
#Repository
public class OrderRepository {
#PostgressMetric(table="order", value="save")
public void save(Order order) {
}
}
and of course this is also a valid setup:
#Repository
#PostgressMetric(table="order", value="save")
public class OrderRepository {
public void save(Order order) {
}
}
Now, in my aspect I want to get the annotation from the class, then get the annotation from the method. If the class has the annotation then get the table and value and use those as default values. If the method has the annotation then if the values aren't blank, use them to override the default values specified in the class annotation.
Is there a standard way to get the two annotations, or is it just a brute force get the class values and then get the method values and override them?

Facing java.lang.IllegalAccessError for a base abstract class when trying to inject its implementation in wildfly 10

We are receiving the following exceptions in one of our code :
java.lang.IllegalAccessError: tried to access class base.BaseMessage from class message.beans.TerminalPowerCommandProducer$Proxy$_$$_WeldSubclass
Our class structure is as follows :
The Base Message creator class with default message properties
package messages.base
//... required imports
abstract class BaseMessage{ //some protected variables and methods }
the intermediate message class with extension to BaseMessage some additional properties for specific message types
package messages.base
//... required imports
public abstract class PowerMessage extends BaseMessage {//some more protected variables and Logger(using #Inject) and methods}
The actual implementation of the above abstract classes
package messages.beans
//... required imports
#Named
public class TerminalCommandMessage extends PowerMessage {// some more variables with injections and methods with abstract method implementation}
This class is now injected in some other classes :
package messages.beans
#Named
public class TerminalPowerCommandProducer {
#Inject
TerminalCommandMessage commandMessage
//some other code
}
We are receiving exception as reported above.
We are using WildFly version 10.1.0 Final with jdk 8
Is there an issue with the way we have consumed it?
Because if we mark the BaseMessage class as public it all works fine.

Ignore method during Proguard Obfuscation

I have been trying desperately to ignore the following method during Proguard obfuscation:
package com.foo.me;
public class MyClass extends Activity implements Callback {
...
public void setButtonColor(String color, Button button){
...
}
...
}
I feel like I've been through all the answers here with no luck. Can any share the correct statement to skip over this method with Proguard?
Thanks,
Josh
You can keep ProGuard from removing or renaming the class and the method with this configuration:
-keep class com.foo.me.MyClass {
public void setButtonColor(java.lang.String, android.widget.Button);
}
See the ProGuard manual > Examples > Processing callback methods

Resolve region manager in wpf

I am trying to do this in a specflow step definition file, so that I can create an object of view model and make method calls to it.
But I get an error saying "Interface cannot be resolved: Microsoft.Practices.Prism.Regions.IRegionManager (resolution path: TestClass)". What is that I am doing wrong?
public class TestClass
{
private IRegionManager _RegionManager;
[ImportingConstructor]
public TestClass(IRegionManger regionManager)
{
this._RegionManager = regionManager;
// stuff here
}
}
Are you expecting your test class to be instantiated by SpecFlow or via Prism? As both have dependency injection functionality.
So I wouldn't expect your test definition file to have any importing constructors or similar MEF attributes. Instead I would expect your test class to written more like
[Binding]
public class TestClass
{
[Given("I setup Prism")]
public void GivenISetupPrism()
{
// Call into prism
}
}
See the documentation on http://specflow.org for more details.

Inheritance concept in jpa

I created one table using Inheritance concept to sore data into google app engine datastore. It contained following coding but it shows error.How to user Inheritance concept.What error in my program
Program 1:
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
public class calender {
#Id
private String EmailId;
#Basic
private String CalName;
#Basic
public void setEmailId(String emailId) {
EmailId = emailId;
}
public String getEmailId() {
return EmailId;
}
public void setCalName(String calName) {
CalName = calName;
}
public String getCalName() {
return CalName;
}
public calender(String EmailId, String CalName) {
this.EmailId = EmailId;
this.CalName = CalName;
}
}
Program 2:
#Entity
public class method extends calender {
#Id
private String method;
public void setMethod(String method) {
this.method = method;
}
public String getMethod() {
return method;
}
public method(String method) {
this.method = method;
}
}
My constraint is I want output like this
Calendartable contain
Emailid
calendarname
and method table contain
Emailid
method
How to achieve this?
It shows the following error in this line public method(String method)
java.lang.Error: Unresolved compilation problem:
Implicit super constructor calender() is undefined. Must explicitly invoke another constructor
According to Using JPA with App Engine, the JOINED inheritance strategy is not supported.
Your code doesn't compile, add a default constructor in Calendar.
I don't think you should annotate the method field with #Id.
The datastore of GAE/J is not an RDBMS so consequently the only "inheritance strategy" that makes any sense is TABLE_PER_CLASS. I would expect GAE/J to throw an exception if you specify that strategy, and if it doesn't then you ought to raise an issue against them
Your error "constructor calender() is undefined" is rather straightforward. You should create constructor without parameters in calendar class (you can make it private if you don't want to use it). That's because compiler can create default constructor by himself only if there aren't another constructors in the class.

Resources