У меня есть веб-приложение Spring, и я хочу сделать юнит-тесты для моих контроллеров.Я решил не использовать 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>
Я настраиваю свой компилятор и верные плагины так:
<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.
Почему мои аннотации, по-видимому, игнорируются при запуске из командной строки ???
/ Eva