user3364192
user3364192

Reputation: 3933

ByteBuddy - transform all methods

I am trying to mimic mock creation with byte buddy. Is it possible to intercept all methods at once?

I tried the following:

new ByteBuddy()
   .subclass(Object.class)
   .method(nameMatches(".*"))
   .intercept(FixedValue.nullValue())

but it unfortunately does not work

Upvotes: 2

Views: 1343

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

This is possible doing this:

new ByteBuddy()
  .subclass(Object.class)
  .method(any())
  .intercept(StubMethod.INSTANCE);

You cannot use nullValue() as some methods do not return anything or a primitive type where null cannot be assigned. Therefore, Byte Buddy throws an exception for your example which indicates this.

Upvotes: 4

Related Questions