How to use JUnit tests with Spring Roo? (Problems with EntityManager)

后端 未结 7 585
忘了有多久
忘了有多久 2021-01-02 19:40

I\'m trying to write a JUnit test for a Spring Roo project. If my test requires use of the entity classes, I get the following Exception:

java.lang.IllegalS         


        
7条回答
  •  没有蜡笔的小新
    2021-01-02 20:41

    This worked for me with Spring Roo:

    import static org.junit.Assert.assertEquals;
    
    import org.junit.Test;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
    
    import com.jitter.finance.analyzer.domain.Address;
    
    @ContextConfiguration(locations = { "classpath*:/META-INF/spring/applicationContext*.xml"})
    public class EmTest extends AbstractJUnit4SpringContextTests {
    
        @Test
        public void checkEm(){
            Address a = new Address();
            a.setName("Primo");
            a.persist();
    
            Address b = new Address();
            b.setName("Secondo");
            b.persist();
    
            for(Address ad : Address.findAllAddresses()){
                System.out.println(ad.getName());
                assertEquals(ad.getName().charAt(ad.getName().length()-1), 'o');
            }
        }
    }
    

    With Address class like this:

    import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
    import org.springframework.roo.addon.javabean.annotations.RooToString;
    import org.springframework.roo.addon.jpa.annotations.activerecord.RooJpaActiveRecord;
    
    @RooJavaBean
    @RooToString
    @RooJpaActiveRecord
    public class Address {
        private String name;
    }
    

提交回复
热议问题