Skip to content

Unable to mock Scala object #534

@pforpramit

Description

@pforpramit

I have started working in Scala very recently. I have below code in the main method that I'll need to mock for writing test cases,

object SacSAAgentSoftwareLogParser extends SparkSessionCreation {

  def main(args: Array[String]): Unit = {

      secret = mapAsJavaMap(SecretConfig.apply())
      
      ...
      

Here is how the object SecretConfig is defined. It reads configs from HC Vault.

object SecretConfig {

  def apply(): collection.Map[String, String] = {
  
  ...

In the test case, I am trying to mock the SecretConfig.apply() so that it does not make a real-time call while executing the same -

class SacSAAgentSoftwareLogParserTest extends AnyFlatSpec with Matchers with MockitoSugar {

  "SacSAAgentSoftwareLogParser" should "execute the main method and process data correctly" in {
  
  val intermediateSecretMap: collection.Map[String, String] = Map( "secret1" -> "value1")
  
      withObjectMocked[SecretConfig.type] {
      when(SecretConfig.apply()).thenReturn(intermediateSecretMap)
    }

    ...

SacSAAgentSoftwareLogParser.main(Array.empty)
    ...

However, the mocking does not really work while running the test case. Instead, it goes on to call the SecretConfig.apply() in real time. I have added that mock-maker-inline thing in src -> test -> resources -> mockito-extensions -> org.mockito.plugins.MockMaker.

Definitely I'm doing some mistakes here, just not able to figure out what they are. There might be some gap in my understandings as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions