Mockito Testcase 忽略注释
我有一个 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(wrong) )).thenReturn(null);因为 dataEntityTypeFacade 对象为 null。当我在 Eclipse 中运行测试用例时,一切都很好,我的模拟对象被实例化,并且调用用 @Before 注释的方法。
为什么从命令行运行时我的注释似乎被忽略了???
/伊娃
I have a Spring web application and I want to make unittests for my controllers. I have decided not to use Spring to setup my tests but to use Mockito mock objects in conjunction with my controllers.
I build and run tests with Maven2 and the surefire plugin. This is from my 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>
I setup my compiler and surefire plugins like this:
<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>
My test class looks like this:
@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());
}
Annotations all around :-)
But when building from the commandline i get a NullPointerException in the line when(dataEntityTypeFacade.getEntityTypeFromTypeName(wrong)).thenReturn(null); as the dataEntityTypeFacade object is null. When I run my testcase in Eclipse all is well and my mock objects are instantiated and the method annotated with the @Before is called.
Why are my annotations seemingly ignored when running from the command line???
/Eva
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否
在基类或测试运行程序中调用过:http:// docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#9
Have you called:
in the base class or a test runner as mention here: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#9