title: @EntityListeners Injection + jUnit Testing
body: <p>I use <code>@EntityListeners</code> to make operations before I save in my Db and after I load.
  Inside my Listener class I make a call to an <code>Ecryptor</code> (which needs to fetch info from configuration file), so the encryptor can't be called statically and need to be injected in my Listener. Right?</p>
  
  <p>Well, injections in EntityListeners can't be done straight away, but you have some methods to do that, like using <code>SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);</code> or even the method showed here. <a href="https://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/" rel="noreferrer">https://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/</a></p>
  
  <p>Cool, the problem is: None of the solutions support unit testing! When running tests that encryptor I had injected in my model Listener is always <code>null</code>.</p>
  
  <p>Here <a href="https://stackoverflow.com/questions/25922956/springbeanautowiringsupport-does-not-inject-beans-in-junit-tests">SpringBeanAutowiringSupport does not inject beans in jUnit tests</a> There is a solution to create this context and pass to a instantiated object, but it does not solve my problem since I have the "Injection" to add to it.</p>
  
  <p>Any way to create a context in my tests and somehow pass it to my listeners?
  If not, any way I can create a static method to my Encryptor and still have access to the Environment API to read my properties?</p>
  
  <p>Package Listener:</p>
  
  <pre><code>public class PackageListener{
     @Autowired
     Encryptor encryptor;
  
     @PrePersist
     public void preSave(final Package pack){
        pack.setBic(encryptor.encrypt(pack.getBic()));
     }
     ...
  </code></pre>
  
  <p>My test</p>
  
  <pre><code> @Test
   @WithuserElectronics
   public void testIfCanGetPackageById() throws PackageNotFoundException{
       Package pack = packagesServiceFactory.getPackageService().getPackage(4000000002L);
   }
  </code></pre>
  
  <p>Package service</p>
  
  <pre><code>  public Package getPackage(Long id) throws PackageNotFoundException{
      Package pack = packageDao.find(id);
  
      if (pack == null) {
          throw new PackageNotFoundException(id);
      }
  
      return pack;
  }
  </code></pre>
  
  <p>Encryptor:</p>
  
  <pre><code>public class Encryptor{
      private String salt;
  
      public Encryptor(String salt){
          this.salt = salt;
      }
  
      public String encrypt(String string){
          String key = this.md5(salt);
          String iv = this.md5(this.md5(salt));
          if (string != null) {
              return encryptWithAesCBC(string, key, iv);
          }
          return string;
      }
      ...
  </code></pre>