解决junit测试问题:initializationError+java.lang.NullPointerException+ source is null for getProperty(null,

1.确定导入依赖:

 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

2.确定导入导入了正确的依赖包:

千万注意,导入的包的是 :

org.junit.Test;

千万不要导成 org.junit.jupiter.api.Test;,否则会报:java.lang.NullPointerException和initializationError

3.测试类要写成public类型,测试方法也要要写成public 。。。并且测试方法要用@Test注解,,测试方法为public void类型的方法,否则也会报initializationError

4.确保你的spring配置文件的指定路径是对的,spring的配置文件没有问题,,所有的spring配置文件都被加载了进来,测试的接口类被加载进了spring的容器中。

下面附上我的代码以供参考(声明式和注解式的)

注解式:

service接口的测试代码:

package cn.itcast.ssm.service;

import cn.itcast.ssm.pojo.Items;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//@ContextConfiguration(locations = { "classpath*:spring/applicationContext-*.xml", "classpath*:spring/applicationContext-mvc.xml",})*/
@RunWith(SpringJUnit4ClassRunner.class)//使用junit4进行测试
@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"})
public class ItemsServiceTest {
    @Autowired
    private ItemsService itemsService;
    @Test
    public void lmj() throws Exception {
        //ItemsService itemsService=new ItemsServiceImpl();
        // ItemsService  itemsService= (ItemsService) applicationContext.getBean("itemsService");
        Items items = itemsService.lmj();
        System.out.println(items);
        System.out.println(items);
    }
}

声明式:(这是我ssm之路(9)系列博客里的spring整合mybatis的单元测试文件,这里拿来粘贴一下)

package cn.itcast.ssm.dao; 

import cn.itcast.ssm.po.OrdersCustom;
import org.junit.Test;
import org.junit.Before; 
import org.junit.After;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/** 
* OrdersMapperCustomImpl Tester. 
* 
* @author <Authors name> 
* @since <pre>ʮһ�� 8, 2018</pre> 
* @version 1.0 
*/
public class OrdersMapperCustomImplTest {
    private ApplicationContext applicationContext;
    @Before
    public void before() throws Exception {
        applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
    }

    @After
    public void after() throws Exception {
    }

    /**
     *
     * Method: findOrdersPlusUser()
     *
     */
    @Test
    public void testFindOrdersPlusUser() throws Exception {
//TODO: Test goes here...
        OrdersMapperCustom  ordersMapperCustom= (OrdersMapperCustom) applicationContext.getBean("ordersMapperCustom");
        List<OrdersCustom> orderslist= ordersMapperCustom.findOrdersPlusUser();
        System.out.println(orderslist);
        System.out.println(orderslist);
    }
}

最后再说明一个大错:

Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'items.name'. Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "name");

source is null for getProperty(null, "name")

犯这个错,完全是我的粗心,还一直再找是不是sql语法的错误:

先贴一下错误的代码:

先是itemsMapperCustom.xml:

 

然后是itemsServiceimpl:(我在形参里定义了封装的pojo类,但再写函数时却忘了将参数放进去,导致最后一值报source is null for getProperty(null, "name");不知道有人与我犯一个错误了吗)

 我的itemMapperCustom:

希望引以为戒:最后放正确的带码:

mapper接口     ItemsMapperCustom.java:

public interface ItemsMapperCustom {
    public List<Items>finditemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}

 实体类pojo:   Items.java:

public class Items {

    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

//省略getter,setter
}

基于实体类items的拓展类(即综合查询的封装pojo类)  ItemsQueryVo.java:

public class ItemsQueryVo {
    private Items items;
    public Items getItems() {
        return items;
    }
    public void setItems(Items items) {
        this.items = items;
    }
}
业务层的接口ItemsService.java:
public interface ItemsService {
    public List<Items> finditemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}

itemsService接口的实现类    ItemsServiceImpl.java:

@Service
public class ItemsServiceImpl implements ItemsService {
    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    @Override
    public List<Items> finditemsList(ItemsQueryVo itemsQueryVo) throws Exception {
        List<Items> list=itemsMapperCustom.finditemsList(itemsQueryVo);
        return list;
    }

sql语句的映射文件(mapper接口的实现类)mapper/ItemsMapperCustom.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itcast.ssm.mapper.ItemsMapperCustom">
    <sql id="query_items_where">
        <if test="items != null and items != ''">
            <if test="items.name != null and items.name != ''">
                items.name like '%${items.name}%'
            </if>
        </if>
    </sql>
    <select id="finditemsList" parameterType="cn.itcast.ssm.pojo.ItemsQueryVo" resultType="cn.itcast.ssm.pojo.Items">
    SELECT * FROM items
    <where>
        <include refid="query_items_where"></include>
    </where>
</select>
</mapper>
ItemsServiceTest测试类:
package cn.itcast.ssm.service;

import cn.itcast.ssm.pojo.Items;
import cn.itcast.ssm.pojo.ItemsQueryVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;

//@ContextConfiguration(locations = { "classpath*:spring/applicationContext-*.xml", "classpath*:spring/applicationContext-mvc.xml",})*/
@RunWith(SpringJUnit4ClassRunner.class)//使用junit4进行测试
@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"})
public class ItemsServiceTest {
    @Autowired
    private ItemsService itemsService;
    @Test
    public void finditemsList() throws Exception {
        Items items=new Items();
        items.setName("水");
        ItemsQueryVo itemsQueryVo=new ItemsQueryVo();
        itemsQueryVo.setItems(items);
        System.out.println(itemsQueryVo);
        List<Items>list=itemsService.finditemsList(itemsQueryVo);
        System.out.println(list);
        System.out.println(list);
    }
}

测试就到此完成。

猜你喜欢

转载自blog.csdn.net/qq_41063141/article/details/83931266