我有一个集成测试类,注释如下
@WebAppConfiguration
@ContextConfiguration(classes = {AppConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class CacheConsumerTest {
}
Run Code Online (Sandbox Code Playgroud)
这是我的AppConfiguration
@Configuration
@ComponentScan(basePackages = {"com.etc.etc.etc."})
@EnableWebMvc
public class AppConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我的@Component豆子都没有@PreDestroy在所有测试结束时被调用CacheConsumerTest.@PostConstruct然而,在任何测试运行之前,都会在开始时调用它.
谁知道问题可能是什么?我的@Component一些后台线程我想关闭(通过调用@Predestroy),否则他们在后台执行的工作将导致其他测试类中的后续测试失败.
我试过添加,@DirtiesContext(classMode=ClassMode.AFTER_CLASS) 但没有帮助.
编辑:找出问题,我必须做一个额外的步骤,使DirtiesContext工作: Spring @DirtiesContext重新加载Spring上下文?
我想对下面的简单 jms 监听器代码进行单元测试
@Component
public class NotificationReader {
@JmsListener(destination = "myAppQ")
public void receiveMessage(NotificationMessage notificationMessage) {
System.out.println("Received <" + notificationMessage.getStaffNumber() + ">");
}
}
Run Code Online (Sandbox Code Playgroud)
在 junit 中,我使用 jmsTemplate 将消息注入 Active MQ。
我想测试 jms 监听器是否被调用。
我看到了一些解决方案(使用计数器),例如How to wait for @JMSListener annotated method to finish in JUnit,这实际上只是为了测试目的而更改侦听器代码,而我不想这样做。
还有其他选择吗?
尝试按照答案中的建议进行配置。
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestNotificationReader {
@Autowired
JmsTemplate jmsTemplate;
@Value("${outbound.endpoint}")
private String destination;
@Test
public void contextLoads() {
}
@Test
public void testPutToQ() {
NotificationMessage notificationMessage = new NotificationMessage();
notificationMessage.setStaffNumber("100");
notificationMessage.setWorkflowType("TYPE"); …Run Code Online (Sandbox Code Playgroud) 我必须使用JUnit测试我的路由构建器,我得到java.lang.NoSuchMethodError,我错过了任何配置或我访问路由构建器的方式应该更改.请在此处找到RouteBuilder,context和Junit类
@Component
public class MyRouteBuilder extends SpringRouteBuilder {
@Autowired
private SourcePoint sourcePoint;
@Autowired
private DestinationPoint destinationPoint;
@Override
public void configure() throws Exception {
from(sourcePoint.getString()).to(destinationPoint.getString());
}
Run Code Online (Sandbox Code Playgroud)
我创建了JUnit测试用例,并在正确的maven位置创建了上下文,如下所示
src/test/java/org/apache/camel/spring/patterns/MyRouteBuilderTest.java
src/test/resources/org/apache/camel/spring/patterns/MyRouteBuilderTest-context.xml
Run Code Online (Sandbox Code Playgroud)
这里我附上了这两个文件
***** MyRouteBuilderTest-context.xml ***
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="MyRouteBuilder" />
</camelContext>
<context:component-scan base-package="com.myroute" />
Run Code Online (Sandbox Code Playgroud)
***** MyRouteBuilderTest.class ***
@RunWith(CamelSpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTest.class)
@ContextConfiguration
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
public class EmypaRouteBuilderTest extends CamelSpringTestSupport {
private static final Logger logger = LoggerFactory.getLogger(MyRouteBuilderTest.class);
@Autowired …Run Code Online (Sandbox Code Playgroud) 我想在测试失败时收到通知.理想情况下,我想知道我的@After注释方法是否通过了测试.我知道它们是一个可以用于此目的的RunListener,但它只有在我们使用JunitCore运行测试时才有效.如果测试用例失败或类似于RunListener可以与SpringJUnit4ClassRunner一起使用,有没有办法得到通知?
我在Spring应用程序中遇到了奇怪的行为.
我正在尝试创建一个这样的JUnit测试类:
public class AppTest {
@Before
public void setUp() {
// Create the application from the configuration
ApplicationContext context = new AnnotationConfigApplicationContext( ApplicationConfig.class )
// Look up the application service interface
service = (TransferService) context.getBean(TransferService.class);
}
}
Run Code Online (Sandbox Code Playgroud)
主要问题是我在setUp()方法的@Before注释上获得了一个错误.它对我说:以前无法解决一个类型
似乎无法找到@Before注释的依赖项.
这是我在pom.xml文件中的依赖项:
<properties>
<org.springframework.version>3.2.16.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define
this if you use Spring Bean APIs (org.springframework.beans.*) --> …Run Code Online (Sandbox Code Playgroud) spring-junit ×5
java ×3
spring ×3
junit ×2
apache-camel ×1
java-ee ×1
spring-jms ×1
spring-mvc ×1
spring-test ×1