0

これは私がテストしたい方法です

@PreAuthorize("isAuthenticated() and hasPermission(#request, 'CREATE_REQUISITION')")
    @RequestMapping(method = RequestMethod.POST, value = "/trade/createrequisition")
    public @ResponseBody
    void createRequisition(@RequestBody CreateRequisitionRO[] request,
            @RequestHeader("validateOnly") boolean validateOnly) {
        logger.debug("Starting createRequisition()...");
        for (int i = 0; i < request.length; i++) {
            CreateRequisitionRO requisitionRequest = request[i];

            // FIXME this has to be removed/moved
            requisitionRequest.setFundManager(requisitionRequest.getUserId());
            // FIXME might have to search using param level as well
            SystemDefault sysDefault = dbFuncs.references.systemDefault
                    .findByCompanyAndDivisionAndPortfolio(
                            userContext.getCompany(),
                            userContext.getDivision(),
                            requisitionRequest.getPortfolio());
            requisitionRequest.setCustodianN(sysDefault.getCustodianN());

            gateKeeper.route(requisitionRequest);
        }
    }

そしてこれはtestNGクラスです:

package in.hexgen.api.facade;


import static org.mockito.AdditionalMatchers.not;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.springframework.security.authentication.BadCredentialsException;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;

import com.hexgen.api.facade.HexgenWebAPI;
import com.hexgen.api.facade.security.HexGenPermissionEvaluator;
import com.hexgen.ro.request.CreateRequisitionRO;

public class HexGenPermissionEvaluatorTest {
    private static final Logger logger = LoggerFactory
            .getLogger(HexGenPermissionEvaluatorTest.class);

       private HexGenPermissionEvaluator filter; //actual class to test
       private AuthenticationManager manager;

    private Authentication authentication;
    private Object name = "chandru";
    private Object permission = "CREATE_REQUISITION";
    CreateRequisitionRO[] request;
    private HexGenPermissionEvaluator permissionEval;
    private HexgenWebAPI webAPI;


    @BeforeTest
       public void setUp() throws Exception {
           SecurityContextHolder.clearContext();
           UsernamePasswordAuthenticationToken rodRequest = new UsernamePasswordAuthenticationToken(name.toString(), name.toString());
           //rodRequest.setDetails(new WebAuthenticationDetails(new MockHttpServletRequest()));
           Authentication rod =
               new UsernamePasswordAuthenticationToken(name.toString(), name.toString(), AuthorityUtils.createAuthorityList("CREATE_REQUISITION"));

           manager = mock(AuthenticationManager.class);
           when(manager.authenticate(rodRequest)).thenReturn(rod);
           when(manager.authenticate(not(eq(rodRequest)))).thenThrow(new BadCredentialsException(""));

       }


    @Test
    public void createRequisitionTest() {
        logger.debug("createRequisition Generate - starting ...");
            webAPI.createRequisition(request, true);
        logger.debug("createRequisition Generate - completed ...");
    }


}

私のTestNGレポート:

createRequisitionTest
"null"
in.hexgen.api.facade.HexGenPermissionEvaluatorTest.createRequisitionTest(HexGenPermissionEvaluatorTest.java:76)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
org.testng.TestRunner.privateRun(TestRunner.java:768)
org.testng.TestRunner.run(TestRunner.java:617)
org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
org.testng.SuiteRunner.run(SuiteRunner.java:240)
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
org.testng.TestNG.runSuitesSequentially(TestNG.java:1188)
org.testng.TestNG.runSuitesLocally(TestNG.java:1113)
org.testng.TestNG.run(TestNG.java:1025)
org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:76)
org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:161)
org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:101)
org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:115)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103)
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)

私を訂正してください、そして私がどこで間違いを犯したのか本当に理解していません.

よろしくお願いします

4

1 に答える 1

1

webAPI メンバーを初期化していないように見えるため、次の行で NPE が発生します。

webAPI.createRequisition(request, true);
于 2013-03-22T13:42:53.067 に答える