Junit, mocking principle

45 views
Skip to first unread message

Pablo Vidaurri

unread,
Sep 27, 2021, 6:23:31 PM9/27/21
to CAS Community
I'm trying to test my custom webflow, for a few of the actions I have a principle. How do I mock the principle? I've reviewed cas code and see the use of  CoreAuthenticationTestUtil but for the life of me I cannot get the dependency included in my project.

Ray Bon

unread,
Sep 27, 2021, 7:02:27 PM9/27/21
to cas-...@apereo.org
Pablo,

Clone the cas repo, https://github.com/apereo/cas
Then search for that file
$ find . -name CoreAuthenticationTestUtils.java
./core/cas-server-core-authentication-api/src/test/java/org/apereo/cas/authentication/CoreAuthenticationTestUtils.java

Then

    testImplementation "org.apereo.cas:cas-server-core-authentication-api"

Ray

On Mon, 2021-09-27 at 15:23 -0700, Pablo Vidaurri wrote:
Notice: This message was sent from outside the University of Victoria email system. Please be cautious with links and sensitive information.

I'm trying to test my custom webflow, for a few of the actions I have a principle. How do I mock the principle? I've reviewed cas code and see the use of  CoreAuthenticationTestUtil but for the life of me I cannot get the dependency included in my project.

-- 
Ray Bon
Programmer Analyst
Development Services, University Systems

I acknowledge and respect the lək̓ʷəŋən peoples on whose traditional territory the university stands, and the Songhees, Esquimalt and WSÁNEĆ peoples whose historical relationships with the land continue to this day.

Pablo Vidaurri

unread,
Sep 28, 2021, 9:15:04 AM9/28/21
to CAS Community, Ray Bon
So, I actually thought of doing that. But is it code I'll now have to maintain in my own repo and have jenkins also build. I guess no other choice.

-psv

Pablo Vidaurri

unread,
Sep 28, 2021, 12:17:54 PM9/28/21
to CAS Community, Ray Bon
Maybe you were not implying that I clone the subproject and include it in my overlay project.

My problem is that I include the following in my build.gradle:
testImplementation "org.apereo.cas:cas-server-core-authentication-api:${project.'cas.version'}"

and in my test class I import:
import org.apereo.cas.authentication.CoreAuthenticationTestUtils;

I see the jar when I do a gradlew dependencies 

But I get a compile error when building:
   MyActionTest.java:27: error: cannot find symbol
   import org.apereo.cas.authentication.CoreAuthenticationTestUtils;
                                    ^
     symbol:   class CoreAuthenticationTestUtils
     location: package org.apereo.cas.authentication
     1 error

     > Task :compileTestJava FAILED


-psv


On Monday, September 27, 2021 at 6:02:27 PM UTC-5 Ray Bon wrote:

Ray Bon

unread,
Sep 28, 2021, 1:15:47 PM9/28/21
to psvid...@gmail.com, cas-...@apereo.org
Pablo,

I only use the cas code for lookups. Where log messages are being generated, relationship among classes etc. It can be useful when something is not working the way you expect or if code has changed and docs have not been updated, etc.
You would not include it or any parts in your overlay project. I have a separate directory where I keep vendor sources.

Ray

Ray Bon

unread,
Sep 28, 2021, 1:32:58 PM9/28/21
to psvid...@gmail.com, cas-...@apereo.org
Pablo,

Sorry, I missed an important point.
You will have to copy that file, CoreAuthenticationTestUtils, into your project (src/test/java/...). And update it during upgrades.

The test classes are not part of the jar files; just the files under src/main/java/...

When you build, if the included file has dependencies, you will get errors. Just repeat the steps to find the missing file and package.
The test classes that you might include should 'not' depend on too many other test classes, just the main classes (hopefully). I have only 2. If you find that you are copying in many test classes, then perhaps the feature you are working on would be better suited to inclusion in the cas project, https://fawnoos.com/2020/10/22/cas63x-codebase-feature-build/

Ray 

P.S. This is my list of library includes (some may not be needed)

    testCompileOnly "org.apereo.cas:cas-server-core-webflow-api"
    testCompileOnly "org.apereo.cas:cas-server-core-util-api"
    testImplementation "org.apereo.cas:cas-server-core-services-authentication"
    testImplementation "org.apereo.cas:cas-server-core-authentication-api"
    testImplementation "org.apereo.cas:cas-server-core-services"
    testImplementation "org.apereo.cas:cas-server-core"
    testImplementation "org.apereo.cas:cas-server-core-api"
    testImplementation "org.apereo.cas:cas-server-support-person-directory"

Pablo Vidaurri

unread,
Sep 29, 2021, 1:30:57 PM9/29/21
to CAS Community, Ray Bon, Pablo Vidaurri
Ray, I owe you one.

Added to build.gradle:
testImplementation "org.apereo.cas:cas-server-core-authentication-api:${project.'cas.version'}"

Copied over following java files in their expected packages in src/test/java:
CoreAuthenticationTestUtils.java
SimpleTestUsernamePasswordAuthenticationHandler.java

I was able to mock the principle and get my junit test completed.

Let me know what your vice is and I'll do my best to send it over.

-psv
Reply all
Reply to author
Forward
0 new messages