java junit runner_java-如何使用其他JUnit Runner运行Spring单元测...

我正在多线程中测试事件处理器.所以我在测试用例中使用vmlens的并发连接.

但是,由于我使用的是ConcurrentTestRunner而不是SpringJunit4ClassRunner,所以在自动装配Bean时出现了nullpoint异常.

这是我的宝

junit

junit

4.12

test

com.vmlens

concurrent-junit

1.0.2

test

org.assertj

assertj-core

2.6.0

test

测试用例源代码:

import com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner;

import com.anarsoft.vmlens.concurrent.junit.ThreadCount;

@RunWith(ConcurrentTestRunner.class)

@ContextConfiguration(locations = "classpath:applicationContext.xml")

public class EventListenerTest {

@Autowired

private EventStore es; //defined in applicationContext.xml

@Autowired

private EntityAppender ea; //a @Component

......

@Test

@ThreadCount(10)

public final void testDefaultListener() {

Long bef = es.countStoredEvents();// nullpoint exception

TestEntity1 t1 = ea.appWithDefaultListener();// nullpoint exception

......

}

}

显然,未正确注入豆子.

有没有什么办法解决这一问题?我应该扩展AbstractJUnit4SpringContextTests吗?

在此处附加最新代码:

EventStore是一个Jpa存储库:

public interface EventStore extends JpaRepository{};

applicationContext.xml

定义EntityAppender仅用于测试.

@Component

public class EntityAppender {

@Autowired

private TestEntity1Repository myRepository; //another Jpa repository

public EntityAppender() {

super();

}

@Transactional

public TestEntity1 appWithDefaultListener() {

TestEntity1 t1 = new TestEntity1(UUID.randomUUID().toString().replaceAll("-", ""), "aaaaaaaaaaaa", 44,

LocalDate.now());

return myRepository.save(t1);

}

...

}

测试用例:

import com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner;

import com.anarsoft.vmlens.concurrent.junit.ThreadCount;

@RunWith(ConcurrentTestRunner.class)

@ContextConfiguration(locations = "classpath:applicationContext.xml")

public class EventListenerTest {

@ClassRule

public static final SpringClassRule springClassRule = new SpringClassRule();

@Rule

public final SpringMethodRule springMethodRule = new SpringMethodRule();

@Autowired

private EventStore es;

@Autowired

private EntityAppender ea;

......

@Before

public void setUp() throws Exception {

bef = es.count(); //<====nullpoint exception due to es being null here

}

@Test

@ThreadCount(10)

public final void testDefaultListener() {

bef = es.count(); //<====== es worked well here

TestEntity1 t1 = ea.appWithDefaultListener();

......

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值