我有一个服务类MyService,它在控制器中定义和使用,如下所示:
public interface MyService {
public String someMethod()
}
@Service("myService")
public class MyServiceImpl implements MyService {
public String someMethod() {
return "something";
}
}
@Controller
public class MyController {
@Autowired
public MyService myService;
@RequestMapping(value="/someurl", method=RequestMethod.GET)
public String blah () {
return myService.getsomeMethod();
}
}我想为someMethod方法编写一个测试用例,但是,下面的方法不起作用。我如何连接实现类?
public class MyServiceImplTest {
@Autowired
private MyService myService;
@Test
public void testSomeMethod() {
assertEquals("something", myService.someMethod());
}}
发布于 2012-11-19 23:44:42
public class MyServiceImplTest {
private MyService myService = new MyServiceImpl();
@Test
public void testSomeMethod() {
assertEquals("something", myService.someMethod());
}
}为什么要在测试中注入bean,而不是自己创建实例呢?
发布于 2012-11-20 13:55:47
试试这个:
@RunWith(SpringJUnit4ClassRunner.class)
// specifies the Spring configuration to load for this test fixture
@ContextConfiguration("yourapplication-config.xml")另请参阅the Spring.IO docs了解更多详细信息。
https://stackoverflow.com/questions/13457128
复制相似问题