Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring单元测试-加载ApplicationContext失败_Java_Spring_Junit - Fatal编程技术网

Java Spring单元测试-加载ApplicationContext失败

Java Spring单元测试-加载ApplicationContext失败,java,spring,junit,Java,Spring,Junit,您好,我是单元测试新手,当您尝试测试我的spring应用程序时,我无法解决这个问题,无法加载应用程序上下文。找不到路径[我在@ContextConfiguration中指定的] 这是我的代码: -应用程序上下文: @Configuration @EnableWebMvc @ComponentScan("com.RealEstate") @Import(ApplicationContext.class) public class WebConfig extends WebMvcConfigurer

您好,我是单元测试新手,当您尝试测试我的spring应用程序时,我无法解决这个问题,无法加载应用程序上下文。找不到路径[我在@ContextConfiguration中指定的] 这是我的代码: -应用程序上下文:

@Configuration
@EnableWebMvc
@ComponentScan("com.RealEstate")
@Import(ApplicationContext.class)
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations("classpath:/images/").setCachePeriod(31556926);
        registry.addResourceHandler("/Resources/**")
                .addResourceLocations("/Resources/").setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry){
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setViewClass(JstlView.class);
        internalResourceViewResolver.setPrefix("/WEB-INF/views/");
        internalResourceViewResolver.setSuffix(".jsp");
        registry.viewResolver(internalResourceViewResolver);
    }

}
这是我的测试课:

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(classes = { WebConfig.class})
public class CustomerDaoImplTest {

    @Autowired
    CustomerDao customerDao;

    @BeforeClass
    public static void setUp(){
        // MockitoAnnotations.initMocks(customerDao);
    }

    @Test
    public void findById(){
        //Assert.assertNotNull(adminDao.readAdminById(1));
        //Assert.assertNull(adminDao.readAdminById(3));
        Assert.assertNull(null);
    }

    @Test
    public void saveCustomer(){
        //customerDao.storeCustomer(getSampleCustomer());
        //Assert.assertEquals(customerDao.getCustomersList().size(), 7);
        Assert.assertEquals(5+2, 7);
    }

    @Test
    public void deleteCustomer(){
        customerDao.deleteCustomer("customer1");
        Assert.assertEquals(customerDao.getCustomersList().size(), 5);
    }

    @Test
    public void deleteEmployeeByInvalidSsn(){
        customerDao.deleteCustomer("23423");
        Assert.assertEquals(customerDao.getCustomersList().size(), 6);
    }

    @Test
    public void findAllEmployees(){
        Assert.assertEquals(customerDao.getCustomersList().size(), 6);
    }

    @Test
    public void findEmployeeByUsername(){
        Assert.assertNotNull(customerDao.readCustomerByUsername("overflow"));
        Assert.assertNull(customerDao.readCustomerByUsername("paul"));
    }

    public Customer getSampleCustomer(){
        Customer customer = new Customer();
        customer.setId(7);
        customer.setName("ihab");
        customer.setLastName("benamer");
        customer.setEmail("benamer@gmail.com");
        customer.setUsername("ihab");
        try{
            customer.setBirthday(new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse("1996-11-10").getTime()));
        } catch (java.text.ParseException e){
            ;
        }
        customer.setIdNumber("987521");
        customer.setAddress("consantine, algérie");
        customer.setPassword("123");
        customer.setPhone("+213557665392");
        customer.setSex("male");
        return customer;
    }

}
我有一个这样的结构: JAVA |com.RealEstate |配置 |刀 |模型 |服务 |控制器 测试 |com.RealEstate |配置 |刀

here's the error slack 
    saveAdmin(com.RealEstate.dao.AdminDaoImplTest)  Time elapsed: 0 sec  <<< ERROR!
java.lang.NullPointerException
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:227)
    at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping.registerHandlers(StandaloneMockMvcBuilder.java:480)
    at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.registerMvcSingletons(StandaloneMockMvcBuilder.java:346)
    at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.initWebAppContext(StandaloneMockMvcBuilder.java:331)
    at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:127)
    at com.RealEstate.dao.AdminDaoImplTest.setUp(AdminDaoImplTest.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
下面是错误

saveAdmin(com.RealEstate.dao.AdminDaoImplTest)运行时间:0秒更新:我尝试使用xml上下文文件而不是config类,但在异常后仍然收到相同的错误stack@Dovmo我更新了帖子,它将导出
@Import(ApplicationContext.class)
。这是不必要的,可能会把你的测试搞砸;不确定这是否是根本原因,但我已经尝试在WebContext类中创建所有bean,而不是2类(+ApplicationContext类),但仍然有相同的错误