[英]How to test/Junit class using AnnotationConfigApplicationContext
我有一个使用AnnotationConfigApplicationContext
的 servlet,我想测试这个 class。 我如何模拟AnnotationConfigApplicationContext
或者有没有办法在 class 下进行测试。 由于非常具体的原因,我不想使用 spring-auto-mock 。
下面是代码
@WebServlet("/Application")
public class Application extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String birthPlace = request.getParameter("birthPlace");
PersonData person = new PersonData();
person.setBirthPlace(birthPlace);
person.setName(name);
PersonDao dao = context.getBean(PersonDao.class);
dao.insertPerson(person);
}
}
@Configuration
@ComponentScan(basePackages = { "com.test.*" })
public class JavaConfig {
@Bean
public NamedParameterJdbcOperations getLettuceConnectionFactory() {
return new NamedParameterJdbcTemplate(mysqlDataSource());
}
public DataSource mysqlDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/springjdbc");
dataSource.setUsername("guest_user");
dataSource.setPassword("guest_password");
return dataSource;
}
}
下面是我到目前为止写的代码
public class ApplicationTest {
@Mock
AnnotationConfigApplicationContext context;
@Test
public void testApplication() throws Exception{
Application app = new Application();
PowerMockito.whenNew(AnnotationConfigApplicationContext.class).withAnyArguments().thenReturn(context);
app.process();
}
}
我看到JavaConfig
中的所有 bean 都在尝试生成,因为我将无法从 Junit 访问数据库,bean 创建失败,但我不希望首先创建 bean。
我是否正确启动了AnnotationConfigApplicationContext
模拟? 是由于AnnotationConfigApplicationContext
private
static
字段引起的问题吗? 我该如何处理这种情况。
final
如果我删除static
字段,在没有电源 Mockito 的情况下是否可以对上述 class 进行测试?
您将无法使用当前实现模拟 AnnotationConfigApplicationContext。 您将需要使用依赖注入。 像这样在 Java 配置 class 中创建一个@Bean。
@Bean
public AnnotationConfigApplicationContext getAnnotationConfigApplicationContext() {
return new AnnotationConfigApplicationContext(JavaConfig.class);
}
然后在您的应用程序 class 中:
@WebServlet("/Application")
public class Application extends HttpServlet {
private static final long serialVersionUID = 1L;
**private final AnnotationConfigApplicationContext context;**
**Application(AnnotationConfigApplicationContext annotationConfigApplicationContext) {
this.annotationConfigApplicationContext = annotationConfigApplicationContext;
}**
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// code here
}
}
M. Dienum 是对的。 这不是 Spring 应该使用的方式。
正确的做法是将 PersonDao 注入应用程序 class。 然后,您将在 ApplicationUnitTest 中模拟 PersonDao class。
为此,它看起来像这样:
@WebServlet("/Application")
public class Application extends HttpServlet {
private static final long serialVersionUID = 1L;
**private final PersonDao dao;**
**Application(PersonDao dao) {
this.dao= dao;
}**
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String birthPlace = request.getParameter("birthPlace");
PersonData person = new PersonData();
person.setBirthPlace(birthPlace);
person.setName(name);
dao.insertPerson(person);
}
}
你的测试看起来像这样:
public class ApplicationTest {
PersonDao personDao = mock(PersonDao.class);
Application app = new Application(personDao);
@Test
public void testApplication() throws Exception{
doNothing().when(personDao).insert(any());
app.doPost(request, response);
verify(personDao).insert(person);
// fill in the details
// not sure what app.process is supposed to be
}
由于这样的原因,Spring 框架具有 MockMvc。 它使测试中与端点的交互更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.