You can use JPA outside of application containers. When running within an application container, you're using container-managed entity managers. You can use JPA in plain JavaSE applications. In those cases, you want an application-managed entity manager. Which is using the javax.persistence.Persistence class to get the EntityManagerFactory (from which you get the EntityManager).
Once you have the entityManager, you can perform the same code. All you need to do in your tests is ensure you can get at the Persistence class, and you should be all good.