这是我的testHelper:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:WEB-INF/spring/appServlet/servlet-context.xml" })
public class TestHelper {
@Mock
private UserDaoImpl userDaoImpl;
@InjectMocks
private Helper helper=new Helper();
private MockMvc mockMvc;
String msg = "Success";
int status = 200;
ResponseMessage message = new ResponseMessage();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(helper).build();
}
@Test
public void testCategories() throws Exception {
message = helper.getCategories(0);
assertNotNull(message);
Assert.assertEquals(status, message.getStatus());
Assert.assertEquals(msg, message.getMessage());
} }
这是我的助手课程:
public class Helper extends BaseHelper {
@Autowired
private UserDao userDao;
private static final Logger logger = LoggerFactory
.getLogger(Helper.class);
public ResponseMessage getCategories(int categoryID) {
logger.info("**Helper getCategories() entry*****");
ResponseMessage message;
try {
List<Category> categories = userDao
.getCategories(categoryID);
String jsonConverted = Utility
.convertToJsonString(categories);
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
Object obj = mapper
.readValue(
jsonConverted,
new TypeReference<List<Category>>() {
});
message = prepareResponse(obj, 200, "Success");
} catch (Exception e) {
logger.error("****Exception in Helper getCategories() : "
+ e.getStackTrace());
message = prepareResponse( null, 500,
e.getMessage());
}
logger.info("***Helper getCategories() exit****");
return message;
}
}
这是我的UserDaoImpl类:
public class UserDaoImpl implements UserDao {
private static Properties applicationProperties;
static {
applicationProperties = Utility.loadAppProperties();
}
public List<Category> getCategories(int category_id) {
Session session = null;
List<Category> categories = null;
try {
session = sessionFactory.getCurrentSession();
logger.info("enter into getCategories from method's body");
Transaction tx = session.beginTransaction();
if (!Utility.isZeroInt(category_id)) {
List list = session.createCriteria(Category.class).add(Restrictions.eq("category_id", category_id))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
categories = list;
} else {
List list = session.createCriteria(Category.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
categories = list;
}
tx.commit();
} catch (Exception e) {
throw new DataException("Unable to get categories. Please try after some time.");
}
logger.info("exit from getCategories method's body");
return categories;
}
}
当我测试我的助手类时,我在userdaoimpl类的applicationProperties = Utility.loadAppProperties();
上得到了错误。而错误是
java.lang.ExceptionInInitializerError
。在userdaoimpl类中加载属性文件时会生成此错误。
这是堆栈跟踪
java.lang.ExceptionInInitializerError
at com.zon.dao.UserDaoImpl.<clinit>(UserDaoImpl.java:71)
at sun.reflect.GeneratedSerializationConstructorAccessor1.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:111)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:51)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:54)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:44)
at org.mockito.Mockito.mock(Mockito.java:882)
at org.mockito.internal.configuration.DefaultAnnotationEngine.processAnnotationOn(DefaultAnnotationEngine.java:59)
at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:34)
at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:84)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
at org.mockito.MockitoAnnotations.scan(MockitoAnnotations.java:110)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:95)
at com.zon.testHelper.TestHelper.setup(TestHelper.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at com.zon.utils.Utility.<clinit>(Utility.java:27)
... 39 more
答案 0 :(得分:1)
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
上述异常表示无法找到属性文件。请尝试将属性文件放在类路径的根目录下。