0

I would like to make some unit tests for a function inside my class, simplified example:

Class Datachecks {
   df = spark.read.parquet(..)
   val logger = Logger(getClass)
   def logColumns(df: DatFrame): Unit = {
      df.columns.foreach(logger.info(_))
}

Which i then want to test with something like

Class DataChecksSuite extends FunSuite with initSpark {
   val initDataChecks = new DataChecks()
   val df = spark.read.parquet()
   test("Example test") {
      assert(initDataChecks.logColumns(df) === "myOutput")
   }
}

Now I know this won't run, because my column does not output a String, nor would I like to rewrite/refacter my entire DataChecks Class to make this possible.

Now I was wondering: is it possible to catch the console output of log4j and turn this into a String? Or would it be possible to mock my logger class to output a String? (I have tried with Mockito but without success..)

My (required) dependencies are log4j & FunSuite (if it's really necessary I could still switch, but as the project is rather large I would like to keep all things consistent.

I can provide a more detailed example when needed, as this is only a very simplified spoof example.

2 Answers 2

1

how about adding another line to logColumns as follows df.columns.mkString(" ") change the return type of the method to String and then use that to run the assert in your test?

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, Thank you for the answer but that's not really what I want to do.The example was just a spoof example, I really just wan't to test the output of my logging function (which in reality is way more complex then just to print the df.columns). This is also something which I want to do in multiple instances (not only in this class) as we are logging quite some bit and we are trying to make everything more robust by adding more unit tests. So it's really not feasible to rewrite everything to return an output.
0

fixed this btw some time ago by adding an appender to my testlog4j2.xml, which logs to a file. Then I could read in the file and let my unit tests run on that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.