如何在Spring MVC中配置测试

时间:2018-10-21 20:03:58

标签: hibernate spring-mvc integration-testing junit4

如何在Spring MVC中严格配置测试? 基于Java的配置。

  

这是我的UserTest类

_value

我的应用程序具有三个配置文件。

  

HibernateUtils.java

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {
        HibernateUtils.class }, loader = AnnotationConfigContextLoader.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
        DbUnitTestExecutionListener.class })

public class UserDaoTest {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
  

WebConfig.java

    @Configuration
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.example.packages") })
public class HibernateUtils {

    @Autowired
    private ApplicationContext context;

    @Bean
    public LocalSessionFactoryBean getSessionFactory() {
        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        factoryBean.setConfigLocation(
                context.getResource("classpath:hibernate.cfg.xml"));
        factoryBean.setAnnotatedClasses(User.class);
        factoryBean.setAnnotatedClasses(Role.class);
        return factoryBean;
    }


    @Bean
    public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
    }



}
  

WebInit

  @Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.example.package" })
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp().prefix("/WEB-INF/pages/").suffix(".jsp");
    }



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HomePageFilter()).addPathPatterns("/");
        registry.addInterceptor(new EncodingFilter()).addPathPatterns("/*");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/create").setViewName("createUser");
        registry.addViewController("/user").setViewName("user");
        registry.addViewController("/admin").setViewName("admin");
        registry.addViewController("/edit").setViewName("editUser");
        registry.addViewController("/remove").setViewName("remove");
        registry.addViewController("/logout").setViewName("logout");
        registry.addViewController("/error").setViewName(" unauthorized");
    }



    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasename("messages");
        return source;
    }
}

毕竟。 我的测试抛出异常

  

public class WebMvcApplicationInit
        extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { WebSecurityConfig.class,
                HibernateUtils.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

如何设置ServletContext? 我在 UserTest类中设置了什么? 休眠状态完美。

1 个答案:

答案 0 :(得分:0)

您应该更改

@ContextConfiguration(classes = {
    HibernateUtils.class }, loader = AnnotationConfigContextLoader.class)

@ContextConfiguration(classes = {
    HibernateUtils.class }, loader = AnnotationConfigWebContextLoader.class)