spring boot 单元测试@Autowired无法注入问题汇总

单元测试详细步骤:

https://blog.csdn.net/SELECT_BIN/article/details/82880847

遇到无法注入情况,排查:

1.调用Service不能用new实例化:

原因:@autowire跟XML方式注入类似,是Ioc容器负责创建新的实例,实例里可以有其他的依赖关系并也由springIoc容器负责注入,如果只是简单的new一个对象的话,新对象里面的所有变量依赖都是没有注入的,这样就失去了Ioc的意义。而且new的对象不能调用注入的其他类!!!

反例:

@Test
	public void testAddWorkPoolListByTask_1()
		throws Exception {
		TKmPendingTskController fixture = new TKmPendingTskController();
		TKmPendingTsk tKmPendingTsk = new TKmPendingTsk();
		TKmPendingTskServiceResponse result = fixture.addWorkPoolListByTask(tKmPendingTsk);
		assertNotNull(result);
		assertEquals("TKmPendingTskServiceResponse(rsp=null)", result.toString());
		assertEquals(null, result.getRsp());
		assertEquals(null, result.getStatus());
		assertEquals("新增失败!", result.getMsg());
		assertEquals("", result.getTxid());
	}

修正:

@Autowired
    TKmPendingTskController fixture;
    @Test
	public void testAddWorkPoolListByTask_1()
		throws Exception {
		TKmPendingTskController fixture = new TKmPendingTskController();
		TKmPendingTsk tKmPendingTsk = new TKmPendingTsk();
		TKmPendingTskServiceResponse result = fixture.addWorkPoolListByTask(tKmPendingTsk);
		assertNotNull(result);
		assertEquals("TKmPendingTskServiceResponse(rsp=null)", result.toString());
		assertEquals(null, result.getRsp());
		assertEquals(null, result.getStatus());
		assertEquals("新增失败!", result.getMsg());
		assertEquals("", result.getTxid());
	}

2.没有加载测试基类:

反例:

public class TKmPendingTskControllerGTest {
    @Autowired
    TKmPendingTskController fixture;

    @Test
    public void testTKmPendingTskController_1()
            throws Exception {
        TKmPendingTskController result = new TKmPendingTskController();
        assertNotNull(result);
    }

}

解决:

public class TKmPendingTskControllerGTest extends ApplicationGTest {
    @Autowired
    TKmPendingTskController fixture;

    @Test
    public void testTKmPendingTskController_1()
            throws Exception {
        TKmPendingTskController result = new TKmPendingTskController();
        assertNotNull(result);
    }

}

测试基类名称不是确定的,视情况而定;

3.测试基类中未加载启动项:

反例:

package com.ai.rai.group.workflow.application;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
 
/**
 * Project:html_template
 * Author:hangke
 * Date:2017/3/22
 * Description://TODO add description here
 */
public class TestApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(TestApplication.class,args);
    }
}

解决:

加入启动项:

@SpringBootApplication
@ComponentScan(basePackages={"com.ai", "com.asiainfo"})

 即:

package com.ai.rai.group.workflow.application;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
 
/**
 * Project:html_template
 * Author:hangke
 * Date:2017/3/22
 * Description://TODO add description here
 */
@SpringBootApplication
@ComponentScan(basePackages={"com.ai", "com.asiainfo"})
public class TestApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(TestApplication.class,args);
    }
}

 

你可能感兴趣的:(Spring)