简体   繁体   English

Mockito Junit: ExceptionInIntialize and NullPointer Error on struts DispatchAction.java when running the Junit tests on a class that extends it

[英]Mockito Junit : ExceptionInIntialize and NullPointer Error on struts DispatchAction.java when running the Junit tests on a class that extends it

I have a class ClassA which is extending DispatchAction class from org.apache.struts.actions.DispatchAction, I have written JUnit code coverage test for ClassA but whenever I try to run the test case I keep getting these error but if I remove the extends DispatchAction from ClassA the test runs normally without any error but then obviously my application won't work ClassA I have a class ClassA which is extending DispatchAction class from org.apache.struts.actions.DispatchAction, I have written JUnit code coverage test for ClassA but whenever I try to run the test case I keep getting these error but if I remove the extends DispatchAction来自 ClassA 的extends DispatchAction测试正常运行,没有任何错误,但显然我的应用程序将无法运行 ClassA

public class ClassA extends DispatchAction {
//some methods and other code
}

TestClassA测试类A

@RunWith(Junit4.class)
public class TestClassA {
@InjectMocks
ClassA objA;
/* mock some objects here */
@Before
public void setUp()
{
MockitoAnnotations.openMocks(this);
}
@Test
public void methodToTest()
{
//Coverage code
objA.methodA();
}
}

Here is the stack trace I get when I run this test case (when I keep "extends DispatchAction" in class ClassA )这是我在运行此测试用例时得到的堆栈跟踪(当我在 class ClassA 中保留“扩展 DispatchAction”时)

java.lang.ExceptionInInitializerError at 
java.lang.J9VMInternals.ensureError(J9VMInternals.java:148) at 
java.lang.J9VMInternals.recordInitializationFailure(J9VMInternals.java:137) at 
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:83) at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57) at 
java.lang.reflect.Constructor.newInstance(Constructor.java:437) at 
org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217) at 
org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at 
org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at 
org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at 
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at 
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at 
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at 
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at 
org.junit.runners.ParentRunner.run(ParentRunner.java:363) at 
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209) 

Caused by: java.lang.NullPointerException at 
org.apache.struts.util.MessageResources.getMessageResources(MessageResources.java:577) at 
org.apache.struts.actions.DispatchAction.<clinit>(DispatchAction.java:153) ... 22 more

Now if I remove the "extends DispatchAction" from class ClassA it will run and cover the intended lines in the test case without any problems but my application won't work as intended.现在,如果我从 class ClassA 中删除“扩展 DispatchAction”,它将运行并覆盖测试用例中的预期行而没有任何问题,但我的应用程序将无法按预期工作。

I tried searching on how to resolve all those errors but I cannot find anything that might help.我尝试搜索如何解决所有这些错误,但找不到任何可能有帮助的东西。

Resolved the issue by adding the latest j2ee jar which was missing earlier.通过添加之前缺失的最新 j2ee jar 解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mockito JUnit Runner NullPointer 异常错误 - Mockito JUnit Runner NullPointer Exception Error 运行junit测试时的ZipException - ZipException when running junit tests 在 JUnit 中运行 Mockito 测试时出现异常 5 - Exception When Running Mockito Test in JUnit 5 从 IntelliJ IDEA (Android) 运行 JUnit 测试时“找不到类” - “Class not found” when running JUnit tests from IntelliJ IDEA (Android) 从测试jar运行JUnit测试时找不到类异常 - Class Not Found Exception When Running JUnit Tests from test jar Maven 在构建 Java 项目时运行带有 junit @Ignore 注释的测试 - Maven running tests annotated with junit @Ignore when building Java project 在程序包中运行所有测试时的JUnit java.lang.OutOfMemoryError - JUnit java.lang.OutOfMemoryError when running all tests in a package 运行JUnit / Mockito测试时org.springframework.http.converter.HttpMessageNotReadableException - org.springframework.http.converter.HttpMessageNotReadableException when running JUnit/Mockito Tests JUnit参数化测试未与Mockito,Spring,Reflection和AspectJ隔离运行 - JUnit Parameterized tests not running in isolation with Mockito, Spring, Reflection and AspectJ 我在Struts应用程序中运行JUNIT测试时遇到java.lang.ExceptionInInitializerError - I am getting java.lang.ExceptionInInitializerError while running JUNIT tests in Struts application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM