1

I've got the following test I'm trying to run. In this case just a blank test to try it out but for some reason it's not working.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
@Transactional
public class UserHibernateDaoTest {

    private static final long USERID = 1;
    private static final long NONEXISTENTUSERID = -1;
    private static final String FIRSTNAME = "TestFirstName";
    private static final String LASTNAME = "TestLastName";
    private static final String EMAIL = "[email protected]";
    private static final String PASSWORD = "TestPassword";
    private static final String PHONENUMBER = "0000000";
    private static final String ROLE = "USER";

    @PersistenceContext
    private EntityManager em;

    private UserHibernateDao userHibernateDao;  
    private JdbcTemplate jdbcTemplate;


    @Before
    @Transactional
    public void setUp() {
            this.userHibernateDao = new UserHibernateDao();
            User u;
            u = new User();
            u.setUserid(123);
            u.setFirstName(FIRSTNAME);
            u.setLastName(LASTNAME);
            u.setEmail(EMAIL);
            u.setPassword(PASSWORD);
            u.setPhoneNumber(PHONENUMBER);
            u.setRole(ROLE);
            em.persist(u);

    }

    @Rollback
    @Test
    public void testCreate() {
            // Trying to run this empty test
    }
}

I get the following when I run it:

INFO: Began transaction (1) for test context [DefaultTestContext@16fb356 testClass = UserHibernateDaoTest, testInstance = ar.edu.itba.paw.persistence.UserHibernateDaoTest@6bc248ed, testMethod = testCreate@UserHibernateDaoTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@224b4d61 testClass = UserHibernateDaoTest, locations = '{}', classes = '{class ar.edu.itba.paw.persistence.TestConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@23a9ba52]; rollback [true]
Jan 15, 2020 1:17:10 PM org.springframework.test.context.transaction.TransactionContext endTransaction
INFO: Rolled back transaction for test context [DefaultTestContext@16fb356 testClass = UserHibernateDaoTest, testInstance = ar.edu.itba.paw.persistence.UserHibernateDaoTest@6bc248ed, testMethod = testCreate@UserHibernateDaoTest, testException = java.lang.IllegalArgumentException: Unknown entity: ar.edu.itba.paw.models.User, mergedContextConfiguration = [MergedContextConfiguration@224b4d61 testClass = UserHibernateDaoTest, locations = '{}', classes = '{class ar.edu.itba.paw.persistence.TestConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].

If change Rollback() to Rollback(false) I get:

 RollbackException: Transaction marked as rollbackOnly

Any ideas how to fix this?

In case you find it neccesary I published my TestConfig in this question.

2 Answers 2

0

Spring do not find you User class as entity. So it does not know about User being an entity. You need to add entity package to component scan in TestConfig class.

@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence", "ar.edu.itba.paw.models" })
Sign up to request clarification or add additional context in comments.

6 Comments

I've been able to test inside my services section importing the User model and it works (instead of adding it to componentScan), so I was trying to do the same here. Anyways, I tried your solution and I still get the same exception.
in your configuration you are setting factoryBean.setPackagesToScan("src.main.java.ar.edu.itba.paw.models"); it should start from ar.edu.itba
Unfortunately no, though I managed to get my test passed commenting a few more lines. I still get the testException though.
@ComponentScan is for beans. @EntityScan is for entities.
@Stackoverflowed can you post some code on github it should work. @neildo I have my application where it is running without using @EntityScan may be I am using different version
|
0

Annotate your TestConfig class with @EntityScan("ar.edu.itba.paw.models"). Also make sure your entity class is annotated with @Entity

2 Comments

I'm using SpringMVC, I'm not sure this is the solution?
You are are using EntityManager which is JPA. This should work. Give it a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.