SpringMVC Junit 注解相关问题

使用Junit测试数据库的增删改查,DAO继承JPA,在SpringMVC框架下采用的@Autowired的方式添加DAO,所以用Junit进行测试的时候不能直接使用A a = new A();这样的方式实例化,也需要采用@Autowired的方式实例化;

如果未实例化会出现DAO使用不了JAP的接口。

实例化方法:
pom.xml 中添加:

    
      org.springframework
      spring-test
      3.2.3.RELEASE
      test
    

Junit自动生成在了test目录下:

SpringMVC Junit 注解相关问题_第1张图片
自动生成方式参考网络

自动生成的Junit(片段)中加入:

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class UserPrivateOpeImpTest {

    @Autowired
    private UserPrivateOpeImp userPrivateOpeImp;

其中需要注意的是ContextConfiguration添加 “SpringMVC配置文件” 路径格式

参考链接
查看注解文件路径
配置pom和注解文件
添加注解文件路径
Junit API

另外想问一下:在网上查找资料的时候看到了Android相关资料也有很多在使用Junit,也有相当多的注解,是有什么样的关联呢?

你可能感兴趣的:(SpringMVC Junit 注解相关问题)