- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定一个 Spring Boot + Thymeleaf Web 应用程序(这与 Spring 项目的 gs-consuming-rest "initial" code tree 几乎相同):
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── hello
│ │ ├── Application.java
│ │ ├── Config.java
│ │ └── Controller.java
│ └── resources
│ └── templates
│ └── index.html
└── test
└── java
└── hello
└── ControllerTest.java
...用户会收到“Hello World!”的问候语。在 http://localhost:8080/
,但 Spring 的“上下文”的连接似乎不适用于集成测试(ControllerTest.java
):
java.lang.AssertionError: Status
Expected :200
Actual :404
测试中的项目布局和/或配置注释有什么问题?
src/main/webapp/
是有意缺失的,还有 web.xml
和 WEB-INF/
之类的东西。这里的目标是使用最少的配置,通过集成测试来测试驱动应用程序的 View 和 Controller 的开发。
血淋淋的细节如下。提前为“文字墙”道歉。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
package hello;
// ...
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
package hello;
@org.springframework.stereotype.Controller
public class Controller {
}
package hello;
// ...
@Configuration
public class Config extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
}
package hello;
// ...
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Config.class)
public class ControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void test() throws Exception {
this.mockMvc
.perform(get("/"))
.andExpect(status().isOk());
}
}
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
最佳答案
感谢@M.Deinum 帮助我意识到这一点:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Config.class)
public class ControllerTest {
...应该是:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = Application.class)
public class ControllerTest {
我想 @ContextConfiguration
用于 Spring 中的集成测试,而 @SpringApplicationConfiguration
用于 Spring 中的集成测试启动。
Class-level annotation that is used to determine how to load and configure an ApplicationContext for integration tests.
Similar to the standard @ContextConfiguration but uses Spring Boot's SpringApplicationContextLoader.
关于java - Spring Boot + Thymeleaf 的@WebAppConfiguration 和@ContextConfiguration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34606760/
假设我有一个这样的父测试类: @ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { MyCus
如果我有 xml 配置文件,我会这样写: @ContextConfiguration(locations = "testContext.xml") 但是如果我通过注释标记我的bean,我必须写什么?
我有一个 spring 单元测试,它扩展了其他项目(tests-common)中的其他测试。我的上下文文件 (my-context.xml) 与 MyTest 位于同一包中。基本上下文文件(“base
我正在尝试创建某种集成测试环境,但当测试上下文框架没有为我的 bean 创建事务代理时遇到问题。我的代码: JUnit 类:FileServiceImplTest @RunWith(SpringJUn
我刚刚空降到一个 spring 项目中,他们不相信单元测试。 我正在尝试建立自己的单元测试环境。 我可以知道注释 @ContextConfiguration 指的是什么吗? @Repository 呢
我在我的项目中创建了一个新测试。对于这个,我使用了 @ContextConfiguration 和一个内部配置类,与测试在同一个类中。但是现在我的其他测试都失败了,因为它们正在使用新测试的配置。 这怎
我正在使用 @ContextConfiguration 注释来管理我的应用程序中的配置。创建配置以便它们仅提供由给定模块公开的 beans。因此,给定模块使用的一些 bean 不一定直接导入。示例:
我正在运行下一个测试: import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.run
我使用以下配置进行数据库集成测试: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "class
我正在学习Java和Spring MVC。我找到了这段代码: @ContextConfiguration(locations = { "classpath: com/myname/spring/jun
我正在从我的存储库项目编写集成测试,它需要加载我的基础设施项目的 IT 测试上下文,但由于某些原因,它因一些奇怪的异常而失败 java.lang.ArrayStoreException: sun.r
在下面的测试类中 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.fa
为了 DRY,我想在父类中定义我的 ContextConfiguration 并让我的所有测试类继承它,如下所示: 父类: package org.my; @RunWith(SpringJUnit4C
在我们的项目中,我们正在编写一个测试来检查 Controller 是否返回正确的模型 View @Test public void controllerReturnsModelToOverzi
我有这个空的测试类 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class FindHandlerTest
当我的测试类作为 JUnit Test 运行时,控制台显示: INFO: Neither @ContextConfiguration nor @ContextHierarchy found for t
我目前正在使用 springockito-annotations,这需要 @RunWith 和 @ContextConfiguration 注释才能工作。我想将这些注释放在我的测试的父类(super
我们有使用元注释的测试类: @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(locations = {"/web/
我遇到过这样的情况。我有一个 junit 测试类,因为我提到了 @ContextConfiguration但是该类从主/资源和其中包含的其他文件(DataSource.xml 和Hibernate.x
我想知道是否有一种方法可以减少我们当前为集成测试编写的样板文件的数量。 罪魁祸首是 ContextConfiguration,我们当前向其中发送了 7 个不同的字符串。 我们的一项测试如下所示(有效负
我是一名优秀的程序员,十分优秀!