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 8388159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:24:33+00:00 2026-06-09T18:24:33+00:00

I am currently working on some monitoring tool using aspectj. Because this tool should

  • 0

I am currently working on some monitoring tool using aspectj. Because this tool should be technology independent (as far as possible), I am not using Spring for injection. But I want my aspects to be unit-tested.

Aspect example:

@Aspect
public class ClassLoadAspect {
    private Repository repository;

    public ClassLoadAspect() {
        repository = OwlApiRepository.getInstance();
    }  

    @After("anyStaticInitialization()")
    public void processStaticInitilization(JoinPoint jp) {
        Class type = jp.getSourceLocation().getWithinType();
        if (type.isInterface()) {
            repository.storeInterfaceInitialization(type);
        } else if (type.isEnum()) {
            repository.storeEnumInitialization(type);
        } else {
            repository.storeClassInitialization(type);
        }

    }

    @Pointcut("staticinitialization(*) && !within(cz.cvut.kbss.odra..*)")
    public void anyStaticInitialization() {
    }

    public Repository getRepository() {
        return repository;
    }

    public void setRepository(Repository repository) {
        this.repository = repository;
    }  
}

However, I really dont know, how to construct unit test (the repository field should be mocked (using mockito)), But I do not have aspect creation under control, hence I cannot set the dependency manually. What should I call to get the instance? Or there is some other scenario how to unit-test aspectj aspects.

Thanks.

  • 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-06-09T18:24:34+00:00Added an answer on June 9, 2026 at 6:24 pm

    You say you find your own way of introducing the mock object hacky. What exactly do you dislike and how do you imagine it to be? I can only guess:

    Do you dislike the fact that you globally replace calls to OwlApiRepository.getInstance() in your meta aspect? Then you could specifically restrict mock object injection to the aspect’s constructor (I am using native AspectJ syntax because I feel uncomfortable with the POJO annotation style):

    public privileged aspect ClassLoadTestAspect {
        static boolean active = true;
    
        declare precedence : ClassLoadTestAspect, ClassLoadAspect;
        pointcut classLoadAspect() :
            if(active) && 
            withincode(ClassLoadAspect.new()) && 
            call(* OwlApiRepository.getInstance());
    
        Object around() : classLoadAspect() {
            return new MockRepository();
        }
    }
    

    As you can also see, this variant of a meta (aspect-testing) aspect also has a switch to turn it on and off at will. Maybe this was also something you disliked. As I said, I am guessing. After your feedback I might be able to answer more specifically.

    Edit: As for your concerns, I think I have addressed them as far as possible:

    • You do not need a mock holder.

    • The aspect can be (de)activated. It would be easy to make its activation dependent on other conditions, so it is only active in your test environment. If this is still not enough, use compile-time weaving for your production aspects and load-time weaving for your test aspect. This way its byte code will not even be present in your production environment.

    • My version does not globally replace anything, but like a good surgeon only cuts in a minimally invasive way exactly at one place.

    • I cannot really understand your concern about byte code manipulation for several reasons: You use AspectJ, i.e. inherently byte code manipulation (weaving). You use Mockito which creates classes at runtime. I also do not understand where you see a deficiency in AspectJ. You have not explained how you want the “standard means of the language” to behave or what interface it should provide for testing. Even if you had, I cannot change the language (AJ) and tool (Mockito) of your own choice for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am currently working on a java application for some network monitoring tool. In
I'm currently working on some coding for a hotel booking widget and am using
I am currently working with some users that do not have the .NET Framework
I'm currently working with some data using Javascript that is in the form of
I am currently working on some POC using JS Tree plugin and related check
I am currently working on some Ajax heavy code and I am wondering how
I'm currently working through some exercises in a c++ book, which uses text based
I am currently working on some simple custom allocators in c++ which generally works
I am currently working on some test cases for my website. I've managed to
I'm currently working on databinding some of my existing Windows Forms, and I've ran

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.