java - How can I test this init() method of PostConstruct using Spring container?
问题描述
I'm using the latest spring-boot-gradle-plugin 2.0 to write unit tests. Long story short, I wanted to write a unit test for the init() postConstruct in the following class:
@Service
public class UserIdService implements IUserIdService
{
// Local store
private Map<String, Map<String, String>> store = new TreeMap<>();
@Autowired
private ResourceLoader resourceLoader;
private static final String CSV_FILE_LOCATION = "data/userid-data.csv";
@PostConstruct
public void init()
{
System.out.println("Loading data from CSV file to UserID Store...");
int totalRecords = 0;
Resource resource = resourceLoader.getResource("classpath:" + CSV_FILE_LOCATION);
try
{
InputStream inputStream = resource.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(inputStreamReader);
for (CSVRecord record : records)
{
totalRecords++;
String number = record.get(0);
String context = record.get(1);
String name = record.get(2);
number = PhoneNumberHelper.convertPhoneNumber(number);
//add only valid numbers to store
if(!number.equals(PhoneNumberHelper.INVALID_INPUT))
{
add(new UserId(name, number, context));
}
}
}
catch (Exception e)
{
System.err.println("Can't load or parse CSV file: " + CSV_FILE_LOCATION);
}
System.out.println("Loaded total [ " + store.size() + " / " + totalRecords + " ] records.");
}
public boolean add(UserId record)
{
if (null == store.get(record.getNumber()))
{
Map<String, String> newContext = new TreeMap<>();
newContext.put(record.getContext(), record.getName());
store.put(record.getNumber(), newContext);
}
else
{
Map<String, String> currentContext = store.get(record.getNumber());
if (currentContext.get(record.getContext()) == null)
{
currentContext.put(record.getContext(), record.getName());
}
else
{
//context already exists for the phone number, throw error
System.err.println("Context already exists for the phone number: " + record.toString());
return false;
}
}
return true;
}
public List<UserId> get(String number)
{
List<UserId> result = new ArrayList<>();
if (null != store.get(number))
{
Map<String, String> currentContext = store.get(number);
for (Map.Entry<String, String> entry : currentContext.entrySet())
{
UserId record = new UserId(entry.getValue(), number, entry.getKey());
result.add(record);
}
}
return result;
}
public boolean contains(String number)
{
return store.containsKey(number);
}
}
The test class I created looks like this:
@SpringBootApplication
public class UserIdServiceTest {
@Autowired
private UserIdService userIdService;
@Test
public void testInit() throws Exception {
userIdService.init();
// assert things here..
}
@Test
public void testAdd() {
}
@Test
public void testGet() {
}
@Test
public void testContains() {
}
}
When I try to run it, the test fails with:
UserIdServiceTest > testInit FAILED java.lang.NullPointerException at UserIdServiceTest.testInit(UserIdServiceTest.java:21)
I've been at this for days. What am I missing? - I'd like to be able to create tests for the methods described, but no matter where I reference userIdService, I always get the same NullPointerException.
Thanks for looking.
解决方案
推荐阅读
- python - Testdriven.io:运行 Github Action 测试 Docker 映像时使用 FastAPI 和 Docker 错误的测试驱动开发
- webdriver-io - 我们如何在 WebdriverIO 中将期望用作独立脚本?
- macros - Zabbix:如何将来自 Web 场景检查的错误消息解析为宏
- internet-explorer - 为什么更改 Internet Explorer(版本 11 及更低版本)中的 innerText 会永久破坏所有后代文本节点?
- typescript - TypeScript:'string' 可分配给类型 T 的约束,但 T 可以用约束 'string | 的不同子类型实例化 细绳[]'
- c++ - 向量指针的访问向量
- java - 无法获取已声明的字符串
- c - 使用 2 次时 scanf 函数出现问题
- javascript - Discord.js v13 重启机器人后反应角色按钮不起作用
- javascript - Fabric JS在缩放视图中获取鼠标点击的相对坐标