标签: spring-test

JUnit测试在Eclipse中传递但在Maven Surefire中失败

我已经使用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)

java spring maven-2 spring-test surefire

86
推荐指数
7
解决办法
9万
查看次数

如何在spring测试中设置环境变量或系统属性?

我想编写一些检查已部署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上下文之前执行某些操作.

java spring environment-variables spring-test

84
推荐指数
6
解决办法
12万
查看次数

SpringJUnit4ClassRunner和SpringRunner有什么区别

每当我看到一篇与Spring测试相关的博客文章时,我都会看到这些类中的任何一个,但却不了解真正的区别:

@RunWith(SpringRunner.class)
@RunWith(SpringJUnit4ClassRunner.class)
Run Code Online (Sandbox Code Playgroud)

java spring unit-testing spring-test

74
推荐指数
2
解决办法
2万
查看次数

在junit测试类中重用spring应用程序上下文

我们有一堆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来构建项目.

junit spring spring-test junit4

73
推荐指数
4
解决办法
6万
查看次数

@Test之后的回滚事务

首先,我在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

java junit spring hibernate spring-test

69
推荐指数
5
解决办法
9万
查看次数

如何使用Parametrized运行JUnit SpringJUnit4ClassRunner?

由于重复@RunWith注释,以下代码无效:

@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(Parameterized.class)
@SpringApplicationConfiguration(classes = {ApplicationConfigTest.class})
public class ServiceTest {
}
Run Code Online (Sandbox Code Playgroud)

但是我如何结合使用这两个注释呢?

java junit spring spring-test

64
推荐指数
2
解决办法
3万
查看次数

org.junit.platform.commons.JUnitException:ID为“junit-jupiter”的TestEngine未能发现测试

我想在 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)

java junit spring-test junit5 junit-jupiter

57
推荐指数
4
解决办法
13万
查看次数

java.lang.IllegalArgumentException:需要ServletContext来配置默认的servlet处理

我有以下测试类:

@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)

spring spring-mvc spring-test

55
推荐指数
3
解决办法
5万
查看次数

如何在使用@RunWith和@ContextConfiguration注释的jUnit测试中访问Spring上下文?

我有以下测试课

@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)

junit spring spring-test applicationcontext

54
推荐指数
3
解决办法
10万
查看次数

如何使用@PathVariable对Spring MVC控制器进行单元测试?

我有一个类似于这个的简单注释控制器:

@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)

java spring unit-testing spring-mvc spring-test

52
推荐指数
3
解决办法
8万
查看次数