Hashmap 在 Spring Boot 中包含键 junit
Hashmap containskey junit in Spring Boot
我有以下class
public class EmployeeService{
private final EmployeeRepository employeeRepo;
private final Map<String, Employee> cache;
@Autowired
public EmployeeService(EmployeeRepository employeeRepo, Map<String, Employee> cache){
this.employeeRepo = employeeRepo;
this.cache = cache;
}
public void loadFromDB(){
//repo call
cache.put("123", {employee object})
}
}
我想为此编写一个 junit,我需要检查该值是否已插入到缓存中。我在下面尝试过,但缓存中没有任何值。
@Mock
private EmployeeRepository employeeRepo;
@Mock
private Map<String, Employee> cache;
@InjectMocks
private EmployeeService employeeService;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
employeeService = new EmployeeService(employeeRepo, cache);
}
@Test
public void shouldLoadDataFromDBtoCache(){
when(employeeRepo.findActiveEmployee()).thenReturn(buildDataFromDB());
EmployeeService.loadFromDB();
Assertions.assertFalse(cache.isEmpty());
//Assertions.assertTrue(cache.containsKey("1625"));
//Assertions.assertTrue(cache.containsKey("1525"));
//Assertions.assertFalse(cache.containsKey("1425"));
}
当我检查缓存映射的大小为零时,cache.containsKey()
得到 assertion error
。
Assertions.assertFalse(cache.isEmpty()); // this is success.
如何测试 hashmap containsKey
如上。
据我所知,您需要做的最小改动如下:
@Mock
private EmployeeRepository employeeRepo;
private Map<String, Employee> cache = new HashMap<>();
private EmployeeService employeeService = new EmployeeService(employeeRepo, cache);
@Test
public void shouldLoadDataFromDBtoCache(){
when(employeeRepo.findActiveEmployee()).thenReturn(buildDataFromDB());
employeeService.loadFromDB();
Assertions.assertFalse(cache.isEmpty());
//Assertions.assertTrue(cache.containsKey("1625"));
//Assertions.assertTrue(cache.containsKey("1525"));
//Assertions.assertFalse(cache.containsKey("1425"));
}
我有以下class
public class EmployeeService{
private final EmployeeRepository employeeRepo;
private final Map<String, Employee> cache;
@Autowired
public EmployeeService(EmployeeRepository employeeRepo, Map<String, Employee> cache){
this.employeeRepo = employeeRepo;
this.cache = cache;
}
public void loadFromDB(){
//repo call
cache.put("123", {employee object})
}
}
我想为此编写一个 junit,我需要检查该值是否已插入到缓存中。我在下面尝试过,但缓存中没有任何值。
@Mock
private EmployeeRepository employeeRepo;
@Mock
private Map<String, Employee> cache;
@InjectMocks
private EmployeeService employeeService;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
employeeService = new EmployeeService(employeeRepo, cache);
}
@Test
public void shouldLoadDataFromDBtoCache(){
when(employeeRepo.findActiveEmployee()).thenReturn(buildDataFromDB());
EmployeeService.loadFromDB();
Assertions.assertFalse(cache.isEmpty());
//Assertions.assertTrue(cache.containsKey("1625"));
//Assertions.assertTrue(cache.containsKey("1525"));
//Assertions.assertFalse(cache.containsKey("1425"));
}
当我检查缓存映射的大小为零时,cache.containsKey()
得到 assertion error
。
Assertions.assertFalse(cache.isEmpty()); // this is success.
如何测试 hashmap containsKey
如上。
据我所知,您需要做的最小改动如下:
@Mock
private EmployeeRepository employeeRepo;
private Map<String, Employee> cache = new HashMap<>();
private EmployeeService employeeService = new EmployeeService(employeeRepo, cache);
@Test
public void shouldLoadDataFromDBtoCache(){
when(employeeRepo.findActiveEmployee()).thenReturn(buildDataFromDB());
employeeService.loadFromDB();
Assertions.assertFalse(cache.isEmpty());
//Assertions.assertTrue(cache.containsKey("1625"));
//Assertions.assertTrue(cache.containsKey("1525"));
//Assertions.assertFalse(cache.containsKey("1425"));
}