Forum Discussion

wimjan's avatar
wimjan
New Contributor
6 years ago

Calling java class from mock script

Hi,   I have a mock soap service and want to  do some magic with basic auth headers. When I try to import the java.utils.Base64 I get class not found errrors. Are the std java classes not on the ...
  • MartinSpamer's avatar
    MartinSpamer
    6 years ago

    To use a Java class such as Base64 you must have the import and the jdk must be on the class path.  Goovy does include the following classes by default.

     

    import java.lang.*
    import java.util.*
    import java.io.*
    import java.net.*
    import groovy.lang.*
    import groovy.util.*
    import java.math.BigInteger
    import java.math.BigDecimal

     

    However in this case Groovy already has built in base64 encoding/decoding.  http://docs.groovy-lang.org/2.4.3/html/api/org/codehaus/groovy/runtime/EncodingGroovyMethods.html

     

    String encoded = 'AUTHORISATION-TOKEN'.bytes.encodeBase64().toString()
    log.info encoded

    byte[] decoded = encoded.decodeBase64()
    log.info new String(decoded)

     

    I'd question the need for mock to actually need to authorisatise, in most cases I test for their presence not their values.