title: Constraints like @Assert\NotBlank() annotation for entities does not work in unit test which gives semantical or auto loaded error
body: <p>When I run the Unit test it gives the following error for the constraints</p>
  
  <p><strong>[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\NotBlank" in property AppBundle\Entity\User::$username does not exist, or could not be auto-loaded.</strong></p>
  
  <p>Here is my entity</p>
  
  <p><strong>User.class</strong></p>
  
  <pre><code>namespace AppBundle\Entity;
  
  use Symfony\Component\Validator\Constraints as Assert;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  
  
  /**
   * Class User
   * @package AppBundle\Entity
   *
   * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
   * @ORM\Table(name="user")
   * @ORM\HasLifecycleCallbacks()
   *
   */
  class User implements AdvancedUserInterface, \Serializable
  {
      /** @var double
       *  @ORM\Column(type="bigint", nullable=false)
       *  @ORM\Id
       *  @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @var string
       * @ORM\Column(type="string", length=60, unique=true)
       * @Assert\NotBlank()
       * @Assert\Email()
       */
      private $username;
  
      /**
       * @ORM\Column(type="string", length=64)
       */
      private $password;
  
      /**
       * @Assert\NotBlank()
       * @Assert\Length(max=4096)
       */
      private $plainPassword;
  
      /**
       * @var string
       * @ORM\Column(name="fullname", type="string", length=100, nullable=false)
       * @Assert\NotBlank()
       */
      protected $fullname;
  
      /**
       * @var \DateTime
       * @ORM\Column(name="createdat", type="datetime", nullable=true)
       */
      protected $createdat;
  
      /**
       * @var \DateTime
       * @ORM\Column(name="modifiedat", type="datetime", nullable=true)
       */
      protected  $modifiedat;
  
      /**
       * @ORM\Column(type="boolean", options={"default" = 1}, nullable=false)
       */
      private $isactive = true;
  
  
      public function __construct()
      {
          $this-&gt;updatedTimestamps();
      }
  
      /**
       * Now we tell doctrine that before we persist or update we call the updatedTimestamps() function.
       *
       * @ORM\PrePersist
       * @ORM\PreUpdate
       */
      public function updatedTimestamps()
      {
          $this-&gt;setModifiedat(new \DateTime(date('Y-m-d H:i:s')));
  
          if($this-&gt;getCreatedat() == null)
          {
              $this-&gt;setCreatedat(new \DateTime(date('Y-m-d H:i:s')));
          }
      }
  
      public function getUsername()
      {
          return $this-&gt;username;
      }
  
      public function getSalt()
      {
          // you *may* need a real salt depending on your encoder
          // see section on salt below
          return '';
      }
  
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      public function getRoles()
      {
          return array('ROLE_USER');
      }
  
      public function eraseCredentials()
      {
      }
  
      public function isAccountNonExpired()
      {
          return true;
      }
  
      public function isAccountNonLocked()
      {
          return true;
      }
  
      public function isCredentialsNonExpired()
      {
          return true;
      }
  
      public function isEnabled()
      {
          return $this-&gt;isactive;
      }
  
      /** @see \Serializable::serialize() */
      public function serialize()
      {
          return serialize(array(
              $this-&gt;id,
              $this-&gt;username,
              $this-&gt;password,
              $this-&gt;isactive
          ));
      }
  
  
      /** @see \Serializable::unserialize() */
      public function unserialize($serialized)
      {
          list (
              $this-&gt;id,
              $this-&gt;username,
              $this-&gt;password,
              $this-&gt;isactive
              ) = unserialize($serialized);
      }
  
  
      /**
       * Get id
       *
       * @return integer
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set username
       *
       * @param string $username
       *
       * @return User
       */
      public function setUsername($username)
      {
          $this-&gt;username = $username;
  
          return $this;
      }
  
      /**
       * Set password
       *
       * @param string $password
       *
       * @return User
       */
      public function setPassword($password)
      {
          $this-&gt;password = $password;
  
          return $this;
      }
  
      /**
       * Set isactive
       *
       * @param boolean $isactive
       *
       * @return User
       */
      public function setIsactive($isactive)
      {
          $this-&gt;isactive = $isactive;
  
          return $this;
      }
  
      /**
       * Get isactive
       *
       * @return boolean
       */
      public function getIsactive()
      {
          return $this-&gt;isactive;
      }
  
      /**
       * @return mixed
       */
      public function getPlainPassword()
      {
          return $this-&gt;plainPassword;
      }
  
      /**
       * @param mixed $plainPassword
       */
      public function setPlainPassword($plainPassword)
      {
          $this-&gt;plainPassword = $plainPassword;
      }
  
  
  
      /**
       * Set fullname
       *
       * @param string $fullname
       *
       * @return User
       */
      public function setFullname($fullname)
      {
          $this-&gt;fullname = $fullname;
  
          return $this;
      }
  
      /**
       * Get fullname
       *
       * @return string
       */
      public function getFullname()
      {
          return $this-&gt;fullname;
      }
  
      /**
       * Set createdat
       *
       * @param \DateTime $createdat
       *
       * @return User
       */
      public function setCreatedat($createdat)
      {
          $this-&gt;createdat = $createdat;
  
          return $this;
      }
  
      /**
       * Get createdat
       *
       * @return \DateTime
       */
      public function getCreatedat()
      {
          return $this-&gt;createdat;
      }
  
      /**
       * Set modifiedat
       *
       * @param \DateTime $modifiedat
       *
       * @return User
       */
      public function setModifiedat($modifiedat)
      {
          $this-&gt;modifiedat = $modifiedat;
  
          return $this;
      }
  
      /**
       * Get modifiedat
       *
       * @return \DateTime
       */
      public function getModifiedat()
      {
          return $this-&gt;modifiedat;
      }
  
      /**
       * The __toString method allows a class to decide how it will react when it is converted to a string.
       *
       * @return string
       * @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring
       */
      function __toString()
      {
          return "id: ". $this-&gt;id ."  email: ". $this-&gt;username . "  fullname: ". $this-&gt;fullname . "  isactive: ". $this-&gt;isactive .
          "  createdat: ". $this-&gt;createdat-&gt;format('Y-m-d H:i:s') ."  updatedat: ". $this-&gt;modifiedat-&gt;format('Y-m-d H:i:s');
      }
  }
  </code></pre>
  
  <p>This is my Unit Test classes:</p>
  
  <p><strong>TestBase.class</strong></p>
  
  <pre><code>namespace tests\AppBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Console\Application;
  use Symfony\Component\DependencyInjection\Container;
  use Doctrine\Common\Annotations\AnnotationRegistry;
  use AppKernel;
  
  AnnotationRegistry::registerFile("../../../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
  require_once __DIR__ . "/../../../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php";
  
  
  class TestBase extends \PHPUnit_Framework_TestCase
  {
  
      /**
       * @var Application
       */
      protected $application;
  
      /**
       * @var \Doctrine\ORM\EntityManager
       */
      protected $entityManager;
  
      /**
       * @var \AppKernel
       */
      protected $kernel;
  
      /**
       * @var Container
       */
      protected $container;
  
      protected function setUp()
      {
          $this-&gt;kernel = new AppKernel("test", true);
          $this-&gt;kernel-&gt;boot();
          $this-&gt;application = new Application($this-&gt;kernel);
          $this-&gt;application-&gt;setAutoExit(false);
  
          // Store the container and the entity manager in test case properties
          $this-&gt;container = $this-&gt;kernel-&gt;getContainer();
          $this-&gt;entityManager = $this-&gt;container-&gt;get("doctrine")-&gt;getManager();
  
          if(is_null($this-&gt;entityManager)){
              print("upssss entitiy manager is null :(");
          }
  
          parent::setUp();
      }
  
      public function tearDown()
      {
          // Shutdown the kernel.
          $this-&gt;kernel-&gt;shutdown();
  
          parent::tearDown();
      }
  
  }
  </code></pre>
  
  <p>And here I test my User class just printing the database.. </p>
  
  <p><strong>UserTest.class</strong></p>
  
  <pre><code>namespace tests\AppBundle\Controller;
  
  require_once ("TestBase.php");
  
  class UserTest extends TestBase
  {
      protected function setUp()
      {
          parent::setUp();
      }
  
      public function tearDown()
      {
          parent::tearDown();
      }
  
      //generic method to list the users
      public function listUsers($users ){
          echo EOL, EOL;
          foreach($users as $user){
              echo $user, EOL;
          }
          echo EOL, EOL;
      }
  
      public function testListUsers(){
  
          $users = $this-&gt;entityManager-&gt;getRepository('AppBundle:User')-&gt;findAll();
          $this-&gt;assertGreaterThan(1, count($users));
          $this-&gt;listUsers($users);
      }
  }
  </code></pre>
  
  <p>By the way it works when I don't use @Assert\NotBlank()
  So from code-wise there is no problem at all... I guess it is just about autoloading when unit testing.. </p>
  
  <p>I really got stuck for 2 weeks with that.. </p>