java – Mockito Testcase忽略注释
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Mockito Testcase忽略注释,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2405字,纯文字阅读大概需要4分钟。
内容图文
![java – Mockito Testcase忽略注释](/upload/InfoBanner/zyjiaocheng/826/dad73c1258884d8990f78ba66ef3aa2f.jpg)
我有一个Spring Web应用程序,我想为我的控制器进行单元测试.我决定不使用Spring来设置我的测试,而是将Mockito模拟对象与我的控制器结合使用.
我使用Maven2和surefire插件构建并运行测试.这是我的pom.xml
<!-- Test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.0-rc1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
我设置我的编译器和surefire插件,如下所示:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<compilerVersion>1.6</compilerVersion>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
</plugin>
我的测试类看起来像这样:
@RunWith(MockitoJUnitRunner.class)
public class EntityControllerTest {
private EntityController entityController;
private DataEntityType dataEntityType = new DataEntityTypeImpl("TestType");
@Mock
private HttpServletRequest httpServletRequest;
@Mock
private EntityFacade entityFacade;
@Mock
private DataEntityTypeFacade dataEntityTypeFacade;
@Before
public void setUp() {
entityController = new EntityController(dataEntityTypeFacade, entityFacade);
}
@Test
public void testGetEntityById_IllegalEntityTypeName() {
String wrong = "WROOONG!!";
when(dataEntityTypeFacade.getEntityTypeFromTypeName(wrong)).thenReturn(null);
ModelAndView mav = entityController.getEntityById(wrong, httpServletRequest);
assertEquals("Wrong view returned in case of error", ".error", mav.getViewName());
}
各地的注释:-)
但是当从命令行构建时,我在行中得到一个NullPointerException(dataEntityTypeFacade.getEntityTypeFromTypeName(错误)).thenReturn(null);因为dataEntityTypeFacade对象为null.当我在Eclipse中运行我的测试用例时,一切都很好,我的模拟对象被实例化,并且调用了使用@Before注释的方法.
从命令行运行时,为什么我的注释似乎被忽略了?
/伊娃
解决方法:
你打电话给:
MockitoAnnotations.initMocks(testClass);
在这里提到的基类或测试运行器:http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#9
内容总结
以上是互联网集市为您收集整理的java – Mockito Testcase忽略注释全部内容,希望文章能够帮你解决java – Mockito Testcase忽略注释所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。