今天打算吧junit整合到SSH框架中,因為之前已經整合過一次了,所以感覺應該沒什么問題,主要就是導入一個org.springframework.test-3.0.5.RELEASE.jar和junit.jar,
然后直接寫測試文件:
import static org.junit.Assert.assertEquals; import java.util.List; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.togo.bean.member.Account; import cn.togo.service.member.AccountService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:beans.xml" }) public class TestDemo extends AbstractJUnit4SpringContextTests { @Resource private AccountService accountService; @Test public void test1() { // System.out.println(accountService.getAccountByUsername("test") .getUsername()); // System.out.println(accountService.getAccountList()); assertEquals("test", accountService.getAccountByUsername("test") .getUsername()); // System.out.println( accountService.getCount()); List<Account> accounts = accountService.getAllAccountList(); for (Account account : accounts) { print_account(account); } } /** * print account * * @param account */ private void print_account(Account account) { System.out.print("name:" + account.getUsername()); System.out.print(" id:" + account.getId()); System.out.println(); } }但是,報錯了,
Unexpected exception parsing XML document from class path resource [beans.xml],
javax.xml.parsers.DocumentBuilderFactory.setAttribute(Ljava/lang/String;Ljava/lang/Object;)V
在網上搜索有人說是包沖突了,找了半天也沒找到哪里沖突了,沒辦法 就把之前的項目拿出來跟現在的對比下,到底哪里不一樣,
發現之前的項目用的是JAVA EE 6 Libraries 而現在用的是JAVA 1.4 Libraries,於是就把現在的也換成6的了,沒想到居然成功了,
不過還是不太理解怎么回事。