Pramit Pakhira
Pramit Pakhira

Reputation: 195

Unable to mock Scala object

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. 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.

Upvotes: 0

Views: 56

Answers (0)

Related Questions