我正在多线程中测试事件处理器.所以我在测试用例中使用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();
......
}
}