Gradle custom sourceSets not compiling

I am new to gradle and following is the existing gradle Project structure:

The tests in tests/ folder has tests for src/main, I am able to mark the test folder as test source like:

sourceSets {
    main {
        java {
            setSrcDirs(listOf("src/java"))
        }
        resources {
            setSrcDirs(listOf("src/resources"))
        }
    }
    test {
        java {
            srcDirs("test")
        }
        resources{
            srcDir("test/resources")
        }
    }
}

Problem Statement:

The tests/ folder has tests which using both JUnit-4 & JUnit-5 also referring classes from modules/ folders which giving compilation issues, please suggest how to proceed.

P.S. It’s not possible to restructure the packages.