spring 整合 JUnit(spring 内单元测试类调用带注入的报空指针异常NullPointException)

效果图:spring 整合 JUnit(spring 内单元测试类调用带注入的报空指针异常NullPointException)_第1张图片

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@ContextConfiguration(locations = { "/applicationContext-test.xml" })
public class BaseSpringTestCase {
		
}
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.ekingwin.bas.rest.service.WritebackUtilsService;

public class MyTestCase extends BaseSpringTestCase {
	@Autowired
	private WritebackUtilsService wb;
	
	@Test
	public void testWb(){
		boolean isWriteBacked1 = wb.isWriteBacked("440318");
		System.out.println(isWriteBacked1);
		boolean isWriteBacked2 = wb.isWriteBacked("124567");
		System.out.println(isWriteBacked2);
		
		wb.updateWriteBackFlag("440319");
		wb.updateWriteBackFlag("440301");
		
		
	}

}

在这里插入图片描述

参考:
JUnit与Spring的整合——JUnit的TestCase如何自动注入Spring容器托管的对象

spring实现的大型项目中,如何实现整合多个配置文件?

你可能感兴趣的:(Spring)