This may be due to the fact that, by default, auto-detection works for classes inside the same pathpath element where it is located persistence.xml.
So, you have separate target folders for the code itself and for the tests (for example, if you use Maven with the default setting), and if this one persistence.xmlgets into the target folder of the tests after compilation, the classes from the main target folder will not be detected.
<jar-file>, pathpath, .
Maven, , :
persistence.xml:
<jar-file>${project.build.outputDirectory}</jar-file>
pom.xml:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>