Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6119199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:31:27+00:00 2026-05-23T15:31:27+00:00

I implemented a ClassFileTransformer for a javaagent using ASM. Because it has some bugs,

  • 0

I implemented a ClassFileTransformer for a javaagent using ASM. Because it has some bugs, I want to write a JUnit test case for it. How do I do this?

Using pseudo-code I thought along the lines:

// Have a test class as subject
public static class Subject {
  public void doSomething(){...}
}
// Manually load and transform the subject
...?
// Normally execute some now transformed methods of the subject
new Subject().doSomething();
// Check the result of the call (i.e. whether the correct attached methods were called)
Assert.assertTrue(MyClassFileTransformer.wasCalled());

Now the question is: How do I manually load and transform the subject and make the JVM/Classloader use my manipulated version of it? Or do I completely miss something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:31:27+00:00Added an answer on May 23, 2026 at 3:31 pm

    I got it. One needs to implement an own ClassLoader that does the same transformation with the test subject as the ClassFileTransformer (e.g. calls it). And of course the subject class may not already be loaded, so there may not be any direct usage of it. So I used Java reflection API to execute the methods of the subject class.

    In a separate file:

    public static class Subject {
        public void doSomething(){...}
    }
    

    In the test:

    private static class TransformingClassLoader extends ClassLoader {
    
        private final String className;
    
        public TransformingClassLoader(String className) {
            super();
            this.className = className;
        }
    
        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (name.equals(className)) {
                byte[] byteBuffer = instrumentByteCode(fullyQualifiedSubjectClass);
                return defineClass(className, byteBuffer, 0, byteBuffer.length);
            }
            return super.loadClass(name);
        }
    }
    
    @Test
    public void testSubject(){
        ClassLoader classLoader = new TransformingClassLoader(fullyQualifiedSubjectClass);
        Class<?> subjectClass = classLoader.loadClass(fullyQualifiedSubjectClass);
        Constructor<?> constructor = subjectClass.getConstructor();
        Object subject = constructor.newInstance();
        Method doSomething = subjectClass.getMethod("doSomething");
        doSomething.invoke(subject);
        Assert.assertTrue(MyClassFileTransformer.wasCalled());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented lazy image load for my UITableView using NSUrlConnection. This is all working
I implemented this to my site from an earlier question but for some reason
Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T>
I implemented some actions in a Struts2+J2EE environment. I'd like to validate inputs with
I implemented paging for one of my GridViews. Now I want that all checked
I implemented a python com server and generate an executable and dll using py2exe
I implemented HTTP digest authentication in my CherryPy application using the built-in tools.auth_digest tool.
I implemented the GPS in my app, in one Activity like this: LocationManager mlocManager
I implemented a REST service and i'm using a web page as client. My
I implemented the following Silverlight app after seeing this instructions , here's my code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.