SSM框架添加junit单元测试
1.添加pom依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--加入spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
2.添加test类
3.配置test类
4.AccountItemServiceTest类
public class AccountItemServiceTest extends CommonService {
@Test
public void queryAccountingSubject(){
List<AccountItemModel> accountItemModel = accountItemService.queryAccountingSubject("会计分录");
}
}
5.CommonService类
public class CommonService {
public BeanFactory beanFactory;
public AccountItemService accountItemService;
@Before
public void setUp(){
beanFactory = new ClassPathXmlApplicationContext("spring/spring.xml");
accountItemService = (AccountItemService) beanFactory.getBean("accountItemService");
}
}
new ClassPathXmlApplicationContext 说明用的sping容器时appliationContext。
启动测试类成功。
为什么要用单元测试?
直接从测试类开始,不用重新启动项目,高效。我这里配置的是调试service,可以根据项目需求配置不同的common…。
遇到问题
项目中用到了dubbo,但是没有配置好 ,报启动报java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy问题
添加如下的依赖就好了
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
还没有评论,来说两句吧...