Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Generation exception #1664

Closed
xiongzhenggang opened this issue May 24, 2021 · 5 comments
Closed

Test Generation exception #1664

xiongzhenggang opened this issue May 24, 2021 · 5 comments

Comments

@xiongzhenggang
Copy link

Describe the bug
Contract generates Test class exception . There isn't contract methond.

Sample
pom

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-cloud-contract.version>3.0.2</spring-cloud-contract.version>
        <spring-cloud.version>Hoxton.SR11</spring-cloud.version>
    </properties>

following contract:

description: Some description
label: some_label
input:
  messageFrom: kafka-topic
  messageBody:
    bookName: 'foo'
  messageHeaders:
    sample: 'header'
outputMessage:
  sentTo: kafka-topic
  body:
    bookName: foo
  headers:
    BOOK-NAME: foo

the following test would be created. use command : mvn clean install -Dtest

[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ scc-pruducer ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\work\workspaces\scc\scc-pruducer\target\test-classes
[INFO] /C:/work/workspaces/scc/scc-pruducer/target/generated-test-sources/contracts/com/xzg/test/scc/MessageTest.java: C:\work\workspaces\scc\scc-pruducer\target\generated-test-sources\contracts\com\xzg\test\scc\MessageTest.java使用了未经检查或不安全的操作。
[INFO] /C:/work/workspaces/scc/scc-pruducer/target/generated-test-sources/contracts/com/xzg/test/scc/MessageTest.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/work/workspaces/scc/scc-pruducer/target/generated-test-sources/contracts/com/xzg/test/scc/MessageTest.java:[35,41] 找不到符号
  符号:   方法 contract(com.xzg.test.scc.MessageTest,java.lang.String)
  位置: 类 com.xzg.test.scc.MessageTest
[ERROR] /C:/work/workspaces/scc/scc-pruducer/target/generated-test-sources/contracts/com/xzg/test/scc/MessageTest.java:[39,41] 找不到符号
  符号:   方法 contract(com.xzg.test.scc.MessageTest,java.lang.String)
  位置: 类 com.xzg.test.scc.MessageTest
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.238 s
[INFO] Finished at: 2021-05-24T14:58:34+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project scc-pruducer: Compilation failure: Compilation failure:
[ERROR] /C:/work/workspaces/scc/scc-pruducer/target/generated-test-sources/contracts/com/xzg/test/scc/MessageTest.java:[35,41] 找不到符号
[ERROR]   符号:   方法 contract(com.xzg.test.scc.MessageTest,java.lang.String)
[ERROR]   位置: 类 com.xzg.test.scc.MessageTest
[ERROR] /C:/work/workspaces/scc/scc-pruducer/target/generated-test-sources/contracts/com/xzg/test/scc/MessageTest.java:[39,41] 找不到符号
[ERROR]   符号:   方法 contract(com.xzg.test.scc.MessageTest,java.lang.String)
[ERROR]   位置: 类 com.xzg.test.scc.MessageTest
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
  • the following is Test Generation MessageTest.java :
@SuppressWarnings("rawtypes")
public class MessageTest extends BaseCase {
	@Inject ContractVerifierMessaging contractVerifierMessaging;
	@Inject ContractVerifierObjectMapper contractVerifierObjectMapper;

	@Test
	public void validate_triggermessage() throws Exception {
		// given:
			ContractVerifierMessage inputMessage = contractVerifierMessaging.create(
					"{\"bookName\":\"foo\"}"
						, headers()
							.header("sample", "header")
			);

		// when:
			contractVerifierMessaging.send(inputMessage, "kafka-topic",
					contract(this, "triggermessage.yml"));

		// then:
			ContractVerifierMessage response = contractVerifierMessaging.receive("kafka-topic",
					contract(this, "triggermessage.yml"));
			assertThat(response).isNotNull();

		// and:
			assertThat(response.getHeader("BOOK-NAME")).isNotNull();
			assertThat(response.getHeader("BOOK-NAME").toString()).isEqualTo("foo");

		// and:
			DocumentContext parsedJson = JsonPath.parse(contractVerifierObjectMapper.writeValueAsString(response.getPayload()));
			assertThatJson(parsedJson).field("['bookName']").isEqualTo("foo");
	}

}

And contractVerifierMessaging.send Method hasn't parameter contract.

I guess the version is wrong, but I did not find a solution.

@marcingrzejszczak
Copy link
Contributor

Can you paste your maven plugin configuration?

@xiongzhenggang
Copy link
Author

@marcingrzejszczak
the following is maven plugin :

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-contract-maven-plugin</artifactId>
                <version>${spring-cloud-contract.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <baseClassForTests>
                        com.xzg.test.scc.BaseCase
                    </baseClassForTests>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.cloud</groupId>
                        <artifactId>spring-cloud-contract-pact</artifactId>
                        <version>${spring-cloud-contract.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

BaseCase.java

@SpringBootTest(classes = SpringcloudContractProviderRestApplication.class,webEnvironment = SpringBootTest.WebEnvironment.NONE)
// remove::start[]
@AutoConfigureMessageVerifier
@EmbeddedKafka(partitions = 1, topics = {"kafka_topic"})
// remove::end[]
@ActiveProfiles("test")
public class BaseCase {
    @Autowired KafkaController controller;

    public void trigger() {
        this.controller.sendFoo("example");
    }

    public void triggerMessage() {
        this.controller.sendFooAsMessage("example");
    }
        @BeforeEach
        public void setup() {
            RestAssuredMockMvc.standaloneSetup(new FraudController());
        }

}

@marcingrzejszczak
Copy link
Contributor

What is the <version>${spring-cloud-contract.version}</version> value?

@marcingrzejszczak
Copy link
Contributor

Ah I see it's 3.0.2. You're using incompatible release trains. You're using Hoxton against contract 3.x. You should use either Hoxton and maven plugin 2.x or 2020.x and contract 3.x

@xiongzhenggang
Copy link
Author

@marcingrzejszczak Oh . Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants