Junit测试用例是否在for循环内

时间:2019-01-23 11:55:11

标签: java junit

由于#include "shared_memory.h" #include "logger.h" namespace apl_shared_memory { bool write_to_memory(std::string wsuid, std::string loop_val, std::string should_intercept, std::string post_data) { bool ret_val; std::string shm_name = shm_prefix + wsuid; std::string mtx_name = mutex_prefix + wsuid; boost::interprocess::named_mutex named_mtx{ boost::interprocess::open_or_create, mtx_name.c_str() }; // Add the size of all the structures we're putting in the shared memory region and add some for the overhead. size_t size = (3*sizeof(IPCString) + loop_val.size() + should_intercept.size() + post_data.size() + sizeof(ShmVector)) + 500; try { named_mtx.lock(); boost::interprocess::shared_memory_object::remove(shm_name.c_str()); boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only, shm_name.c_str(), size); CharAllocator charallocator(segment.get_segment_manager()); StringAllocator stringallocator(segment.get_segment_manager()); IPCString shm_loop_val(charallocator); IPCString shm_should_intercept(charallocator); IPCString shm_intercepted_data(charallocator); shm_loop_val = loop_val.c_str(); shm_should_intercept = should_intercept.c_str(); shm_intercepted_data = post_data.c_str(); segment.destroy<ShmVector>("ShmVector"); ShmVector *shmVector = segment.construct<ShmVector>("ShmVector")(stringallocator); shmVector->clear(); shmVector->reserve(3); // push_back will call a copy-constructor. But, if we send a rvalue reference (i.e. if we move it), there will be no copying. shmVector->push_back(boost::move(shm_loop_val)); shmVector->push_back(boost::move(shm_should_intercept)); shmVector->push_back(boost::move(shm_intercepted_data)); ret_val = true; } catch (const std::exception& ex) { ret_val = false; boost::interprocess::shared_memory_object::remove(shm_name.c_str()); } named_mtx.unlock(); return ret_val; } } ,我的测试用例失败了。另外,不确定如何在junit中处理或调试此null错误。

以下是测试用例代码:

NullPointerException

由于以下代码很容易导致我的测试案例中出现List<String> groupName = new ArrayList<String>(); when(claimGroupingHistoryRepository.getGroupingKeyName(1l)).thenReturn(groupName); service.updateClaimGroupingKeyForDealer(dealer.getDealerId(), groupingKeyResource, userId); 错误。如果null pointer内的if block注释未收到错误。不确定如何在此处测试if条件。

for-loop

以下是正在得到的测试用例错误:

for(GroupingKeys groupingKey : groupingKeyResource.getGroupingKeys()){          
          if(groupingKey.getValue() != 0) {  // no sure how to check this condition using junit
              List<String> name = claimGroupingHistoryRepository.getGroupingKeyName(groupingKey.getId());
              for(String nameList:name) {
                  if(!groupingName.equalsIgnoreCase("")) {
                      groupingName = groupingName.concat(", ").concat(nameList);
                  } else {
                      groupingName = nameList;
                  }

              }
          } 
      }

请找到我完整的测试文件

已更新:1 *

ClaimServiceImplTest.shouldUpdateClaimGroupingKeyForDealerUpdate:6057 » NullPointer

1 个答案:

答案 0 :(得分:1)

除非我们看到GroupingKeys的定义,否则无法确定这一点,但是获得NPE的原因是因为字段value不是基元。

为避免NPE,您可以将GroupingKeys(特别是value)初始化为某个数字。