gpt4 book ai didi

java - 如何使用注解对 Spring Controller 进行单元测试?

转载 作者:搜寻专家 更新时间:2023-11-01 01:49:25 26 4
gpt4 key购买 nike

我不熟悉使用 Spring Controller 进行单元测试的概念。我正在关注我在网上找到的一些示例并尝试实现他们的测试策略。这是我的基本 Controller :

@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";
}

}

这是我的单元测试:

@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"));
}

}

看起来很简单,但我收到以下错误:

java.lang.IllegalStateException: Neither GenericXmlWebContextLoader nor AnnotationConfigWebContextLoader was able to detect defaults, and no ApplicationContextInitializers were declared for context configuration [ContextConfigurationAttributes@1698539 declaringClass = 'com.practice.demo.ControllerGreetingTest', locations = '{}', classes = '{}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.ContextLoader']

我假设我必须向 @ContextConfiguration 注释添加一个参数,但不确定要在其中包含什么。

EDIT = 这是我目前所拥有的:

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"));
}

}

它完成了工作,但它没有像我之前尝试做的那样使用任何 Spring 注释。这种方法不好,所以试图弄清楚为什么每当我在测试文件中包含注释时我总是出错.

我的 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>

最佳答案

你应该使用 spring-boot-starter-test 依赖。它几乎包含所有用于测试的东西。

为了测试 spring 应用程序的 Controller 部分,您应该为您的测试类使用 @WebMvcTest 注释。使用此注释,spring 将为 Controller 部分加载上下文。另外,如果您使用此注释,则无需设置方法。您可以简单地 Autowiring mockMvc。你的测试类应该是这样的:

@RunWith(SpringRunner.class)
@WebMvcTest(GreetingController.class)
public class ControllerGreetingTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private SomeServiceClass someServiceClass;

@Test
public void shouldReturnDefaultString() throws Exception {
mockMvc.perform(get("/greeting"))
.andExpect(status().isOk())
.andExpect(view().name("greetings"))
.andExpect(model().attribute("name","World2"));
}

}

注意:您的 Controller 没有任何 Autowiring 的字段。如果 Controller 有一些 Autowiring 的对象,如服务或存储库对象。你可以简单地用注解模拟它们 @MockBean 正如你在上面的代码中看到的那样。

参见 this link对于 spring 提供的其他测试切片注释

关于java - 如何使用注解对 Spring Controller 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46745900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com