本文介绍了Spring服务层的JUnit测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图配置JUnit并为Spring 3.2 MVC应用程序的服务层编写测试用例。我无法找到关于如何从头开始配置JUnit的更多信息,并使其适用于Spring服务层。这里是我的问题 我真的不知道要使用何种版本的junit,所以我只是抓住最新版本 < dependency> < groupId> junit< / groupId> < artifactId> junit< / artifactId> < version> 4.11< / version> < scope> test< / scope> < /依赖关系> 我的服务类 @Service public class AuthService实现IAuthService,ApplicationContextAware, org.springframework.context.ApplicationListener< org.springframework.context.event.ContextRefreshedEvent> { 公共收藏 Set< String> result = new HashSet< String>(); String commandName = command.getBeanName(); String defaultAdministerPermission = command.getAdministerPermission() String defaultExecutePermission = command.getExecutePermission()字符串overrideAdminPermission = null; 字符串overrideExecPermission = null; String finalAdministerPermission = overrideAdminPermission == null? defaultAdministerPermission :overrideAdminPermission; command.setAdministerPermission(finalAdministerPermission); result.add(finalAdministerPermission); String finalFxecutePermission = overrideExecPermission == null? defaultExecutePermission :overrideExecPermission; command.setExecutePermission(finalFxecutePermission); result.add(finalFxecutePermission); 尝试{ session.saveOrUpdate(command); session.flush(); }最后{ // TODO - 吞下更多的异常。 } 返回结果; } //一些其他方法} My Test Class(部分使用groovy) import junit.framework.TestCase; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpSession; import org.springframework.test.context.ContextConfiguration; import com.dc.core.security.service.impl.AuthService import com.dc.core.behavior.command.model.impl.CommandMetadata; import org.hibernate.SessionFactory import org.hibernate.classic.Session $ b @ContextConfiguration(locations =file:WebContent / WEB-INF / applicationContext.xml) public class AuthServiceTest extends TestCase { @Autowired private AuthService authService; @Autowired private MockHttpSession mockHttpSession; @Autowired ApplicationContext appContext $ b @Autowired SessionFactory sessionFactory private Session mockHibernateSession = Mockito.mock(org。 hibernate.classic.Session.class); private CommandMetadata commandMetadata = new CommandMetadata(); $ b $ public void setUp(){ appContext.getBeanFactory()。registerScope(request,new RequestScope()) MockHttpServletRequest request = new MockHttpServletRequest() ServletRequestAttributes attributes = new ServletRequestAttributes(request) RequestContextHolder.setRequestAttributes(attributes) CurrentRequestProperties currentRequestProperties = appContext.getBean(currentRequestProperties) session = sessionHandler.initiateSession(sessionFactory,currentRequestProperties ) public void testAddCommandPermissions(){ commandMetadata.beanName =TestBean commandMetadata.administerPermission =TestBean。管理 commandMetadata.executePermission =TestBean.execute集合< String> results = authorizationService.addCommandPermissions(session,commandMetadata); assertTrue(results.contains(TestBean.administer))} public void testCanary(){ assertTrue(true); $ / code> 当我运行我的测试用例时,出现以下错误 java.lang.NullPointerException:无法在null对象上调用方法getBeanFactory() 我认为问题的原因是appContext没有正确注入,因此我们得到了NPE 。但我无法解决这个问题。我真的很感谢某人在这方面的帮助。解决方案添加Spring JUnit类跑步者。您还应该使用@Test注释来代替扩展TestCase。 eg I am trying to configure JUnit and write test cases for Service layer of Spring 3.2 MVC application. I couldnt find much information on how to configure the JUnit from scratch and make it work for Spring service layer. here is my problemI dont really know what version of junit to be used so i just grabbed the latest versionMaven junit dependancy <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>My service Class@Servicepublic class AuthService implements IAuthService, ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent> {public Collection<? extends String> addCommandPermissions(Session session, CommandMetadata command) { Set<String> result = new HashSet<String>(); String commandName = command.getBeanName(); String defaultAdministerPermission = command.getAdministerPermission() String defaultExecutePermission = command.getExecutePermission() String overrideAdminPermission = null; String overrideExecPermission = null; String finalAdministerPermission = overrideAdminPermission == null ? defaultAdministerPermission : overrideAdminPermission; command.setAdministerPermission(finalAdministerPermission); result.add(finalAdministerPermission); String finalFxecutePermission = overrideExecPermission == null ? defaultExecutePermission : overrideExecPermission; command.setExecutePermission(finalFxecutePermission); result.add(finalFxecutePermission); try { session.saveOrUpdate(command); session.flush(); } finally { // TODO - more swallowed exceptions. } return result;}// some other methods}My Test Class(used groovy partially)import junit.framework.TestCase;import org.mockito.Mockito;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mock.web.MockHttpSession;import org.springframework.test.context.ContextConfiguration;import com.dc.core.security.service.impl.AuthServiceimport com.dc.core.behavior.command.model.impl.CommandMetadata;import org.hibernate.SessionFactoryimport org.hibernate.classic.Session@ContextConfiguration(locations = "file:WebContent/WEB-INF/applicationContext.xml")public class AuthServiceTest extends TestCase { @Autowired private AuthService authService; @Autowired private MockHttpSession mockHttpSession; @Autowired ApplicationContext appContext @Autowired SessionFactory sessionFactory private Session mockHibernateSession = Mockito.mock(org.hibernate.classic.Session.class); private CommandMetadata commandMetadata = new CommandMetadata(); public void setUp() { appContext.getBeanFactory().registerScope("request", new RequestScope()) MockHttpServletRequest request = new MockHttpServletRequest() ServletRequestAttributes attributes = new ServletRequestAttributes(request) RequestContextHolder.setRequestAttributes(attributes) CurrentRequestProperties currentRequestProperties = appContext.getBean("currentRequestProperties") session = sessionHandler.initiateSession(sessionFactory, currentRequestProperties) } public void testAddCommandPermissions() { commandMetadata.beanName = "TestBean" commandMetadata.administerPermission = "TestBean.administer" commandMetadata.executePermission = "TestBean.execute" Collection<String> results = authorizationService.addCommandPermissions(session, commandMetadata); assertTrue(results.contains("TestBean.administer")) } public void testCanary() { assertTrue(true); }}When I run my test case am getting the below errorjava.lang.NullPointerException: Cannot invoke method getBeanFactory() on null objectI think cause of the issue is appContext is not injected properly hence am getting NPE. But I couldnt solve this issue. I really appreciate someone's help on this 解决方案 Add the Spring JUnit class runner. You should also use the @Test annotations instead of extending TestCase.e.g.@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) 这篇关于Spring服务层的JUnit测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-30 19:15