-1

アノテーションベースの構成を使用して Spring と Hibernate を統合するアプリケーション (Spring MVC 4 + Hibernate/JPA + MySQL + Annotation を使用した Maven 統合の例) があります。

私はこのコントローラーを持っています:

  public class AndroidBackController {

        protected static final Logger LOGGER = LoggerFactory.getLogger(AndroidBackController.class);

        private static final Hashtable<String, Date> SMS_NOTIFICATION = new Hashtable<String, Date>();

@RequestMapping(value = { "/sigfoxCallBack" }, method = RequestMethod.GET)
    public String performAndroidCallBack(@RequestParam Map<String, String> allRequestParams) throws ClientProtocolException, IOException {

 Date lastsmsSend = SMS_NOTIFICATION.get(deviceEvent.getDevice().getKey());

    ..
    }

and this Test:



  public class AndroidBackControllerTest {

        @InjectMocks
        AndroidBackController androidCallBackController;

        @Mock
        DeviceService deviceService;

        @Mock
        DeviceEventService deviceEventService;

        @Mock
        Hashtable<String, Date> SMS_NOTIFICATION = new Hashtable<String, Date>();


        @Mock
        Map<String, String> allRequestParams;

        @BeforeClass
        public void setUp(){
            MockitoAnnotations.initMocks(this);
        }

        @Test
        public void androidCallBack() throws ClientProtocolException, IOException {

             PowerMockito.mockStatic(Hashtable.class);

            Device device = new Device();
            DeviceType deviceType = new DeviceType();
            deviceType.setType("SMARTEVERYTHING_KIT");
            device.setDeviceType(deviceType);

            when(allRequestParams.get("devideId")).thenReturn("E506");
            when(allRequestParams.get("rssi")).thenReturn("155.55");
            when(SMS_NOTIFICATION.get("E506")).thenReturn(new Date());


            when(deviceService.findByKey("E506")).thenReturn(device);



            Assert.assertEquals(androidCallBackController.performAndroidCallBack(allRequestParams), "alldevices");

        }        
    }

しかし、次の行に java.lang.NullPointerException があります。

日付 lastsmsSend = SMS_NOTIFICATION.get(deviceEvent.getDevice().getKey());

4

2 に答える 2

0

すでに述べたように、Mockito は静的フィールドにモックを挿入できません (PowerMock は final 修飾子を削除するため、final フィールドについては特に触れません)。

以下を使用して制限をバイパスできます。

Whitebox.setInternalState(AndroidBackController.class, "SMS_NOTIFICATION", smsNnotification);
于 2016-03-18T08:02:26.350 に答える