如何使用JPA从junit命中数据库

时间:2014-06-17 10:28:36

标签: junit

我正在编写junit测试用例,如何使用junitjpa点击数据库。

我编写了代码,但得到了以下异常

javax.persistence.PersistenceException: No Persistence provider for EntityManager named smsPU

我在provide文件中添加了src/test/resources/META-INF/persistent.xml,但我收到了错误。

我发布了我的代码,请检查此代码中的错误位置。

public class SmsBeanTest {

    private SmsBean smsBean;
    private SmsNotification notification;
    private EntityManagerFactory entity;
    private EntityManager em;

    @Before
    public void setUp() throws Exception {

        String CONFIG_ATTR_NAME = "webappConfig";
        smsBean = new SmsBean();
        ServletContext ctx = mock(ServletContext.class);
        Configuration config = new Configuration();
        config.initProperties("syniverse-sms.properties");
        when(ctx.getAttribute(CONFIG_ATTR_NAME)).thenReturn(config);

        smsBean.setServletContext(ctx);

        **//em = mock(EntityManager.class);
        Properties prop = new Properties();
        // Ensure RESOURCE_LOCAL transactions is used.
        prop.put(TRANSACTION_TYPE,
                PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
        // Configure the internal connection pool
        prop.put(JDBC_DRIVER, "com.mysql.jdbc.Driver");
        prop.put(JDBC_URL, "jdbc:mysql://localhost:3306/platform_service_db");
        prop.put(JDBC_USER, "root");
        prop.put(JDBC_PASSWORD, "root");

        prop.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, 
                "src/test/resources/META-INF/persistence.xml/lib/fdn-persistence-1.0.0-SNAPSHOT.jar");

       entity = Persistence.createEntityManagerFactory("smsPU", prop);
       System.out.println(entity);
        em = entity.createEntityManager(prop);
        smsBean.setEm(em);**
        // doNothing().when(em).persist(notification);
        smsBean.init();

        notification = new SmsNotification();

    }

    @After
    public void tearDown() throws Exception {
        smsBean = null;
        notification = null;
        //entity.close();
        //em.close();

    }

    @Test
    public void testSendSms() {

        SmsNotificationDTO sms = new SmsNotificationDTO();

        sms.setToAddress("9985291980");
        sms.setMessage("Sending Message...");

        try {

            SyniverseDispatcher disp = mock(SyniverseDispatcher.class);
            SyniverseResponse resp = new SyniverseResponse();
            resp.setResponseStr("1234");
            String smsTo = notification.getDestination();
            String smsMsg = notification.getMessage() + new Date().getTime();
            String urlStr = smsBean.getHostname() + "?user=" + smsBean.getUser() + "&pass=" + smsBean.getPass() + "&smsfrom=" + smsBean.getShortCode() + "&smsto=" + sms.getToAddress() + "&smsmsg=" + sms.getMessage();
            SyniverseRequest req = new SyniverseRequest();
            req.setRequestURL(urlStr);
            when(disp.dispatch(req)).thenReturn(resp);

            smsBean.sendSms(sms);
            assertNotNull(sms.getToAddress());
            assertEquals(10, sms.getToAddress().length());
            assertNotNull(sms.getMessage());

            assertEquals("1234", resp.getResponseStr());
        } catch (SmsException e) {
            fail(e.getMessage());
        }
    }
}

请帮助我,

1 个答案:

答案 0 :(得分:0)

找不到persistence.xml,因为它定义不正确。 "src/test/resources/META-INF/persistence.xml/lib/fdn-persistence-1.0.0-SNAPSHOT.jar"不应列出jar文件。