Questions tagged [objenesis]

Objenesis is a small Java library that serves one purpose of instantiating a new object of a particular class.

Objenesis is a small Java library that serves one purpose:

To instantiate a new object of a particular class. When would you want this?

Java already supports this dynamic instantiation of classes using Class.newInstance(). However, this only works if the class has an appropriate constructor. There are many times when a class cannot be instantiated this way, such as when the class contains:

Constructors that require arguments. Constructors that have side effects. Constructors that throw exceptions. As a result, it is common to see restrictions in libraries stating that classes must require a default constructor. Objenesis aims to overcomes these restrictions by bypassing the constructor on object instantiation.

Typical uses

Needing to instantiate an object without calling the constructor is a fairly specialized task, however there are certain cases when this is useful:

Serialization, Remoting and Persistence - Objects need to be instantiated and restored to a specific state, without invoking code. Proxies, AOP Libraries and Mock Objects - Classes can be subclassed without needing to worry about the super() constructor. Container Frameworks - Objects can be dynamically instantatiated in non-standard ways.

9 questions
7
votes
1 answer

Missing class org.springframework.objenesis.ObjenesisStd

I am trying to use Spring Data for MongoDB. I am using full text search feature of MongoDB, and wanted to try Spring Data annotations for text index fields (@TextIndexed). This feature is available in 1.6.0.BUILD-SNAPSHOT of Spring Data MongoDB. I…
azec-pdx
  • 4,790
  • 6
  • 56
  • 87
3
votes
1 answer

Objenesis dependency causes instantiation error

Just starting a new Gradle project. This test passes: def 'Launcher.main should call App.launch'(){ given: GroovyMock(Application, global: true) when: Launcher.main() then: 1 * Application.launch( App, null ) >> null } ...…
mike rodent
  • 14,126
  • 11
  • 103
  • 157
3
votes
1 answer

Mockito ClassCastException after using PowerMock

After successfully integrating PowerMock into my project, I ran into this error: ClassCastException occurred while creating the mockito mock. I found a solution, but it does not work. When running tests, an error occurs in the class that mocking…
CoolONE
  • 89
  • 4
  • 8
1
vote
2 answers

CXF Interceptor phases being skipped for missing phase declaration

Been working on this one for a bit and haven't had any luck. I'm in the process of upgrading from Spring 3.2.9 to 4.2.6. My first step was upgrading to cxf-core 3.1.6, which I did with no issues to the app. When upgrading all spring dependencies I…
slk42089
  • 21
  • 4
0
votes
2 answers

strange behaviour with mockito-core v3.6.0 using maven

I am using mockito-core dependency version 3.6.0 using maven as our dependency manager. I observed that same versioned mockito-core used to come with 'compile' dependency scope for 'objenesis' jar +- org.mockito:mockito-core:jar:3.6.0:test [INFO] | …
Vishal
  • 666
  • 1
  • 8
  • 30
0
votes
1 answer

How efficiently create Java proxy object using CGLib (or any other reflection/byte-code library)

Objective: (as the question title says): to efficiently create proxy (domain) objects using CGLib or any other reflection library (e.g. ByetBuddy?) Having our domain class (please note Lombok…
Rad
  • 4,292
  • 8
  • 33
  • 71
0
votes
1 answer

java.lang.InstantiationError when creating instance of static inner class with Objenesis

I am trying to create a utility method that should be able to deep-clone any object. (Object.clone() only works on Object implementing Cloneable and I heard it's flawed anyways.) I am using Objenesis to create new instances of objects without the…
Lahzey
  • 373
  • 5
  • 17
0
votes
1 answer

Objenesis - how to initialize fields

How to make Objenesis initialize field as a normal constructor call? Here is my code: public static class MakeThis implements Serializable{ private int a = 3; private String b = "4"; private HashMap c = new…
Bojan Vukasovic
  • 2,054
  • 22
  • 43
0
votes
2 answers

ClassNotFoundException org.springframework.objenesis.ObjenesisException

`I'm trying to do basic configuration of Maven, Spring, Spring MVC, JPA with hibernate,c3p0, MySql and Jetty server once i start server I'm getting ClassNotFoundException org.springframework.objenesis.ObjenesisException, but I've also added…
Amit Kammar
  • 29
  • 1
  • 8