title: Spock:Unit Test for Graphic2d and BuffredImage
body: <p>I am having a difficulty how to test the below class.How Can I write a test which checks whether
  graphics draw &quot;name&quot; or not with specific width ,heigh ?And what other tests I can write against the following class using spock. And How Can I mock the exception?</p>
  <pre><code> class NameCardCreator{
      
         private val bufferedImage = BufferedImage(580, 380, BufferedImage.TYPE_INT_ARGB)
          var name: String? = null
          const val DISCLAIMER = &quot;Name is Correct&quot;
          fun createNameCard(): String?
          {
              var generic64: String? = null
              try {
                  dashed = BasicStroke(3f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0f, floatArrayOf(5f), 0f)
                  val graphics =  bufferedImage.createGraphics()
                  graphics.drawRoundRect(20, 20, 535, 340, 15, 15) //actual card size
                  val fontMetrics = graphics.fontMetrics
                  graphics.drawLine(300, 120, 540, 120)
                  if (!name.isNullOrEmpty()) {
                      graphics.drawString(name, 300, 110)
                  }
                  graphics.drawString(DISCLAIMER, 50, 290)
                  generic64 = toBase64(bufferedImage)
                  return generic64
              } catch (ie: IOException) {
                  logger.info(&quot;Name card could not be generated $ie.message&quot;)
              }
              return generic64
          }
          
          protected fun toBase64(bufferedImage: BufferedImage): String? {
              var base64Str: String? = null
              var os: ByteArrayOutputStream? = null
              var b64: OutputStream? = null
              try {
                  os = ByteArrayOutputStream()
                  b64 = Base64OutputStream(os)
                  ImageIO.write(bufferedImage, &quot;png&quot;, b64)
                  base64Str = os.toString(&quot;UTF-8&quot;)
              } catch (e: Exception) {
                  logger.error(e.message)
              } finally {
                  os?.close()
                  b64?.close()
              }
              return base64Str
          }
      }
  </code></pre>