Caused by: java.lang.ClassNotFoundException: scala.Serializable

I have upgraded Scala version from 2.12.7 to 3.4.2, I am able to compile the scala code but somehow I am unable to run scalatests. Maven build is failing with an error:
Logs:

[INFO] — scalatest:1.0:test (test) @ core —
An exception or error caused a run to abort. This may have been caused by a problematic custom reporter.
java.lang.NoClassDefFoundError: scala/Serializable
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
Caused by: java.lang.ClassNotFoundException: scala.Serializable
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
… 28 more

My POM:

<properties>
    <scala.binary.version>3</scala.binary.version>
    <scala.version>3.4.2</scala.version>
    <java.version>17</java.version>
    <scoverage.plugin.version>2.0.5</scoverage.plugin.version>
    <diagnostic-events-gson.version>1.0.1347</diagnostic-events-gson.version>
    <flink.version>1.18.1</flink.version>
    <hadoop.version>3.4.0</hadoop.version>
    <skipTests>true</skipTests>
    <encoding>UTF-8</encoding>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <maven.compiler.release>17</maven.compiler.release>
    <dependency.scope>provided</dependency.scope>
    <kafka.connector.version>3.1.0-1.18</kafka.connector.version>
</properties>

<dependencies>
<dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_3</artifactId>
      <version>3.2.9</version>
      <exclusions>
        <exclusion>
          <groupId>org.scala-lang</groupId>
          <artifactId>scala3-library_3</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mockito/mockito-scala-scalatest -->
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-scala-scalatest_2.13</artifactId>
      <version>1.17.37</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.scala-lang</groupId>
          <artifactId>scala-library</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.scalatestplus</groupId>
      <artifactId>mockito-5-12_3</artifactId>
      <version>3.2.19.0</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.scala-lang</groupId>
          <artifactId>scala3-library_3</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala3-library_3</artifactId>
      <version>3.4.0</version>
      <scope>provided</scope>
    </dependency>
</dependencies>

can someone please help to get the exact cause, why its failing? Also I am unable to get exact reason of build failure

Excluding both Scala 3 and Scala 2 libraries just include a Scala 3 library for different version then used by the compiler does not seem to be a good idea. I belive that might be course of problems, but cannot really tell, especially since I have little to no experience in setting up Scala on Maven

Also side note:

    <scoverage.plugin.version>2.0.5</scoverage.plugin.version>

Scoverage plugin is not published for Scala 3, it’s built in into the compiler

Its not related with scoverage plugin, but yes I have disabled it

In Scala 2.12, scala.Serializable was an actual trait. In Scala 2.13, it became merely a type alias for java.io.Serializable. The error you are seeing indicates that you have at least one Scala 2.12 library on your classpath, since only a 2.12 library would be expecting scala.Serializable to actually exist independently.

Scala 3 can use Scala 2.13 libraries, but it cannot use 2.12 libraries.

Note that as a general principle, it’s recommended to upgrade to 2.13 first, get things working, and only then attempt to upgrade to 3. It’s not impossible to go directly from 2.12 to 3, but unless you’re quite expert on all of the differences between the three versions, doing it in the two stages is going to be easier.

4 Likes