Jbehave 测试配置和测试报告未按预期工作

问题描述

我正在使用 Jbehave 来测试一些 API。我配置了我的故事,但是当我运行配置文件时,我得到以下输出

Reports view generated with 0 stories (of which 0 pending) containing 0 scenarios (of which 0 pending)

我认为我的配置工作正常,并试图运行我的整个测试包,但它返回一个输出

No tests found in the package "com.example.prederiq.PrederaAiq"

然而,当我运行包含测试用例的 PrederaAiqApplicationTests.java 文件时,所有测试都通过了,但没有返回给我的控制台报告或 html 报告。我在下面的测试包中附上了所有相关文件以及下面的项目结构,请帮我整理一下

项目结构:

我的配置文件

public class StoryConfiguration extends JUnitStories {

    @Override
    public Configuration configuration() {
        return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(this.getClass())).useStoryReporterBuilder(new StoryReporterBuilder().withCodeLocation(CodeLocations.codeLocationFromClass(this.getClass())).withDefaultFormats().withFormats(StoryReporterBuilder.Format.CONSOLE,StoryReporterBuilder.Format.HTML));
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration(),new PrederaAiqApplicationTests());
    }

    @Override
    public List<String> storyPaths() {
        String codeLoc = codeLocationFromClass(this.getClass()).getFile();
        return new StoryFinder().findpaths(codeLoc,asList("**/test*.story"),asList(""),"file"+codeLoc);
    }
}

test.story 文件

Given: An authorised URL
When: A GET req is made
Then: 200 status code is returned

Given:Unauthorised link
When:GET req is made
Then: 401 status code is returned

Given: An authorised URL.
When: A GET req is made to it
Then: application type is json

Given: An authorised URL.
When: A GET req is made to it
Then: output same as that in documentation

PrederaAiqApplicationsTest 文件

class PrederaAiqApplicationTests {
    String contentType;
    String uri = "https://sandBox.predera.com/aiq/api/projects";
    wiremockServer wiremockServer = new wiremockServer(wiremockConfig().dynamicPort().dynamicHttpsPort());
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String auth = "Bearer " + "eyJraWQiOiJndE1YKzh2bVBaNnk0NElmdllGNDZqVDlvRG5RZWxoeUg4d1JjMVwvWkdbnD0iLCJhbGciOiJSUzI1NiJ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Request httpUriRequest;
    @Test
    @Given("An authorised URL")
    void test(){
        httpUriRequest = RequestBuilder.get().setUri(uri).setHeader(HttpHeaders.AUTHORIZATION,auth).build();
    }
    @When("A GET req is made")
    void testGet() throws IOException {
        HttpResponse response =httpClient.execute(httpUriRequest);
        status = response.getStatusLine().getStatusCode();
    }
    @Then("200 status code is returned")
    void test200() {
        assertEquals(200,status);
    }

    @Test
    @Given("Unauthorised link")
    void test2() {
        httpUriRequest = RequestBuilder.get().setUri(uri).build();
    }
    @When("GET req is made")
    void get2() throws IOException {
        HttpResponse response = httpClient.execute(httpUriRequest);
        status = response.getStatusLine().getStatusCode();
    }
    @Then("401 status code is returned")
    void statusTest2(){
        assertEquals(401,status);
    }
    @Test
    @Given("An authorised URL.")
    void test3(){
        httpUriRequest = RequestBuilder.get().setUri(uri).setHeader(HttpHeaders.AUTHORIZATION,auth).build();
    }
    @When("A GET req is made to it")
    void getTest3() throws IOException {
        HttpResponse response = httpClient.execute(httpUriRequest);
        contentType = String.valueOf(response.getEntity().getContentType());
    }
    @Then("application type is json")
    void content(){
        assertEquals("application/json",contentType);
    }
    @Test
    @Given("An authorised URL.")
    void test4(){
        httpUriRequest = RequestBuilder.get().setUri(uri).setHeader(HttpHeaders.AUTHORIZATION,auth).build();
    }
    @When("A GET req is made to it")
    void testGet4() throws IOException,ParseException {
        HttpResponse response = httpClient.execute(httpUriRequest);
        httpentity entity = response.getEntity();
        Scanner scanner = new Scanner(entity.getContent());
        String content = "";
        while(scanner.hasNext()){
            content += scanner.nextLine();
        }
        scanner.close();
        JSONParser parser = new JSONParser();
        array = (JSONArray) parser.parse(content);
    }
    @Then("output same as that in documentation")
    void content2(){
        assertEquals("[{\"owner\":\"[email protected]\",\"name\":\"churn-juyma\",\"description\":\"Customer churn Example\",\"last_modified_date\":\"2021-06-09T17:38:06.048Z\",\"id\":\"a-443018111\",\"created_date\":\"2021-06-09T17:38:06.048Z\",\"last_modified_by\":\"[email protected]\",\"created_by\":\"[email protected]\",\"users\":[\"[email protected]\"]}]",array.toString());
    }
}

Pom.xml:

        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.prederiq</groupId>
    <artifactId>PrederaAiq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>PrederaAiq</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jbehave</groupId>
            <artifactId>jbehave-core</artifactId>
            <version>4.8.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.tomakehurst</groupId>
            <artifactId>wiremock-jre8</artifactId>
            <version>2.28.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)