title: How to use a custom codec in a Grails unit test
body: <p>I am working through a Grails tutorial from InfoQ called Getting Started With Grails, Second Edition, and I am trying to add a custom codec to a unit test. My environment is Grails 1.3.7 and Groovy 1.7.8. </p>
  
  <p>So the codec is SHACodec.groovy, and it lives in grails-app/utils. The contents are:</p>
  
  <pre><code>import java.security.MessageDigest 
  
  class SHACodec{ 
    static encode = {target-&gt; 
      MessageDigest md = MessageDigest.getInstance('SHA') 
      md.update(target.getBytes('UTF-8')) 
      return new String(md.digest()).encodeAsBase64()     
    }     
  } 
  </code></pre>
  
  <p>The codec works just fine when I log into the app. It's being used for a password field in my UserController.authenticate()</p>
  
  <pre><code>def authenticate = { 
      def user =  
            User.findByLoginAndPassword(params.login, params.password.encodeAsSHA()) 
          if(user){ 
        session.user = user 
            flash.message = "Hello ${user.login}!" 
        redirect(controller:"race", action:"list")       
          }else{ 
        flash.message = "Sorry, ${params.login}. Please try again." 
            redirect(action:"login") 
      }         
      }
  </code></pre>
  
  <p>When I add this to a unit test, the following error displays:</p>
  
  <blockquote>
    <p>No such property: SHACodec for class:
    racetrack.UserControllerTests
    groovy.lang.MissingPropertyException:
    No such property: SHACodec for class:
    racetrack.UserControllerTests     at
    racetrack.UserControllerTests.testAuthenticate(UserControllerTests.groovy:39)</p>
  </blockquote>
  
  <p>The test is:</p>
  
  <pre><code>package racetrack
  
  import org.codehaus.groovy.grails.plugins.codecs.*
  import grails.test.*
  
  class UserControllerTests extends ControllerUnitTestCase {
      protected void setUp() {
          super.setUp()
  
          String.metaClass.encodeAsSHA = {-&gt;  
            SHACodec.encode(delegate) 
          }
      }
  
      protected void tearDown() {
          super.tearDown()
      }
  
  void testAuthenticate(){ 
  
    def jdoe = new User(login:"jdoe", password:"password".encodeAsSHA()) 
    mockDomain(User, [jdoe]) 
  
    controller.params.login = "jdoe" 
    controller.params.password = "password" 
    controller.authenticate() 
    assertNotNull controller.session.user 
    assertEquals "jdoe", controller.session.user.login 
  
    controller.params.password = "foo" 
    controller.authenticate() 
    assertTrue controller.flash.message.startsWith( 
      "Sorry, jdoe")       
  }
  </code></pre>
  
  <p>I found two different recommendations so far. 
  First, call the codec directly from the test:</p>
  
  <pre><code>SHACodec codec = new SHACodec()
  codec.encode("password")
  </code></pre>
  
  <p>Second, use an integration test instead of a unit test. </p>
  
  <p>The results were the same for each option. What is the small thing I am missing? </p>
  
  <p>Thanks in advance for your time!</p>