集成测试和 build.gradle Java Spring。

huangapple 未分类评论49阅读模式
英文:

Integration test and build.gradle Java Spring

问题

  1. 我正在尝试为我的服务编写集成测试
  2. 我已经尝试编写一个简单的测试作为我的第一个测试我的WebControllerIT类如下
  3. @RunWith(SpringRunner.class)
  4. @SpringBootTest(
  5. classes = SocialInteractionApplication.class,
  6. webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  7. public class WebControllerIT {
  8. @LocalServerPort private int webServerPort;
  9. @Autowired private AppUserRepository AppUserRepository;
  10. @Autowired private WebController webController;
  11. private RestTemplate restTemplate = new RestTemplate();
  12. private String createURLWithPort(String uri) {
  13. return "http://localhost:" + webServerPort + uri;
  14. }
  15. @Test
  16. public void testSetNewAppUser() {
  17. HttpEntity<String> entity = new HttpEntity<String>(null,null);
  18. ResponseEntity<String> response =
  19. restTemplate.exchange(
  20. createURLWithPort("/{RobsAndroid}/setNewAppUser/{Rob}"),
  21. HttpMethod.GET,
  22. entity,
  23. String.class);
  24. String expected = "New AppUser: " + "RobsAndroid" + " set OK.";
  25. assertEquals(expected,response.getBody());
  26. assertEquals(HttpStatus.OK,response.getStatusCode());
  27. }
  28. }
  29. 然而我遇到了一个错误
  30. Execution failed for task ':integrationTest'.
  31. > No tests found for given includes: [com.socialinteraction.componenttests.WebControllerIT.testSetNewAppUser](filter.includeTestsMatching)
  32. 我似乎无法弄清楚发生了什么有什么想法吗
  33. 为了提供更多上下文我不确定如何开始进行组件测试这包括它们应该放在哪里它们应该有什么依赖关系如何在gradle构建中运行它们等等我遵循了一个教程我的build.gradle文件最终如下
  34. plugins {
  35. id 'org.springframework.boot' version '2.2.5.RELEASE'
  36. id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  37. id 'java'
  38. }
  39. group = 'com.socialinteraction'
  40. version = '0.0.1-SNAPSHOT'
  41. sourceCompatibility = '1.8'
  42. configurations {
  43. compileOnly {
  44. extendsFrom annotationProcessor
  45. }
  46. }
  47. repositories {
  48. mavenCentral()
  49. }
  50. sourceSets {
  51. integrationTest {
  52. java {
  53. compileClasspath += main.output + test.output
  54. runtimeClasspath += main.output + test.output
  55. srcDir file('src/integration-test/java')
  56. }
  57. resources.srcDir file('src/integration-test/resources')
  58. }
  59. }
  60. configurations {
  61. integrationTestCompile.extendsFrom testCompile
  62. integrationTestRuntime.extendsFrom testRuntime
  63. integrationTestImplementation.extendsFrom testImplementation
  64. }
  65. dependencies {
  66. implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
  67. implementation 'org.springframework.boot:spring-boot-starter-web'
  68. compileOnly 'org.projectlombok:lombok'
  69. annotationProcessor 'org.projectlombok:lombok'
  70. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  71. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  72. }
  73. testImplementation('org.junit.jupiter:junit-jupiter-api:5.4.2')
  74. testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
  75. // testCompile "org.powermock:powermock-module-junit4:1.6.4"
  76. // testCompile 'org.mockito:mockito-core:2.7.22'
  77. implementation 'org.jetbrains:annotations:15.0'
  78. implementation 'junit:junit:4.12'
  79. implementation 'org.testng:testng:6.9.6'
  80. implementation 'junit:junit:4.12'
  81. implementation 'junit:junit:4.12'
  82. implementation 'org.testng:testng:6.9.6'
  83. }
  84. test {
  85. useJUnitPlatform()
  86. }
  87. task integrationTest(type: Test) {
  88. testClassesDirs = sourceSets.integrationTest.output.classesDirs
  89. classpath = sourceSets.integrationTest.runtimeClasspath
  90. }
  91. check.dependsOn integrationTest
  92. integrationTest.mustRunAfter test

此外,关于testRuntimetestImplementation和其他依赖类型的问题,您可以查阅官方文档或相关教程以获得更多详细信息。

英文:

I am trying to write integration tests for my service.

