0
我是junit和TDD的新手。junit with spring and mybatis
我剛做了第一個測試用例。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-beans/spring-cms-root.xml"})
public class MemberServiceImplTest {
@Autowired
MemberService memberService;
@Test
public void testGetMemberById() {
Member member = memberService.getMemberById("testid");
assertTrue(member != null);
}
@Test
public void testGetMemberBySeq() {
Member member = memberService.getMemberBySeq(1);
assertTrue(member != null);
}
}
但是這個Test Class在運行junit測試時給了我這個錯誤。
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.tource.cms.member.MemberService.getMemberById
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:507)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500)
at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:240)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:71)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:39)
at $Proxy18.getMemberById(Unknown Source)
at com.tource.cms.member.MemberServiceImplTest.testGetMemberById(MemberServiceImplTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
所以我改變了我的測試類如下所示。
public class MemberServiceImplTest {
MemberService memberService;
public MemberServiceImplTest() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans/spring-cms-root.xml");
memberService = ctx.getBean("memberServiceImpl", MemberServiceImpl.class);
}
@Test
public void testGetMemberById() {
Member member = memberService.getMemberById("testid");
assertTrue(member != null);
}
@Test
public void testGetMemberBySeq() {
Member member = memberService.getMemberBySeq(1);
assertTrue(member != null);
}
}
而且這個效果很好。任何人都知道它爲什麼有效或沒有?
我對兩個類別之間的差異沒有任何意見。
聽起來像你的mybatis映射/配置文件沒有被加載,雖然你沒有提供很多細節。 (1)你的項目目錄結構中的配置在哪裏(例如'src/main/resources')? (2)您如何構建項目(例如Maven?)? (3)由於您刪除了'@ RunWith'註釋,因此工作示例是如何啓動的。 (4)在破碎的例子中,如果你在'@ Autowired'正下方的'memberService'聲明中添加'@Qualifier(「memberServiceImpl」)'會發生什麼?僅供參考而不是'assertTrue(member!= null)',你應該'assertNotNull(member)'。 – jtoberon
@jtoberon哇,你解決了它。按照您的評論配置\ @Qualifier後,一切正常。謝謝 –
很酷。那是在黑暗中拍攝的。我將這個建議轉到了答案上,以便你可以接受它作爲後代。 – jtoberon