Tôi có một ứng dụng web mùa xuân và tôi muốn thực hiện unittests cho bộ điều khiển của tôi. Tôi đã quyết định không sử dụng Spring để thiết lập các thử nghiệm của mình, nhưng để sử dụng các đối tượng giả Mockito kết hợp với các bộ điều khiển của tôi.Mockito Testcase bỏ qua chú thích
Tôi tạo và chạy thử nghiệm với Maven2 và plugin chắc chắn. Đây là từ pom.xml của tôi
<!-- 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>
tôi cài đặt trình biên dịch của tôi và plugin chắc chắn hơn như thế này:
<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>
lớp thử nghiệm của tôi trông như thế này:
@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());
}
Chú thích tất cả xung quanh: -)
Nhưng khi xây dựng từ dòng lệnh tôi nhận được một NullPointerException trong dòng khi (dataEntityTypeF acade.getEntityTypeFromTypeName (sai)). thenReturn (null); vì đối tượng dataEntityTypeFacade là null. Khi tôi chạy testcase của tôi trong Eclipse tất cả là tốt và các đối tượng giả của tôi được khởi tạo và phương thức được chú thích bằng @Before được gọi.
Tại sao chú thích của tôi dường như bị bỏ qua khi chạy từ dòng lệnh ???
/Eva
Bằng cách "xây dựng từ dòng lệnh", bạn có nghĩa là một maven xây dựng hoặc cái gì khác? –