I'm fairly new to Java security stuff and I'm having a hard time figuring out how to google the right answer to this.
I have a line of code that reads AccessController.getContext()
.
I want to run a test where I mock the result of this. Unfortunately, I have no idea what I'm doing.
How do I set the AccessControlContext
returned by AccessController.getContext()
?
Update
Just to clarify, I know how to mock interfaces. AccessController is a Java security feature. I'm having trouble finding out how to set my own context so that when a method calls AccessController.getContext()
it returns a AccessControlContext of my choosing.
Okay, I have this figured out. Basically what I was trying to do was this:
Subject subject = Subject.getSubject(AccessController.getContext());
In order to create an AccessControlContext
that contains a Subject
, you have to call the method that attempts to obtain the subject like so:
Subject subject = new Subject(); //Set Principles here
Subject.doAs(subject, new PrivilegedAction<Void>()
{
public Void run()
{
Foo.methodThatCaresAboutSubject();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With