Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to mock Scala object #534

Open
pforpramit opened this issue Jul 10, 2024 · 0 comments
Open

Unable to mock Scala object #534

pforpramit opened this issue Jul 10, 2024 · 0 comments

Comments

@pforpramit
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant