JUnits中@RepositoryRestResource的URL上的404

时间:2018-11-04 16:16:25

标签: java rest spring-boot spring-data-jpa junit4

JUnits丢失或配置错误-我找不到。 在Junits中获取Selangor而不是404200处的应用程序显示预期的响应

pom.xml

http://localhost:8080/app

application.yml [主要和测试都相同]

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.github.anandchakru</groupId>
    <artifactId>jtest</artifactId>
    <version>1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!-- DB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- commons -->
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <!-- logging -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>
        <!-- Testing -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

JTestInit.java

logging:
  level:
    org.springframework: INFO
    org.hibernate: ERROR
    jtest: DEBUG
  file: jtest.log
spring: 
  datasource: 
    url: jdbc:mysql://localhost:3306/cifi3?useSSL=false&allowPublicKeyRetrieval=true
    username: tester
    password: tester
  jpa: 
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    hibernate:
      ddl-auto: create

AppRepo.java

@Configuration
@EntityScan({ "jtest" })
@ComponentScan({ "jtest" })
@SpringBootApplication
public class JTestInit {
    private static final Logger logger = LoggerFactory.getLogger("jtest");

    public static void main(String[] args) throws Exception {
        SpringApplication.run(JTestInit.class, args);
        logger.debug("Initialized CifiCore.");
    }
}

App.java

@RepositoryRestResource(path = "app")
public interface AppRepo extends PagingAndSortingRepository<App, Long> {}

AppRepoTest.java

@Entity
@Table(name = "app")
public class App {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long appId;
    private String appName;
    //getters,setters
}

浏览器响应:

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebMvcTest
@RunWith(SpringRunner.class)
@Import({ JTestInit.class })
public class AppRepoTest {
    @Autowired
    private MockMvc mockMvc;
    ObjectMapper json = new ObjectMapper();

    @Test
    public void testFindAll() throws Exception {
        mockMvc.perform(get("/app/")).andDo(print()).andExpect(status().isOk());
    }
}

** AppRepoTest-为什么?????? **

{
  "_embedded" : {
    "apps" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/app{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/app"
    },
    "search" : {
      "href" : "http://localhost:8080/app/search"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 0,
    "totalPages" : 0,
    "number" : 0
  }
}

1 个答案:

答案 0 :(得分:0)

@WebMvcTest仅应用于Controllers。因此,@RepositoryRestResource类没有初始化。

@AutoConfigureMockMvc@SpringBootTest代替它就可以了。

其他类似的测试实用程序批注:

@WebMvcTest-用于控制器

@JsonTest-用于JSON封送和封送

@DataJpaTest-用于存储库

@RestClientTests-用于REST客户端

@SpringBootTest-其他所有内容