Je suis nouveau au concept de test unitaire avec les contrôleurs Spring. Je suis quelques exemples que j'ai trouvés en ligne et essayant de mettre en application leur stratégie de test. Ceci est mon contrôleur de base:Comment tester l'unité Spring Controller à l'aide d'annotations?
@Controller
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value = "name2", required = false, defaultValue = "World2") String name2,
Model model) {
model.addAttribute("name", name2);
return "greeting";
}
}
Ceci est mon test unitaire:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class ControllerGreetingTest {
private MockMvc mockMvc;
@Autowired
GreetingController greetingController;
@Test
public void shouldReturnSomething() throws Exception {
mockMvc.perform(get("/greeting"))
.andExpect(status().isOk())
.andExpect(view().name("greeting"));
}
}
Semble assez simple, mais je reçois l'erreur suivante:
java.lang.IllegalStateException: Neither GenericXmlWebContextLoader nor AnnotationConfigWebContextLoader was able to detect defaults, and no ApplicationContextInitializers were declared for context configuration [[email protected] declaringClass = 'com.practice.demo.ControllerGreetingTest', locations = '{}', classes = '{}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.ContextLoader']
Je suppose que j'ai pour ajouter un paramètre à l'annotation @ContextConfiguration
mais ne savez pas quoi inclure.
EDIT = C'est ce que j'ai jusqu'à présent:
public class ControllerGreetingTest {
private MockMvc mockMvc;
@Before
public void setup(){
this.mockMvc = standaloneSetup(new GreetingController()).build();
}
@Test
public void shouldReturnDefaultString() throws Exception {
mockMvc.perform(get("/greeting"))
.andExpect(status().isOk())
.andExpect(view().name("greetings"))
.andExpect(model().attribute("name","World2"));
}
}
Il fait le travail, mais il n'utilise pas des annotations de printemps comme je l'ai essayé de le faire avant .. cette approche n'est pas bon si essayer de comprendre pourquoi je garde des erreurs de lecture chaque fois que j'inclue les annotations dans mon fichier de test.
Mon POM:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.7.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
Essayez d'utiliser @RunWith (SpringRunner.class) si nous parlons de démarrage du printemps – Antoniossss
@Antoniossss J'utilise botte printemps. Quelle dépendance dois-je inclure dans le pom pour inclure le SpringRunner.class car j'obtiens un 'ne peut pas résoudre le symbole 'SpringRuner'' – dfqupwndguerrillamailde
org.springframework.boot: spring-boot-starter-test – Antoniossss