Reputation: 1039
I need to test a service by JUnit.
Here is my code :
public class AdviesBoxTestDaoImpl {
private SearchDaoImpl searchDaoImpl;
private SearchParametersDto searchParametersDto;
JSONObject jsonObject;
@Before
public void loadJsonFile(){
try{
ObjectMapper mapper = new ObjectMapper();
searchParametersDto = mapper.readValue(new File("D:\\productsData.json"), SearchParametersDto.class);
}
catch(Exception e){
}
}
@Test
public void testsDemoMethod() throws SQLException {
System.out.println(searchParametersDto.toString());
assertEquals( "Products saved successfully",
searchDaoImpl.inTable(searchParametersDto));
}
}
Result of my service is message as "Products saved successfully" in String which is I am comparing here. Each time I run the test case , I get the NullPointerException Error.
What changes I should make in the code so that I can test the service correctly?
Upvotes: 2
Views: 63
Reputation: 24510
You should not catch the exception in the loadJsonFile()
method. It hides any exception and you don't see the real cause of failing tests. Here is an improved loadJsonFile()
.
@Before
public void loadJsonFile() throws Exception {
ObjectMapper mapper = new ObjectMapper();
searchParametersDto = mapper.readValue(
new File("D:\\productsData.json"),
SearchParametersDto.class
);
}
Upvotes: 1