89071388 / powermock

Automatically exported from code.google.com/p/powermock
0 stars 0 forks source link

Unit Testing JPA code requires annotated classes to be ignored #456

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Use the Powermockrunner and prepare a static class for test

What is the expected output? What do you see instead?
I expect the test to pass, but I get a PropertyAccessException from hibernate 
because its trying to set the wrong types to the properties.

Workaround:
Add the package(s) where the JPA entites are located to the @PowerMockIgnore 
annotation.

I don't think the above should be necessary but it is and I couldn't find this 
documented anywhere. I also had to ignore the persistence packages although the 
FAQ says this isn't neccessary after version 1.3

Please provide any additional information below.
Details of my POM file and the Exception can be found here 
http://stackoverflow.com/questions/18566988/unit-testing-dao-fails-on-persist-wi
th-hsqldb-and-hibernate

mock versions
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.5.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-easymock</artifactId>
        <version>1.5.1</version>
        <scope>test</scope>
    </dependency>

Test Case

@RunWith(PowerMockRunner.class)
@PrepareForTest(DaoLoader.class)
@PowerMockIgnore({"javax.persistence.*"})
public class UserParserTest {

    private static EntityManagerFactory emf;
    private EntityManager em;

    @BeforeClass
    public static void initializeEMF(){
        emf = Persistence.createEntityManagerFactory("snowmore");
    }

    @AfterClass
    public static void closeEntityManagerFactory() {
        emf.close();
    }

    @Before
    public void setUp() throws Exception {
        em = emf.createEntityManager();
        em.getTransaction().begin();

        //This is all to get the em into the dao without PersistenceContext
        dao = new UserDao(em);
        PowerMock.mockStatic(DaoLoader.class);

        PowerMock.replay(DaoLoader.class);
    }

    @After
    public void rollbackTransaction() {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }

        if (em.isOpen()) {
            em.close();
        }

        PowerMock.verify(DaoLoader.class);
    }

    @Test
    public void testPersist() throws Exception {
        User firstOccurence = new User();
        firstOccurence.setUsername("test_user");
        em.persist(firstOccurence);
        em.flush();
    }
}

Entity

@Entity
@Table(name = "REPORTER")
public class User{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "REPORTER_ID", nullable = false, unique = true)
    private Long id;

    @Column(name = "REPORTER_USERNAME", nullable = false, unique = true)
    private String username;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username){
        this.username = username;
    }
}

Original issue reported on code.google.com by edwards....@gmail.com on 2 Sep 2013 at 3:58