简单的JUnit测试assertTrue(true)失败

时间:2015-11-30 21:51:27

标签: java spring spring-mvc junit junit4

为什么以下测试失败?不应该{0}永远是真的吗?

assertTrue(true)

堆栈跟踪如下:

package a.a.a;

import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Timed;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ConfigurationClass.class})
@WebAppConfiguration
public class JUnitConfigurationClass {

    @Autowired
    WebApplicationContext wac;
    MockMvc mockMvc;

    @Before(value = "")
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void init() {
        assertTrue(true);
    }
}

2 个答案:

答案 0 :(得分:1)

AspectJ应该做的@Before注释是什么?我认为以前没有调用init(),所以mockMvc不是构建的。您应该尝试使用org.junit.Before

另一个镜头:如果您按mockMvc构建MockMvcBuilders.standaloneSetup(),行为是否相同?可能您的上下文未正确设置。 javax.servlet.SessionCookieConfig似乎在运行时不可用,也许您应该检查依赖项。

答案 1 :(得分:1)

断言没有失败,那就是“AssertError”,但你的单元测试在运行时抛出异常。在这种情况下,java.lang.NoClassDefFoundError。当您在类路径中没有该类时会发生这种情况。缺少的班级是javax.servlet.SessionCookieConfig。 servlet API是否在类路径中?

相关问题