I want to declare the class that inherited the abstract class as an inner class in private and access it from the outside.

Asked 2 years ago, Updated 2 years ago, 80 views

 ■ MyAbstractClass.java
public abstract class MyAbstractClass {
    publicMyAbstractClass(){
    }
    public abstract int notifyAbs(inta);
}

■ MyClassAbstractInnerClass.java
publicclassMyClassAbstractInnerClass{
    privateMyAbstractClass testAbstract=newMyAbstractClass(){
        @ Override
        public int notifyAbs(inta){
            return a;
        }
    };
}

Is it possible to have external access to the method (notifyAbs) of the private class instance (testAbstract) that inherits the above abstract class?
I'd like to test notifyAbs on JUnit, but I don't know how to access it.

java junit

2022-09-30 17:50

2 Answers

"If you search the net in ""junit private method test"", you will find many things."
For example:

Method method=Sample.class.getDeclaredMethod("getName", Integer.class);
method.setAccessible(true);
String resetl=(String)method.invoke(new Sample(), new Integer(1));

Note: http://javatechnology.net/java/private-junit/


2022-09-30 17:50

Thank you for your reply.Access to private classes, private methods, and inner classes has been realized, but it has never been possible in a field that inherits an abstract class such as the subject and has a method inside it.

publicclassMyClassAbstractInnerClass{
    private InnerClass testAbstract = new InnerClass();
    public class InnerClass extensions MyAbstractClass{
        @ Override
        public int notifyAbs(inta){
            return a;
        }
    }
}

Thus, by separating the declaration and processing of the fields as described above, we have achieved access to notifyAbs().


2022-09-30 17:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.