SpringMVC Junit 注解相关问题

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

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

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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.2.3.RELEASE</version>
      <scope>test</scope>
    </dependency>

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

自动生成方式参考网络

自动生成的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,也有相当多的注解,是有什么样的关联呢?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容