简体   繁体   English

使用Mockito和MockMvc的方法中的缺少代码属性

[英]Absent Code attribute in method with Mockito and MockMvc

public class ExampleRestControllerTest {

     private MockMvc mockMvc;

     @Mock
     private MyService myService;

     @InjectMocks
     private MyRestController myRestController;

     @Before
     public void setUp() {
        MockitoAnnotations.initMocks(this);
        StandaloneMockMvcBuilder setup =    MockMvcBuilders.standaloneSetup(myService);
        mockMvc = setup.build();
}

When MockMvcBuilders.standaloneSetup(myService); MockMvcBuilders.standaloneSetup(myService); is executed I have returned this stack trace: 执行后,我已返回此堆栈跟踪:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup(MockMvcBuilders.java:75)
at it.my.test.main.MyServiceRestControllerTest.setUp(MyServiceRestControllerTest.java:39)
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:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
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:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)

What may be wrong? 有什么问题吗?

Thanks 谢谢

standaloneSetup() takes your controller. standaloneSetup()您的控制器。 Maybe the following example can help you: 也许以下示例可以为您提供帮助:

@RunWith(MockitoJUnitRunner.class)
public class IndexControllerTest {

    private IndexController classUnderTest;

    @Mock
    private IndexService mockedService;

    @Before
    public void setUp() {
        classUnderTest = new IndexController(mockedService);
    }

    @Test
    public void testMockMvc() throws Exception{
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(classUnderTest).build();

        mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index"));

    }
}

暂无
暂无

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

相关问题 控制器Spring 4 MockMvc缺少代码属性的单元测试 - Unit Test of Controller Spring 4 MockMvc Absent Code Attribute 类ClassFormatError:方法不是本机的缺少代码属性 - Class ClassFormatError :Absent-Code-attribute-in-method-that-is-not-native 类文件中不是本机或抽象的方法中的Testng缺少代码属性 - Testng Absent Code attribute in method that is not native or abstract in class file 使用 MockMvc 和 Mockito 的测试 Get 方法 - Test Get method using MockMvc and Mockito 在类文件javax / faces / webapp / FacesServlet中的非本机或抽象方法中的Absent Code属性 - Absent Code attribute in method that is not native or abstract in class file javax/faces/webapp/FacesServlet Java EE 6中类文件javax / mail / MessagingException中不是本机或抽象的方法中的缺少Code属性 - Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException in Java EE 6 ClassFormatError:在类文件 javax/mail/MessagingException 中非本机或抽象的方法中缺少代码属性 - ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException 方法中的Absent Code属性在类文件javax / servlet / ServletException中不是本机的或抽象的 - Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException class 文件 javax/validation/ConstraintViolationException 中非本机或抽象方法中的缺失代码属性 - Absent Code attribute in method that is not native or abstract in class file javax/validation/ConstraintViolationException java.lang.ClassFormatError:类文件javax / faces / FacesException中不是本机或抽象的方法中的缺少Code属性 - java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/FacesException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM