一尘不染

Spring服务层的JUnit测试案例

spring-mvc

我正在尝试配置JUnit并为Spring 3.2
MVC应用程序的Service层编写测试用例。我找不到有关如何从头开始配置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
}

我的测试班(部分使用过时髦)

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未正确注入,因此正在获得NPE。但是我无法解决这个问题。我真的很感谢某人对此的帮助


阅读 288

收藏
2020-06-01

共1个答案

一尘不染

添加Spring JUnit类运行器。您还应该使用@Test批注,而不是扩展TestCase。

例如

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