Junit 使用jsonpath和mockito进行模拟

Junit 使用jsonpath和mockito进行模拟,junit,mockito,jsonpath,mockmvc,Junit,Mockito,Jsonpath,Mockmvc,如果你知道这不是编译的原因,“.andExpect(JsonPath($.title)是(“测试博客条目”)”)会在EclipseLuna中引起问题。没有它也行。这是一个springmvc教程应用程序,pom也附在下面。 包com.spring.angular.tutorial.web @InjectMocks private BlogEntryController blogEntryController; private MockMvc mockMvc; @Before public vo

如果你知道这不是编译的原因,“.andExpect(JsonPath($.title)是(“测试博客条目”)”)会在EclipseLuna中引起问题。没有它也行。这是一个springmvc教程应用程序,pom也附在下面。 包com.spring.angular.tutorial.web

@InjectMocks
private BlogEntryController blogEntryController;

private MockMvc mockMvc;

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

@Test
public void viewTest() throws Exception{
    //mockMvc.perform(get("/")).andDo(print());
    mockMvc.perform(post("/")
            .content("{\"title\":\"Test Blog Entry coming in\"}")
            .contentType(MediaType.APPLICATION_JSON)                        
    ).andExpect(JsonPath("$.title", is("Test Blog Entry")))
    .andDo(print());
}   
}

pom是:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring_angular_教程 spring_angular_教程 0.0.1-快照 战争


org.springframework
春豆
4.2.0.1发布
org.springframework
弹簧芯
4.2.0.1发布
编译
org.springframework
SpringWebMVC
4.2.0.1发布
org.springframework
spring上下文
4.2.0.1发布
运行时
javax.servlet
javax.servlet-api
3.1.0
假如
log4j
log4j
1.2.17
编译
朱尼特
朱尼特
4.12
测试
org.springframework
弹簧试验
4.2.0.1发布
测试
com.jayway.jsonpath
json路径
2.0.0
测试
com.jayway.jsonpath
json路径断言
2.0.0
测试
com.fasterxml.jackson.core
杰克逊核心
2.6.1
com.fasterxml.jackson.core
杰克逊注释
2.6.1
com.fasterxml.jackson.core
杰克逊数据绑定
2.6.1
org.mockito
莫基托所有
2.0.2-beta
测试
org.apache.tomcat.maven
tomcat7 maven插件
2.2
org.apache.maven.plugins
maven编译器插件
3.3

好的,通过将.andExpect(JsonPath($.title),is(“Test Blog Entry”))替换为andExpect(JsonPath($.title”).value(“Test Blog Entry”))得到了预期的结果。我不确定为什么原始版本不起作用,因为它与教程中使用的syntex相同,可能是JsonPath库发生了变化(教程中的0.9.x,我使用的是2.0)?不是很好的答案,但是如果其他人有相同的问题,如果有人知道的话,我们仍然会对更好的解释感兴趣。好的,通过将.andExpect(JsonPath($.title),is(“Test Blog Entry”))替换为andExpect(JsonPath($.title”).value(“Test Blog Entry”),得到了预期的结果不确定为什么原始版本不起作用,因为它与本教程中使用的syntex相同,也许jsonPath库已经更改(本教程中为0.9.x,我使用的是2.0)?这不是一个很好的答案,但如果其他人也有同样的问题,如果有人知道的话,我仍然希望得到更好的解释。
<dependencies>

    <!-- Spring dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.0.RELEASE</version>
        <scope>compile</scope>
        <!-- <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> 
            </exclusion> </exclusions> -->
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.0.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- logging dependencies -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>compile</scope>
    </dependency>

    <!-- Testing dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.2.0.RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>2.0.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path-assert</artifactId>
        <version>2.0.0</version>
        <scope>test</scope>
    </dependency>

    <!-- jackson dependencies NOTE: needed to remove type bundle for maven to accept them-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>2.0.2-beta</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
        </plugin>
    </plugins>
</build>