I have tried to write a simple test as my first test. My WebControllerIT class is as follows:

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(
  3. classes = SocialInteractionApplication.class,
  4. webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  5. public class WebControllerIT {
  6. @LocalServerPort private int webServerPort;
  7. @Autowired private AppUserRepository AppUserRepository;
  8. @Autowired private WebController webController;
  9. private RestTemplate restTemplate = new RestTemplate();
  10. private String createURLWithPort(String uri) {
  11. return &quot;http://localhost:&quot; + webServerPort + uri;
  12. }
  13. @Test
  14. public void testSetNewAppUser() {
  15. HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(null,null);
  16. ResponseEntity&lt;String&gt; response =
  17. restTemplate.exchange(
  18. createURLWithPort(&quot;/{RobsAndroid}/setNewAppUser/{Rob}&quot;),
  19. HttpMethod.GET,
  20. entity,
  21. String.class);
  22. String expected = &quot;New AppUser: &quot; + &quot;RobsAndroid&quot;+ &quot; set OK.&quot;;
  23. assertEquals(expected,response.getBody());
  24. assertEquals(HttpStatus.OK,response.getStatusCode());
  25. }
  26. }

However, I am getting an error:

  1. Execution failed for task &#39;:integrationTest&#39;.
  2. &gt; No tests found for given includes: [com.socialinteraction.componenttests.WebControllerIT.testSetNewAppUser](filter.includeTestsMatching)

I can't seem to work out what's going on, any ideas?

To give some more context, I wasn't sure how to start with component testing. This included: where they should be located, what dependencies they should have, how to run them in the gradle build etc. I followed a tutorial and my build.gradle file has ended up as follows:

  1. plugins {
  2. id &#39;org.springframework.boot&#39; version &#39;2.2.5.RELEASE&#39;
  3. id &#39;io.spring.dependency-management&#39; version &#39;1.0.9.RELEASE&#39;
  4. id &#39;java&#39;
  5. }
  6. group = &#39;com.socialinteraction&#39;
  7. version = &#39;0.0.1-SNAPSHOT&#39;
  8. sourceCompatibility = &#39;1.8&#39;
  9. configurations {
  10. compileOnly {
  11. extendsFrom annotationProcessor
  12. }
  13. }
  14. repositories {
  15. mavenCentral()
  16. }
  17. sourceSets {
  18. integrationTest {
  19. java {
  20. compileClasspath += main.output + test.output
  21. runtimeClasspath += main.output + test.output
  22. srcDir file(&#39;src/integration-test/java&#39;)
  23. }
  24. resources.srcDir file(&#39;src/integration-test/resources&#39;)
  25. }
  26. }
  27. configurations {
  28. integrationTestCompile.extendsFrom testCompile
  29. integrationTestRuntime.extendsFrom testRuntime
  30. integrationTestImplementation.extendsFrom testImplementation
  31. }
  32. dependencies {
  33. implementation &#39;org.springframework.boot:spring-boot-starter-data-mongodb&#39;
  34. implementation &#39;org.springframework.boot:spring-boot-starter-web&#39;
  35. compileOnly &#39;org.projectlombok:lombok&#39;
  36. annotationProcessor &#39;org.projectlombok:lombok&#39;
  37. testImplementation(&#39;org.springframework.boot:spring-boot-starter-test&#39;) {
  38. exclude group: &#39;org.junit.vintage&#39;, module: &#39;junit-vintage-engine&#39;
  39. }
  40. testImplementation(&#39;org.junit.jupiter:junit-jupiter-api:5.4.2&#39;)
  41. testRuntime(&#39;org.junit.jupiter:junit-jupiter-engine:5.4.2&#39;)
  42. // testCompile &quot;org.powermock:powermock-module-junit4:1.6.4&quot;
  43. // testCompile &#39;org.mockito:mockito-core:2.7.22&#39;
  44. implementation &#39;org.jetbrains:annotations:15.0&#39;
  45. implementation &#39;junit:junit:4.12&#39;
  46. implementation &#39;org.testng:testng:6.9.6&#39;
  47. implementation &#39;junit:junit:4.12&#39;
  48. implementation &#39;junit:junit:4.12&#39;
  49. implementation &#39;org.testng:testng:6.9.6&#39;
  50. }
  51. test {
  52. useJUnitPlatform()
  53. }
  54. task integrationTest(type: Test) {
  55. testClassesDirs = sourceSets.integrationTest.output.classesDirs
  56. classpath = sourceSets.integrationTest.runtimeClasspath
  57. }
  58. check.dependsOn integrationTest
  59. integrationTest.mustRunAfter test

I essentially made sure the integration test dependencies were inherited from unit tests, there was a new intergrationTest type and that this was ran in the build after unit tests.

Side question - not sure testRuntime, testImplementation and other dependencies type are all needed/right, so a good link to explain these would be great if someone has one?

Thanks so much for your help!

答案1

得分: 2

尝试在任务描述中添加useJUnitPlatform()

  1. task integrationTest(type: Test) {
  2. useJUnitPlatform()
  3. testClassesDirs = sourceSets.integrationTest.output.classesDirs
  4. classpath = sourceSets.integrationTest.runtimeClasspath
  5. }
  6. 它对我有效。
英文:

try to add useJUnitPlatform() inside your task desc:

  1. task integrationTest(type: Test) {
  2. useJUnitPlatform()
  3. testClassesDirs = sourceSets.integrationTest.output.classesDirs
  4. classpath = sourceSets.integrationTest.runtimeClasspath
  5. }

It works for me.

huangapple
  • 本文由 发表于 2020年4月4日 01:24:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/61017234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定