我已经使用JUnit 4和spring-test库编写了一些JUnit测试.当我在Eclipse中运行测试然后运行正常并通过.但是当我使用Maven运行它们时(在构建过程中),它们无法给出与弹簧相关的错误.我不确定导致问题的是什么,JUnit,Surefire或Spring.这是我的测试代码,弹簧配置和我从Maven获得的异常:
PersonServiceTest.java
package com.xyz.person.test;
import static com.xyz.person.util.FjUtil.toFjList;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.xyz.person.bo.Person;
import com.xyz.person.bs.PersonService;
import fj.Effect;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:personservice-test.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class PersonServiceTest {
@Autowired
private PersonService service;
@Test
@Transactional
public void testCreatePerson() {
Person person = new Person();
person.setName("abhinav");
service.createPerson(person);
assertNotNull(person.getId());
}
@Test
@Transactional
public void testFindPersons() {
Person …
Run Code Online (Sandbox Code Playgroud) 我想编写一些检查已部署WAR的XML Spring配置的测试.不幸的是,某些bean需要设置一些环境变量或系统属性.在使用带有@ContextConfiguration的方便测试样式时,如何在初始化spring bean之前设置环境变量?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext { ... }
Run Code Online (Sandbox Code Playgroud)
如果我使用注释配置应用程序上下文,我没有看到一个钩子,我可以在初始化spring上下文之前执行某些操作.
每当我看到一篇与Spring测试相关的博客文章时,我都会看到这些类中的任何一个,但却不了解真正的区别:
@RunWith(SpringRunner.class)
@RunWith(SpringJUnit4ClassRunner.class)
Run Code Online (Sandbox Code Playgroud) 我们有一堆JUnit测试用例(集成测试),它们在逻辑上分为不同的测试类.
我们能够为每个测试类加载一次Spring应用程序上下文,并将其重用于JUnit测试类中的所有测试用例,如http://static.springsource.org/spring/docs/current/spring-framework-reference中所述./html/testing.html
但是,我们只是想知道是否有一种方法只为一堆JUnit测试类加载一次Spring应用程序上下文.
FWIW,我们使用Spring 3.0.5,JUnit 4.5并使用Maven来构建项目.
首先,我在StackOverflow上发现了很多关于此的线程,但是没有一个真正帮助过我,所以很抱歉可能会重复提问.
我正在使用spring-test运行JUnit测试,我的代码看起来像这样
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {})
public class StudentSystemTest {
@Autowired
private StudentSystem studentSystem;
@Before
public void initTest() {
// set up the database, create basic structure for testing
}
@Test
public void test1() {
}
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我希望我的测试不会影响其他测试.所以我想为每个测试创建类似回滚的东西.我为此搜索了很多,但到目前为止我一无所获.我正在使用Hibernate和MySql
由于重复@RunWith
注释,以下代码无效:
@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(Parameterized.class)
@SpringApplicationConfiguration(classes = {ApplicationConfigTest.class})
public class ServiceTest {
}
Run Code Online (Sandbox Code Playgroud)
但是我如何结合使用这两个注释呢?
我想在 Gradle 项目中实施 Junit 5 测试。我试过这个:
梯度配置:
plugins {
id 'org.springframework.boot' version '2.5.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'test'
version = '0.0.1'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2020.0.4")
}
dependencies {
...............
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)
联合测试:
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;
public class GeneratePdf {
@DisplayName("Test MessageService.get()")
@Test
@Timeout(value = 60, unit …
Run Code Online (Sandbox Code Playgroud) 我有以下测试类:
@ActiveProfiles({ "DataTC", "test" })
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class, PropertyPlaceholderConfiguration.class })
public class RegularDayToTimeSlotsTest {
...
Run Code Online (Sandbox Code Playgroud)
问题似乎来自BaseTestConfiguration类:
@Configuration
@ComponentScan(basePackages = { "com.bignibou" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = RooRegexFilter.class),
@Filter(type = FilterType.ANNOTATION, value = Controller.class), @Filter(type = FilterType.ANNOTATION, value = ControllerAdvice.class) })
public class BaseTestConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
我系统地得到这个例外:
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:329)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44.CGLIB$defaultServletHandlerMapping$22(<generated>)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44$$FastClassByCGLIB$$368bb5c1.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:326)
at …
Run Code Online (Sandbox Code Playgroud) 我有以下测试课
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest {
@Autowired
MyService service;
...
}
Run Code Online (Sandbox Code Playgroud)
是否可以通过services-test-config.xml
其中一种方法以编程方式访问?喜欢:
ApplicationContext ctx = somehowGetContext();
Run Code Online (Sandbox Code Playgroud) 我有一个类似于这个的简单注释控制器:
@Controller
public class MyController {
@RequestMapping("/{id}.html")
public String doSomething(@PathVariable String id, Model model) {
// do something
return "view";
}
}
Run Code Online (Sandbox Code Playgroud)
我想用这样的单元测试来测试它:
public class MyControllerTest {
@Test
public void test() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/test.html");
new AnnotationMethodHandlerAdapter()
.handle(request, new MockHttpServletResponse(), new MyController());
// assert something
}
}
Run Code Online (Sandbox Code Playgroud)
问题是AnnotationMethodHandlerAdapter.handler()方法抛出异常:
java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:642)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:514)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:262)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:146)
Run Code Online (Sandbox Code Playgroud) spring-test ×10
spring ×9
java ×7
junit ×5
spring-mvc ×2
unit-testing ×2
hibernate ×1
junit4 ×1
junit5 ×1
maven-2 ×1
surefire ×1