我在依赖注入(Spring autowiring)和maven-surefire方面遇到了一些问题.使用TestNG在eclipse中运行时,以下测试没有问题:注入service-object,然后@BeforeClass
调用-method.
@TransactionConfiguration(defaultRollback=false)
@ContextConfiguration(locations={"/testContext.xml"})
public class MyServiceTest extends AbstractTransactionalTestNGSpringContextTests {
@Autowired
private MyService service;
@BeforeTest
public void setup() {
System.out.println("*********************"+service);
Assert.assertNotNull(service);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用maven-surefire运行相同的测试用例时,首先调用setup(),这会导致测试失败:
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ myserver ---
[INFO] Surefire report directory: D:\...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
**************************null
2011-03-04 11:08:57,462 DEBUG ionTestExecutionListener.prepareTestInstance - Performing dependency injection for test context [[TestContext@1fd6bea...
2011-03-04 11:08:57,462 DEBUG ractGenericContextLoader.loadContext - Loading ApplicationContext for locations [classpath:/testContext.xml].
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?如果我更换@BeforeClass
与@Test
它在行家的作品作为TestNG的Eclipse插件.
Maven的万无一失,插件:2.7.2
Eclipse:Helios Service Release 1
jdk1.6.0_14 …
使用junit4测试spring服务层的下一个问题是:如何在所有@Test方法之前调用仅填充一次数据库的脚本:我想在所有@Tests之前执行一次:
JdbcTestUtils.executeSqlScript(jdbcTemplate(), new FileSystemResource(
"src/main/resources/sql/mysql/javahelp-insert.sql"), false);
Run Code Online (Sandbox Code Playgroud)
我试图在我的GenericServiceTest类上使用@PostConstruct(由测试类扩展).事实证明,每次@Test方法之前都会调用@PostConstruct.有趣的是,甚至在每个@Test方法之前调用注释了@Autowired of GenericServiceTest的方法.
我不希望在每个测试类之前填充数据库,但只在spring-test启动时填充一次.
如何在使用spring测试框架和junit4的所有@Test方法之前只执行一次上面的方法?
谢谢!
我正在使用@RunWith(MockitoJUnitRunner.class)
mockito进行junit测试.但现在我正在使用spring boot app并尝试使用@RunWith(SpringRunner.class)
.使用是否比使用@RunWith(SpringRunner.class)
有任何优势@RunWith(MockitoJUnitRunner.class)
?我仍然可以使用功能一样@Injectmock
,@Mock
,@Spy
与@RunWith(SpringRunner.class)
在我们的Spring Web应用程序中,我们使用Spring bean配置文件来区分三种场景:开发,集成和生产.我们使用它们连接到不同的数据库或设置其他常量.
使用Spring bean配置文件非常适合更改Web应用程序环境.
我们遇到的问题是我们的集成测试代码需要改变环境.在这些情况下,集成测试会加载Web应用程序的应用程序上下文.这样我们就不必重新定义数据库连接,常量等(应用DRY原则).
我们设置了如下的集成测试.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = ["classpath:applicationContext.xml"])
public class MyTestIT
{
@Autowired
@Qualifier("myRemoteURL") // a value from the web-app's applicationContext.xml
private String remoteURL;
...
}
Run Code Online (Sandbox Code Playgroud)
我可以使用它在本地运行@ActiveProfiles
,但这是硬编码的,导致我们的测试在构建服务器上失败.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = ["classpath:applicationContext.xml"])
@ActiveProfiles("development")
public class MyTestIT
{ ... }
Run Code Online (Sandbox Code Playgroud)
我也试过使用@WebAppConfiguration
希望它可能以某种方式spring.profiles.active
从Maven 导入属性,但这不起作用.
另外需要注意的是,我们还需要配置代码,以便开发人员可以运行Web应用程序,然后使用IntelliJ的测试运行器(或其他IDE)运行测试.这对于调试集成测试来说要容易得多.
我正在尝试结合以下注释:
org.springframework.test.context.jdbc.Sql 和 org.junit.Before
像下面的代码:
@Test
@Sql(scripts = "dml-parametro.sql")
public void testData(){
Iterable<Parametro> parametros = parametroService.findAll();
List<Parametro> parametrosList = Lists.newArrayList(parametros);
Assert.assertThat(parametrosList.size(), Is.is(1));
}
@Before
public void beforeMethod() {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "PARAMETRO");
}
Run Code Online (Sandbox Code Playgroud)
方法@Before中的代码在@Sql批注中的脚本"dml-parametro.sql"之后运行.
这样做是对的吗?
为了解决这个问题,我使用@After而不是@Before,但是我想在测试执行之前而不是之后删除表.
我不想使用@SqlConfig.我没有在测试级别上使用transacional scope,所以我需要在每个测试方法中清理我的表.如果每个测试方法都需要清理表,我想在@Before方法中执行此操作.我不想在每个使用@SqlConfig的测试方法中都这样做.我认为@Sql在@Before之前执行的行为是错误的.
自 Spring boot 2.7.1 起,@LocalServerPort
(在包中org.springframework.boot.web.server.LocalServerPort
)已被弃用。
我可以用什么来代替这个注释?
我有几个junit测试,
@ContextConfiguration(locations = { "file:../business/src/test/resources/application-context-test.xml",
"file:src/main/webapp/WEB-INF/confA.xml", "classpath:/mvc-dispatcher-servlet-test.xml"})
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ProductContentControllerTest {
...
}
Run Code Online (Sandbox Code Playgroud)
在类中,所有测试都必须在相同的上下文中运行(在这种情况下).
但我希望我所有的测试类都是独立的.我假设它是默认行为,但是当我一起运行所有测试时,它似乎运行得太快了.
它是如何工作的?应用程序上下文是否仅针对每个测试类启动一次?
我应该添加:@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
在每个测试类?
谢谢
我试图在使用@Primary在测试配置中声明的测试期间覆盖Spring bean.一个声明位于src/main/java路径中,另一个声明位于src/test/java路径中.
但是,Spring故意用非主bean替换主bean,这是我不想用于测试的bean.如果我只是注释掉生产(src/main/java)配置bean,它会根据需要在测试配置中使用主测试(src/main/test)bean.(显然,每次我想运行测试时都无法注释掉代码.)
从日志:
osbfsDefaultListableBeanFactory - 使用不同的定义覆盖bean'sqsConnectionFactory'的bean定义:replace [Root bean:class [null]; 范围=; 抽象= FALSE; lazyInit = FALSE; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE; 初级= TRUE; factoryBeanName = testJmsConfiguration; factoryMethodName = sqsConnectionFactory; initMethodName = NULL; destroyMethodName =(推断); 在类路径资源[com/foo/configuration/TestJmsConfiguration.class]中定义]
同
[root bean:class [null]; 范围=; 抽象= FALSE; lazyInit = FALSE; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE; primary = false ; factoryBeanName = jmsConfiguration; factoryMethodName = sqsConnectionFactory; initMethodName = NULL; destroyMethodName =(推断); 在类路径资源[com/foo/configuration/JmsConfiguration.class]中定义]
为什么spring用非主bean替换主bean?如何让Spring使用专门标记为主bean的bean?
编辑:src/main/java配置:
@Configuration
public class JmsConfiguration …
Run Code Online (Sandbox Code Playgroud) 我在尝试测试一个接收an UserDetails
作为参数注释的休息端点时遇到了麻烦@AuthenticationPrincipal
.
似乎没有使用在测试场景中创建的用户实例,但是尝试使用默认构造函数进行实例化: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.andrucz.app.AppUserDetails]: No default constructor found;
REST端点:
@RestController
@RequestMapping("/api/items")
class ItemEndpoint {
@Autowired
private ItemService itemService;
@RequestMapping(path = "/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Callable<ItemDto> getItemById(@PathVariable("id") String id, @AuthenticationPrincipal AppUserDetails userDetails) {
return () -> {
Item item = itemService.getItemById(id).orElseThrow(() -> new ResourceNotFoundException(id));
...
};
}
}
Run Code Online (Sandbox Code Playgroud)
测试类:
public class ItemEndpointTests {
@InjectMocks
private ItemEndpoint itemEndpoint;
@Mock
private ItemService itemService;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this); …
Run Code Online (Sandbox Code Playgroud) 我已经使用现有工作区迁移到Eclipse Photon.我在这个工作区中有一些Maven项目.所有项目在Eclipse Oxygen中都没有任何错误.打开我的工作区在Eclipse光子所有测试类,其进口之后org.mockito.Mockito
,org.springframework.mock
并org.springframework.test
有错误.虽然Eclipse知道它们,但无法解析这些导入,因为我可以跳转到类中.
为什么Eclipse Photon无法解析这些导入?我该如何解决这个问题?
spring-test ×10
spring ×7
java ×6
junit ×2
mockito ×2
spring-boot ×2
spring-mvc ×2
maven ×1
mysql ×1
spring-junit ×1
testing ×1
testng ×1