gpt4 book ai didi

java - Spring 服务层的 JUnit 测试用例

转载 作者:行者123 更新时间:2023-11-30 07:08:19 25 4
gpt4 key购买 nike

我正在尝试配置 JUnit 并为 Spring 3.2 MVC 应用程序的服务层编写测试用例。我找不到很多关于如何从头开始配置 JUnit 并使其适用于 Spring 服务层的信息。这是我的问题

我真的不知道要使用什么版本的 junit 所以我只是捕获了最新版本

Maven junit 依赖

 <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

我的服务等级

@Service
public 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
}

我的测试类(部分使用了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

@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);
}
}

当我运行我的测试用例时出现以下错误java.lang.NullPointerException: 无法在空对象上调用方法 getBeanFactory()

我认为问题的原因是 appContext 未正确注入(inject),因此出现 NPE。但我无法解决这个问题。我真的很感谢有人在这方面的帮助

最佳答案

添加 Spring JUnit 类运行程序。您还应该使用 @Test 注释而不是扩展 TestCase。

例如

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})

关于java - Spring 服务层的 JUnit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24091778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com