Best Spectrum code snippet using junit.spring.SomeComponent
Source:AutowiredHoverProviderTest.java
...98 "void createRuntimeBean(String info);",99 "}"100 ).collect(Collectors.joining("\n"))101 );102 p.createType("com.example.SomeComponent",103 Stream.of("package com.example;",104 "",105 "import org.springframework.context.annotation.Bean;",106 "",107// "@Component",108 "public class SomeComponent {",109 "",110 "@Bean",111 "public RuntimeBeanFactory getBeanFactory() {",112 "\treturn new RuntimeBeanFactory() {",113 "\t\tpublic void createRuntimeBean(String info){}",114 "\t};",115 "}",116 "",117 "}"118 ).collect(Collectors.joining("\n"))119 );120 p.createType("com.example.FooImplementation", FOO_IMPL_CONTENTS);121 };122 @Autowired123 private BootLanguageServerHarness harness;124 private ProjectsHarness projects = ProjectsHarness.INSTANCE;125 @Autowired126 private MockRunningAppProvider mockAppProvider;127 @Before128 public void setup() throws Exception {129 MavenJavaProject jp = projects.mavenProject("empty-boot-15-web-app", FOO_INTERFACE);130 assertTrue(jp.getIndex().findType("com.example.Foo").exists());131 harness.useProject(jp);132 harness.intialize(null);133 }134 @Test135 public void javaxInjectAnnotationHover() throws Exception {136 LiveBeansModel beans = LiveBeansModel.builder()137 .add(LiveBean.builder()138 .id("autowiredClass")139 .type("com.example.AutowiredClass")140 .dependencies("dependencyA")141 .build()142 )143 .add(LiveBean.builder()144 .id("dependencyA")145 .type("com.example.DependencyA")146 .fileResource(harness.getOutputFolder() + Paths.get("/com/example/DependencyA.class").toString())147 .build()148 )149 .build();150 mockAppProvider.builder()151 .isSpringBootApp(true)152 .processId("111")153 .processName("the-app")154 .beans(beans)155 .build();156 Editor editor = harness.newEditor(LanguageId.JAVA,157 "package com.example;\n" +158 "\n" +159 "import javax.inject.Inject;\n" +160 "import org.springframework.stereotype.Component;\n" +161 "\n" +162 "@Component\n" +163 "public class AutowiredClass {\n" +164 "\n" +165 " @Inject\n" +166 " private DependencyA depA;\n" +167 "}\n"168 );169 editor.assertHighlights("@Component", "@Inject");170 editor.assertTrimmedHover("@Inject",171 "**← `DependencyA`**\n" +172 "- Bean: `dependencyA` \n" +173 " Type: `com.example.DependencyA` \n" +174 " Resource: `" + Paths.get("com/example/DependencyA.class") + "`\n" +175 " \n" +176 "Bean id: `autowiredClass` \n" +177 "Process [PID=111, name=`the-app`]"178 );179 }180 @Test181 public void componentWithAutomaticallyWiredConstructorInjections() throws Exception {182 LiveBeansModel beans = LiveBeansModel.builder()183 .add(LiveBean.builder()184 .id("autowiredClass")185 .type("com.example.AutowiredClass")186 .dependencies("dependencyA", "dependencyB")187 .build()188 )189 .add(LiveBean.builder()190 .id("dependencyA")191 .type("com.example.DependencyA")192 .fileResource(harness.getOutputFolder() + Paths.get("/com/example/DependencyA.class").toString())193 .build()194 )195 .add(LiveBean.builder()196 .id("dependencyB")197 .type("com.example.DependencyB")198 .classpathResource("com/example/DependencyB.class")199 .build()200 )201 .build();202 mockAppProvider.builder()203 .isSpringBootApp(true)204 .processId("111")205 .processName("the-app")206 .beans(beans)207 .build();208 Editor editor = harness.newEditor(LanguageId.JAVA,209 "package com.example;\n" +210 "\n" +211 "import org.springframework.beans.factory.annotation.Autowired;\n" +212 "import org.springframework.stereotype.Component;\n" +213 "\n" +214 "@Component\n" +215 "public class AutowiredClass {\n" +216 "\n" +217 " @Autowired\n" +218 " public AutowiredClass(DependencyA depA, DependencyB depB) {\n" +219 " }\n" +220 "}\n"221 );222 editor.assertHighlights("@Component", "@Autowired");223 editor.assertTrimmedHover("@Autowired",224 "**← `DependencyA` `DependencyB`**\n" +225 "- Bean: `dependencyA` \n" +226 " Type: `com.example.DependencyA` \n" +227 " Resource: `" + Paths.get("com/example/DependencyA.class") + "`\n" +228 "- Bean: `dependencyB` \n" +229 " Type: `com.example.DependencyB` \n" +230 " Resource: `com/example/DependencyB.class`\n" +231 " \n" +232 "Bean id: `autowiredClass` \n" +233 "Process [PID=111, name=`the-app`]\n"234 );235 }236 @Test237 public void noHoversWhenNoRunningApps() throws Exception {238 Editor editor = harness.newEditor(LanguageId.JAVA,239 "package com.example;\n" +240 "\n" +241 "import org.springframework.beans.factory.annotation.Autowired;\n" +242 "import org.springframework.stereotype.Component;\n" +243 "\n" +244 "@Component\n" +245 "public class AutowiredClass {\n" +246 "\n" +247 " @Autowired\n" +248 " public AutowiredClass(DependencyA depA, DependencyB depB) {\n" +249 " }\n" +250 "}\n"251 );252 editor.assertHighlights(/*NONE*/);253 editor.assertNoHover("@Autowired");254 }255 @Test256 public void noHoversWhenRunningAppDoesntHaveTheComponent() throws Exception {257 LiveBeansModel beans = LiveBeansModel.builder()258 .add(LiveBean.builder()259 .id("whateverBean")260 .type("com.example.UnrelatedComponent")261 .build()262 )263 .add(LiveBean.builder()264 .id("myController")265 .type("com.example.UnrelatedComponent")266 .dependencies("whateverBean")267 .build()268 )269 .build();270 mockAppProvider.builder()271 .isSpringBootApp(true)272 .processId("111")273 .processName("unrelated-app")274 .beans(beans)275 .build();276 Editor editor = harness.newEditor(LanguageId.JAVA,277 "package com.example;\n" +278 "\n" +279 "import org.springframework.beans.factory.annotation.Autowired;\n" +280 "import org.springframework.stereotype.Component;\n" +281 "\n" +282 "@Component\n" +283 "public class AutowiredClass {\n" +284 "\n" +285 " @Autowired\n" +286 " public AutowiredClass(DependencyA depA, DependencyB depB) {\n" +287 " }\n" +288 "}\n"289 );290 editor.assertHighlights(/*MONE*/);291 editor.assertNoHover("@Autowired");292 }293 @Test294 public void noHoversWhenRunningAppDoesntHaveDependenciesForTheAutowiring() throws Exception {295 LiveBeansModel beans = LiveBeansModel.builder()296 .add(LiveBean.builder()297 .id("autowiredClass")298 .type("com.example.AutowiredClass")299 .build()300 )301 .add(LiveBean.builder()302 .id("dependencyA")303 .type("com.example.DependencyA")304 .build()305 )306 .add(LiveBean.builder()307 .id("dependencyB")308 .type("com.example.DependencyB")309 .build()310 )311 .build();312 mockAppProvider.builder()313 .isSpringBootApp(true)314 .processId("111")315 .processName("the-app")316 .beans(beans)317 .build();318 Editor editor = harness.newEditor(LanguageId.JAVA,319 "package com.example;\n" +320 "\n" +321 "import org.springframework.beans.factory.annotation.Autowired;\n" +322 "import org.springframework.stereotype.Component;\n" +323 "\n" +324 "@Component\n" +325 "public class AutowiredClass {\n" +326 "\n" +327 " @Autowired\n" +328 " public AutowiredClass(DependencyA depA, DependencyB depB) {\n" +329 " }\n" +330 "}\n"331 );332 editor.assertHighlights("@Component");333 editor.assertNoHover("@Autowired");334 }335 @Test public void bug_152553935() throws Exception {336 //https://www.pivotaltracker.com/story/show/152553935337 LiveBeansModel beans = LiveBeansModel.builder()338 .add(LiveBean.builder()339 .id("defaultFoo")340 .type("com.example.FooImplementation")341 .dependencies("superBean", "scheduler")342 .build()343 )344 .add(LiveBean.builder()345 .id("superBean")346 .type("com.example.FooImplementation")347 .build()348 )349 .add(LiveBean.builder()350 .id("scheduler")351 .type("org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler")352 .build()353 )354 .build();355 mockAppProvider.builder()356 .isSpringBootApp(true)357 .processId("111")358 .processName("the-app")359 .beans(beans)360 .build();361 Editor editor = harness.newEditor(LanguageId.JAVA, FOO_IMPL_CONTENTS);362 editor.assertHighlights("@Component", "@Autowired", "@Autowired");363 editor.assertHoverContains("@Autowired", 1,364 "**← `FooImplementation`**\n" +365 "- Bean: `superBean` \n" +366 " Type: `com.example.FooImplementation`");367 editor.assertHoverContains("@Autowired", 2,368 "**← `ThreadPoolTaskScheduler`**\n" +369 "- Bean: `scheduler` \n" +370 " Type: `org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler`");371 }372 @Test public void bug_152621242_autowired_constructor_on_a_controller() throws Exception {373 //https://www.pivotaltracker.com/story/show/152621242374 LiveBeansModel beans = LiveBeansModel.builder()375 .add(LiveBean.builder()376 .id("myController")377 .type("com.example.MyController")378 .dependencies("restTemplate")379 .build()380 )381 .add(LiveBean.builder()382 .id("restTemplate")383 .type("org.springframework.web.client.RestTemplate")384 .build()385 )386 .build();387 mockAppProvider.builder()388 .isSpringBootApp(true)389 .processId("111")390 .processName("the-app")391 .beans(beans)392 .build();393 Editor editor = harness.newEditor(LanguageId.JAVA,394 "package com.example;\n" +395 "\n" +396 "import org.springframework.beans.factory.annotation.Autowired;\n" +397 "import org.springframework.stereotype.Component;\n" +398 "import org.springframework.stereotype.Controller;\n" +399 "import org.springframework.web.client.RestTemplate;\n" +400 "\n" +401 "@Controller\n" +402 "public class MyController {\n" +403 "\n" +404 " private RestTemplate restClient;\n" +405 "\n" +406 " @Autowired\n" +407 " public MyController(RestTemplate restClient) {\n" +408 " this.restClient = restClient;\n" +409 " }\n" +410 " \n" +411 "}"412 );413 editor.assertHighlights("@Controller", "@Autowired");414 editor.assertHoverContains("@Autowired",415 "**← `RestTemplate`**\n" +416 "- Bean: `restTemplate` \n" +417 " Type: `org.springframework.web.client.RestTemplate`"418 );419 editor.assertTrimmedHover("@Controller",420 "**← `RestTemplate`**\n" +421 "- Bean: `restTemplate` \n" +422 " Type: `org.springframework.web.client.RestTemplate`\n" +423 " \n" +424 "Bean id: `myController` \n" +425 "Process [PID=111, name=`the-app`]\n"426 );427 }428 @Test429 public void implicitAutowiringSingleConstructor() throws Exception {430 LiveBeansModel beans = LiveBeansModel.builder()431 .add(LiveBean.builder()432 .id("someComponent")433 .type("com.example.SomeComponent")434 .dependencies("dependencyA", "dependencyB")435 .build()436 )437 .add(LiveBean.builder()438 .id("dependencyA")439 .type("com.example.DependencyA")440 .build()441 )442 .add(LiveBean.builder()443 .id("dependencyB")444 .type("com.example.DependencyB")445 .build()446 )447 .build();448 mockAppProvider.builder()449 .isSpringBootApp(true)450 .processId("111")451 .processName("the-app")452 .beans(beans)453 .build();454 Editor editor = harness.newEditor(LanguageId.JAVA,455 "package com.example;\n" +456 "\n" +457 "import org.springframework.stereotype.Component;\n" +458 "\n" +459 "@Component\n" +460 "public class SomeComponent {\n" +461 "\n" +462 " private DepedencyA depA;\n" +463 " private DepedencyB depB;\n" +464 "\n" +465 " public SomeComponent(DependencyA depA, DependencyB depB) {\n" +466 " this.depA = depA;\n" +467 " this.depB = depB;\n" +468 " }\n" +469 "}\n"470 );471 editor.assertHighlights("@Component", "SomeComponent", "depA", "depB");472 editor.assertTrimmedHover("SomeComponent", 2,473 "**← `DependencyA` `DependencyB`**\n" +474 "- Bean: `dependencyA` \n" +475 " Type: `com.example.DependencyA`\n" +476 "- Bean: `dependencyB` \n" +477 " Type: `com.example.DependencyB`\n" +478 " \n" +479 "Bean id: `someComponent` \n" +480 "Process [PID=111, name=`the-app`]\n"481 );482 editor.assertTrimmedHover("depA", 2,483 "**← `DependencyA`**\n" +484 "- Bean: `dependencyA` \n" +485 " Type: `com.example.DependencyA`\n" +486 " \n" +487 "Bean id: `someComponent` \n" +488 "Process [PID=111, name=`the-app`]\n"489 );490 editor.assertTrimmedHover("depB", 2,491 "**← `DependencyB`**\n" +492 "- Bean: `dependencyB` \n" +493 " Type: `com.example.DependencyB`\n" +494 " \n" +495 "Bean id: `someComponent` \n" +496 "Process [PID=111, name=`the-app`]\n"497 );498 }499 @Test500 public void noImplicitAutowiringForConstructorFromNonBean() throws Exception {501 LiveBeansModel beans = LiveBeansModel.builder()502 .add(LiveBean.builder()503 .id("someOtherComponent")504 .type("com.example.SomeOtherComponent")505 .dependencies("dependencyA", "dependencyB")506 .build()507 )508 .add(LiveBean.builder()509 .id("dependencyA")510 .type("com.example.DependencyA")511 .build()512 )513 .add(LiveBean.builder()514 .id("dependencyB")515 .type("com.example.DependencyB")516 .build()517 )518 .build();519 mockAppProvider.builder()520 .isSpringBootApp(true)521 .processId("111")522 .processName("the-app")523 .beans(beans)524 .build();525 Editor editor = harness.newEditor(LanguageId.JAVA,526 "package com.example;\n" +527 "\n" +528 "public class SomeComponent {\n" +529 "\n" +530 " private DepedencyA depA;\n" +531 " private DepedencyB depB;\n" +532 "\n" +533 " public SomeComponent(DependencyA depA, DependencyB depB) {\n" +534 " this.depA = depA;\n" +535 " this.depB = depB;\n" +536 " }\n" +537 "}\n"538 );539 editor.assertHighlights();540 for (int i = 1; i < 2; i++) {541 editor.assertNoHover("SomeComponent", i);542 }543 }544 @Test545 public void noImplicitAutowiringForMultipleConstructors() throws Exception {546 LiveBeansModel beans = LiveBeansModel.builder()547 .add(LiveBean.builder()548 .id("someComponent")549 .type("com.example.SomeComponent")550 .dependencies("dependencyA", "dependencyB")551 .build()552 )553 .add(LiveBean.builder()554 .id("dependencyA")555 .type("com.example.DependencyA")556 .build()557 )558 .add(LiveBean.builder()559 .id("dependencyB")560 .type("com.example.DependencyB")561 .build()562 )563 .build();564 mockAppProvider.builder()565 .isSpringBootApp(true)566 .processId("111")567 .processName("the-app")568 .beans(beans)569 .build();570 Editor editor = harness.newEditor(LanguageId.JAVA,571 "package com.example;\n" +572 "\n" +573 "import org.springframework.stereotype.Component;\n" +574 "\n" +575 "@Component\n" +576 "public class SomeComponent {\n" +577 "\n" +578 " private DepedencyA depA;\n" +579 " private DepedencyB depB;\n" +580 "\n" +581 " public SomeComponent() {\n" +582 " }\n" +583 "\n" +584 " public SomeComponent(DependencyA depA, DependencyB depB) {\n" +585 " this.depA = depA;\n" +586 " this.depB = depB;\n" +587 " }\n" +588 "}\n"589 );590 editor.assertHighlights("@Component");591 for (int i = 1; i < 3; i++) {592 editor.assertNoHover("SomeComponent", i);593 }594 }595 @Test596 public void unableToMatchWiredBean() throws Exception {597 LiveBeansModel beans = LiveBeansModel.builder()598 .add(LiveBean.builder()599 .id("someComponent")600 .type("com.example.SomeComponent")601 .dependencies("dependencyA", "dependencyB")602 .build()603 )604 .add(LiveBean.builder()605 .id("dependencyA")606 .type("com.example.DependencyA")607 .build()608 )609 .add(LiveBean.builder()610 .id("dependencyB")611 .type("com.example.DependencyB")612 .build()613 )614 .build();615 mockAppProvider.builder()616 .isSpringBootApp(true)617 .processId("111")618 .processName("the-app")619 .beans(beans)620 .build();621 Editor editor = harness.newEditor(LanguageId.JAVA,622 "package com.example;\n" +623 "\n" +624 "import org.springframework.beans.factory.annotation.Autowired;\n" +625 "import org.springframework.stereotype.Component;\n" +626 "\n" +627 "@Component\n" +628 "public class SomeComponent {\n" +629 "\n" +630 " @Autowired\n" +631 " private IDependency dep;\n" +632 "\n" +633 " public SomeComponent() {\n" +634 " }\n" +635 "\n" +636 "}\n"637 );638 editor.assertHighlights("@Component", "@Autowired");639 editor.assertTrimmedHover("@Autowired", 1,640 "**← UNKNOWN**\n" +641 "- (Cannot find precise information for the bean)\n" +642 " \n" +643 "Bean id: `someComponent` \n" +644 "Process [PID=111, name=`the-app`]\n"645 );646 }647 @Test648 public void qualifierParameterWiredBean() throws Exception {649 LiveBeansModel beans = LiveBeansModel.builder()650 .add(LiveBean.builder()651 .id("someComponent")652 .type("com.example.SomeComponent")653 .dependencies("dependencyA", "dependencyB")654 .build()655 )656 .add(LiveBean.builder()657 .id("dependencyA")658 .type("com.example.DependencyA")659 .build()660 )661 .add(LiveBean.builder()662 .id("dependencyB")663 .type("com.example.DependencyB")664 .build()665 )666 .build();667 mockAppProvider.builder()668 .isSpringBootApp(true)669 .processId("111")670 .processName("the-app")671 .beans(beans)672 .build();673 Editor editor = harness.newEditor(LanguageId.JAVA,674 "package com.example;\n" +675 "\n" +676 "import org.springframework.beans.factory.annotation.Qualifier;\n" +677 "import org.springframework.stereotype.Component;\n" +678 "\n" +679 "@Component\n" +680 "public class SomeComponent {\n" +681 "\n" +682 " private IDependency a;\n" +683 " private IDependency b;\n" +684 "\n" +685 " public SomeComponent(@Qualifier(\"dependencyA\") IDependency depA, @Qualifier(\"dependencyB\") IDependency depB) {\n" +686 " this.a = a;\n" +687 " this.b = b;\n" +688 " }\n" +689 "\n" +690 "}\n"691 );692 editor.assertHighlights("@Component", "SomeComponent", "depA", "depB");693 editor.assertTrimmedHover("SomeComponent", 2,694 "**← `DependencyA` `DependencyB`**\n" +695 "- Bean: `dependencyA` \n" +696 " Type: `com.example.DependencyA`\n" +697 "- Bean: `dependencyB` \n" +698 " Type: `com.example.DependencyB`\n" +699 " \n" +700 "Bean id: `someComponent` \n" +701 "Process [PID=111, name=`the-app`]\n"702 );703 editor.assertTrimmedHover("depA",704 "**← `DependencyA`**\n" +705 "- Bean: `dependencyA` \n" +706 " Type: `com.example.DependencyA`\n" +707 " \n" +708 "Bean id: `someComponent` \n" +709 "Process [PID=111, name=`the-app`]\n"710 );711 editor.assertTrimmedHover("depB",712 "**← `DependencyB`**\n" +713 "- Bean: `dependencyB` \n" +714 " Type: `com.example.DependencyB`\n" +715 " \n" +716 "Bean id: `someComponent` \n" +717 "Process [PID=111, name=`the-app`]\n"718 );719 }720 @Test721 public void qualifierFieldWiredBean() throws Exception {722 LiveBeansModel beans = LiveBeansModel.builder()723 .add(LiveBean.builder()724 .id("someComponent")725 .type("com.example.SomeComponent")726 .dependencies("dependencyA")727 .build()728 )729 .add(LiveBean.builder()730 .id("dependencyA")731 .type("com.example.DependencyA")732 .build()733 )734 .build();735 mockAppProvider.builder()736 .isSpringBootApp(true)737 .processId("111")738 .processName("the-app")739 .beans(beans)740 .build();741 Editor editor = harness.newEditor(LanguageId.JAVA,742 "package com.example;\n" +743 "\n" +744 "import org.springframework.beans.factory.annotation.Autowired;\n" +745 "import org.springframework.beans.factory.annotation.Qualifier;\n" +746 "import org.springframework.stereotype.Component;\n" +747 "\n" +748 "@Component\n" +749 "public class SomeComponent {\n" +750 "\n" +751 " @Autowired\n" +752 " @Qualifier(\"dependencyA\")\n" +753 " private IDependency a;\n" +754 "\n" +755 " public SomeComponent() {\n" +756 " }\n" +757 "\n" +758 "}\n"759 );760 editor.assertHighlights("@Component", "@Autowired");761 editor.assertTrimmedHover("@Autowired", 1,762 "**← `DependencyA`**\n" +763 "- Bean: `dependencyA` \n" +764 " Type: `com.example.DependencyA`\n" +765 " \n" +766 "Bean id: `someComponent` \n" +767 "Process [PID=111, name=`the-app`]\n"768 );769 }770 @Test771 public void qualifierNotFoundWiredBean() throws Exception {772 LiveBeansModel beans = LiveBeansModel.builder()773 .add(LiveBean.builder()774 .id("someComponent")775 .type("com.example.SomeComponent")776 .dependencies("dependencyA")777 .build()778 )779 .add(LiveBean.builder()780 .id("dependencyA")781 .type("com.example.DependencyA")782 .build()783 )784 .build();785 mockAppProvider.builder()786 .isSpringBootApp(true)787 .processId("111")788 .processName("the-app")789 .beans(beans)790 .build();791 Editor editor = harness.newEditor(LanguageId.JAVA,792 "package com.example;\n" +793 "\n" +794 "import org.springframework.beans.factory.annotation.Autowired;\n" +795 "import org.springframework.beans.factory.annotation.Qualifier;\n" +796 "import org.springframework.stereotype.Component;\n" +797 "\n" +798 "@Component\n" +799 "public class SomeComponent {\n" +800 "\n" +801 " @Autowired\n" +802 " @Qualifier(\"X\")\n" +803 " private IDependency a;\n" +804 "\n" +805 " public SomeComponent() {\n" +806 " }\n" +807 "\n" +808 "}\n"809 );810 editor.assertHighlights("@Component");811 editor.assertNoHover("@Autowired");812 }813 @Test814 public void qualifierWrongTypeWiredBean() throws Exception {815 LiveBeansModel beans = LiveBeansModel.builder()816 .add(LiveBean.builder()817 .id("someComponent")818 .type("com.example.SomeComponent")819 .dependencies("dependency")820 .build()821 )822 .add(LiveBean.builder()823 .id("dependency")824 .type("com.example.DependencyA")825 .build()826 )827 .build();828 mockAppProvider.builder()829 .isSpringBootApp(true)830 .processId("111")831 .processName("the-app")832 .beans(beans)833 .build();834 Editor editor = harness.newEditor(LanguageId.JAVA,835 "package com.example;\n" +836 "\n" +837 "import org.springframework.beans.factory.annotation.Autowired;\n" +838 "import org.springframework.beans.factory.annotation.Qualifier;\n" +839 "import org.springframework.stereotype.Component;\n" +840 "\n" +841 "@Component\n" +842 "public class SomeComponent {\n" +843 "\n" +844 " @Autowired\n" +845 " @Qualifier(\"dependency\")\n" +846 " private DependencyB a;\n" +847 "\n" +848 " public SomeComponent() {\n" +849 " }\n" +850 "\n" +851 "}\n"852 );853 editor.assertHighlights("@Component");854 editor.assertNoHover("@Autowired");855 }856 @Test857 public void collectionWiredBean() throws Exception {858 LiveBeansModel beans = LiveBeansModel.builder()859 .add(LiveBean.builder()860 .id("someComponent")861 .type("com.example.SomeComponent")862 .dependencies("dependencyA")863 .build()864 )865 .add(LiveBean.builder()866 .id("dependencyA")867 .type("com.example.DependencyA")868 .build()869 )870 .build();871 mockAppProvider.builder()872 .isSpringBootApp(true)873 .processId("111")874 .processName("the-app")875 .beans(beans)876 .build();877 Editor editor = harness.newEditor(LanguageId.JAVA,878 "package com.example;\n" +879 "\n" +880 "import java.util.List;\n" +881 "import org.springframework.beans.factory.annotation.Autowired;\n" +882 "import org.springframework.stereotype.Component;\n" +883 "\n" +884 "@Component\n" +885 "public class SomeComponent {\n" +886 "\n" +887 " @Autowired\n" +888 " private List<DependencyA> a;\n" +889 "\n" +890 " public SomeComponent() {\n" +891 " }\n" +892 "\n" +893 "}\n"894 );895 editor.assertHighlights("@Component", "@Autowired");896 editor.assertTrimmedHover("@Autowired", 1,897 "**← `DependencyA`**\n" +898 "- Bean: `dependencyA` \n" +899 " Type: `com.example.DependencyA`\n" +900 " \n" +901 "Bean id: `someComponent` \n" +902 "Process [PID=111, name=`the-app`]\n"903 );904 }905 @Test906 public void collectionWithQualifierWiredBean() throws Exception {907 LiveBeansModel beans = LiveBeansModel.builder()908 .add(LiveBean.builder()909 .id("someComponent")910 .type("com.example.SomeComponent")911 .dependencies("dependencyA")912 .build()913 )914 .add(LiveBean.builder()915 .id("dependencyA")916 .type("com.example.DependencyA")917 .build()918 )919 .build();920 mockAppProvider.builder()921 .isSpringBootApp(true)922 .processId("111")923 .processName("the-app")924 .beans(beans)925 .build();926 Editor editor = harness.newEditor(LanguageId.JAVA,927 "package com.example;\n" +928 "\n" +929 "import java.util.List;\n" +930 "import org.springframework.beans.factory.annotation.Autowired;\n" +931 "import org.springframework.beans.factory.annotation.Qualifier;\n" +932 "import org.springframework.stereotype.Component;\n" +933 "\n" +934 "@Component\n" +935 "public class SomeComponent {\n" +936 "\n" +937 " @Autowired\n" +938 " @Qualifier(\"dependencyA\")\n" +939 " private List<DependencyA> a;\n" +940 "\n" +941 " public SomeComponent() {\n" +942 " }\n" +943 "\n" +944 "}\n"945 );946 editor.assertHighlights("@Component", "@Autowired");947 editor.assertTrimmedHover("@Autowired", 1,948 "**← `DependencyA`**\n" +949 "- Bean: `dependencyA` \n" +950 " Type: `com.example.DependencyA`\n" +951 " \n" +952 "Bean id: `someComponent` \n" +953 "Process [PID=111, name=`the-app`]\n"954 );955 }956 @Test957 public void collectionOfCollectionsWiredBeanNotFound() throws Exception {958 LiveBeansModel beans = LiveBeansModel.builder()959 .add(LiveBean.builder()960 .id("someComponent")961 .type("com.example.SomeComponent")962 .dependencies("dependencyA")963 .build()964 )965 .add(LiveBean.builder()966 .id("dependencyA")967 .type("com.example.DependencyA")968 .build()969 )970 .build();971 mockAppProvider.builder()972 .isSpringBootApp(true)973 .processId("111")974 .processName("the-app")975 .beans(beans)976 .build();977 Editor editor = harness.newEditor(LanguageId.JAVA,978 "package com.example;\n" +979 "\n" +980 "import java.util.List;\n" +981 "import org.springframework.beans.factory.annotation.Autowired;\n" +982 "import org.springframework.beans.factory.annotation.Qualifier;\n" +983 "import org.springframework.stereotype.Component;\n" +984 "\n" +985 "@Component\n" +986 "public class SomeComponent {\n" +987 "\n" +988 " @Autowired\n" +989 " @Qualifier(\"dependencyA\")\n" +990 " private List<List<DependencyA>> a;\n" +991 "\n" +992 " public SomeComponent() {\n" +993 " }\n" +994 "\n" +995 "}\n"996 );997 editor.assertHighlights("@Component");998 editor.assertNoHover("@Autowired");999 }1000 @Test1001 public void collectionRawWithQualifierWiredBean() throws Exception {1002 LiveBeansModel beans = LiveBeansModel.builder()1003 .add(LiveBean.builder()1004 .id("someComponent")1005 .type("com.example.SomeComponent")1006 .dependencies("dependencyA")1007 .build()1008 )1009 .add(LiveBean.builder()1010 .id("dependencyA")1011 .type("com.example.DependencyA")1012 .build()1013 )1014 .build();1015 mockAppProvider.builder()1016 .isSpringBootApp(true)1017 .processId("111")1018 .processName("the-app")1019 .beans(beans)1020 .build();1021 Editor editor = harness.newEditor(LanguageId.JAVA,1022 "package com.example;\n" +1023 "\n" +1024 "import java.util.List;\n" +1025 "import org.springframework.beans.factory.annotation.Autowired;\n" +1026 "import org.springframework.beans.factory.annotation.Qualifier;\n" +1027 "import org.springframework.stereotype.Component;\n" +1028 "\n" +1029 "@Component\n" +1030 "public class SomeComponent {\n" +1031 "\n" +1032 " @Autowired\n" +1033 " @Qualifier(\"dependencyA\")\n" +1034 " private List a;\n" +1035 "\n" +1036 " public SomeComponent() {\n" +1037 " }\n" +1038 "\n" +1039 "}\n"1040 );1041 editor.assertHighlights("@Component");1042 editor.assertNoHover("@Autowired");1043 }1044 @Test1045 public void arrayWithQualifierWiredBean() throws Exception {1046 LiveBeansModel beans = LiveBeansModel.builder()1047 .add(LiveBean.builder()1048 .id("someComponent")1049 .type("com.example.SomeComponent")1050 .dependencies("dependencyA")1051 .build()1052 )1053 .add(LiveBean.builder()1054 .id("dependencyA")1055 .type("com.example.DependencyA")1056 .build()1057 )1058 .build();1059 mockAppProvider.builder()1060 .isSpringBootApp(true)1061 .processId("111")1062 .processName("the-app")1063 .beans(beans)1064 .build();1065 Editor editor = harness.newEditor(LanguageId.JAVA,1066 "package com.example;\n" +1067 "\n" +1068 "import org.springframework.beans.factory.annotation.Autowired;\n" +1069 "import org.springframework.beans.factory.annotation.Qualifier;\n" +1070 "import org.springframework.stereotype.Component;\n" +1071 "\n" +1072 "@Component\n" +1073 "public class SomeComponent {\n" +1074 "\n" +1075 " @Autowired\n" +1076 " @Qualifier(\"dependencyA\")\n" +1077 " private DependencyA[] a;\n" +1078 "\n" +1079 " public SomeComponent() {\n" +1080 " }\n" +1081 "\n" +1082 "}\n"1083 );1084 editor.assertHighlights("@Component", "@Autowired");1085 editor.assertTrimmedHover("@Autowired", 1,1086 "**← `DependencyA`**\n" +1087 "- Bean: `dependencyA` \n" +1088 " Type: `com.example.DependencyA`\n" +1089 " \n" +1090 "Bean id: `someComponent` \n" +1091 "Process [PID=111, name=`the-app`]\n"1092 );1093 }1094 @Test1095 public void arrayWiredBean() throws Exception {1096 LiveBeansModel beans = LiveBeansModel.builder()1097 .add(LiveBean.builder()1098 .id("someComponent")1099 .type("com.example.SomeComponent")1100 .dependencies("dependencyA")1101 .build()1102 )1103 .add(LiveBean.builder()1104 .id("dependencyA")1105 .type("com.example.DependencyA")1106 .build()1107 )1108 .build();1109 mockAppProvider.builder()1110 .isSpringBootApp(true)1111 .processId("111")1112 .processName("the-app")1113 .beans(beans)1114 .build();1115 Editor editor = harness.newEditor(LanguageId.JAVA,1116 "package com.example;\n" +1117 "\n" +1118 "import org.springframework.beans.factory.annotation.Autowired;\n" +1119 "import org.springframework.stereotype.Component;\n" +1120 "\n" +1121 "@Component\n" +1122 "public class SomeComponent {\n" +1123 "\n" +1124 " @Autowired\n" +1125 " private IDependency[] a;\n" +1126 "\n" +1127 " public SomeComponent() {\n" +1128 " }\n" +1129 "\n" +1130 "}\n"1131 );1132 editor.assertHighlights("@Component", "@Autowired");1133 editor.assertTrimmedHover("@Autowired", 1,1134 "**← `DependencyA`**\n" +1135 "- Bean: `dependencyA` \n" +1136 " Type: `com.example.DependencyA`\n" +1137 " \n" +1138 "Bean id: `someComponent` \n" +1139 "Process [PID=111, name=`the-app`]\n"1140 );1141 }1142 @Test1143 public void arrayWiredBeans() throws Exception {1144 LiveBeansModel beans = LiveBeansModel.builder()1145 .add(LiveBean.builder()1146 .id("someComponent")1147 .type("com.example.SomeComponent")1148 .dependencies("dependencyA", "dependencyB")1149 .build()1150 )1151 .add(LiveBean.builder()1152 .id("dependencyA")1153 .type("com.example.DependencyA")1154 .build()1155 )1156 .add(LiveBean.builder()1157 .id("dependencyB")1158 .type("com.example.DependencyB")1159 .build()1160 )1161 .build();1162 mockAppProvider.builder()1163 .isSpringBootApp(true)1164 .processId("111")1165 .processName("the-app")1166 .beans(beans)1167 .build();1168 Editor editor = harness.newEditor(LanguageId.JAVA,1169 "package com.example;\n" +1170 "\n" +1171 "import org.springframework.beans.factory.annotation.Autowired;\n" +1172 "import org.springframework.stereotype.Component;\n" +1173 "\n" +1174 "@Component\n" +1175 "public class SomeComponent {\n" +1176 "\n" +1177 " @Autowired\n" +1178 " private IDependency[] a;\n" +1179 "\n" +1180 " public SomeComponent() {\n" +1181 " }\n" +1182 "\n" +1183 "}\n"1184 );1185 editor.assertHighlights("@Component", "@Autowired");1186 editor.assertTrimmedHover("@Autowired", 1,1187 "**← `DependencyA` `DependencyB`**\n" +1188 "- Bean: `dependencyA` \n" +1189 " Type: `com.example.DependencyA`\n" +1190 "- Bean: `dependencyB` \n" +1191 " Type: `com.example.DependencyB`\n" +1192 " \n" +1193 "Bean id: `someComponent` \n" +1194 "Process [PID=111, name=`the-app`]\n"1195 );1196 }1197 @Test1198 public void multiDimensionalArrayWiredBean() throws Exception {1199 LiveBeansModel beans = LiveBeansModel.builder()1200 .add(LiveBean.builder()1201 .id("someComponent")1202 .type("com.example.SomeComponent")1203 .dependencies("dependencyA")1204 .build()1205 )1206 .add(LiveBean.builder()1207 .id("dependencyA")1208 .type("com.example.DependencyA")1209 .build()1210 )1211 .build();1212 mockAppProvider.builder()1213 .isSpringBootApp(true)1214 .processId("111")1215 .processName("the-app")1216 .beans(beans)1217 .build();1218 Editor editor = harness.newEditor(LanguageId.JAVA,1219 "package com.example;\n" +1220 "\n" +1221 "import org.springframework.beans.factory.annotation.Autowired;\n" +1222 "import org.springframework.stereotype.Component;\n" +1223 "\n" +1224 "@Component\n" +1225 "public class SomeComponent {\n" +1226 "\n" +1227 " @Autowired\n" +1228 " private IDependency[][] a;\n" +1229 "\n" +1230 " public SomeComponent() {\n" +1231 " }\n" +1232 "\n" +1233 "}\n"1234 );1235 editor.assertHighlights("@Component");1236 editor.assertNoHover("@Autowired");1237 }1238 @Test1239 public void anonymousInnerClassBeanWiring() throws Exception {1240 LiveBeansModel beans = LiveBeansModel.builder()1241 .add(LiveBean.builder()1242 .id("anotherComponent")1243 .type("com.example.AnotherComponent")1244 .dependencies("anonymousBeanFactory")1245 .build()1246 )1247 .add(LiveBean.builder()1248 .id("anonymousBeanFactory")1249 .type("com.example.SomeComponent$1")1250 .build()1251 )1252 .build();1253 mockAppProvider.builder()1254 .isSpringBootApp(true)1255 .processId("111")1256 .processName("the-app")1257 .beans(beans)1258 .build();1259 Editor editor = harness.newEditor(LanguageId.JAVA,1260 "package com.example;\n" +1261 "\n" +1262 "import org.springframework.beans.factory.annotation.Autowired;\n" +1263 "import org.springframework.stereotype.Component;\n" +1264 "\n" +1265 "@Component\n" +1266 "public class AnotherComponent {\n" +1267 "\n" +1268 " @Autowired\n" +1269 " RuntimeBeanFactory beanFactory;\n" +1270 "}\n"1271 );1272 editor.assertHighlights("@Component", "@Autowired");1273 editor.assertTrimmedHover("@Autowired", 1,1274 "**← `SomeComponent$1`**\n" +1275 "- Bean: `anonymousBeanFactory` \n" +1276 " Type: `com.example.SomeComponent$1`\n" +1277 " \n" +1278 "Bean id: `anotherComponent` \n" +1279 "Process [PID=111, name=`the-app`]\n"1280 );1281 }1282}...
Source:SomeComponentTest.java
...15@ExtendWith(SpringExtension.class)16@TestPropertySource(properties = "app.scheduling.enable=false")17@SpringBootTest18@ComponentScan(basePackages = "com.stulsoft.poc.pjunit5.spring.boot")19class SomeComponentTest {20 @Autowired21 private SomeComponent someComponent;22 @Test23 void work() {24 someComponent.work();25 }26}...
SomeComponent
Using AI Code Generation
1import junit.spring.SomeComponent;2import junit.spring.SomeComponentImpl;3import junit.spring.SomeService;4import junit.spring.SomeServiceImpl;5import junit.spring.SomeDao;6import junit.spring.SomeDaoImpl;7import junit.spring.SomeController;8import junit.spring.SomeControllerImpl;9import junit.spring.SomeWebApp;10import junit.spring.SomeWebAppImpl;11{12 private SomeComponent someComponent;13 private SomeService someService;14 private SomeDao someDao;15 private SomeController someController;16 public SomeWebAppImpl()17 {18 this.someComponent = new SomeComponentImpl();19 this.someService = new SomeServiceImpl();20 this.someDao = new SomeDaoImpl();21 this.someController = new SomeControllerImpl();22 }23 public void doSomething()24 {25 someComponent.doSomething();26 someService.doSomething();27 someDao.doSomething();28 someController.doSomething();29 }30}31import junit.spring.SomeComponent;32import junit.spring.SomeComponentImpl;33import junit.spring.SomeService;34import junit.spring.SomeServiceImpl;35import junit.spring.SomeDao;36import junit.spring.SomeDaoImpl;37import junit.spring.SomeController;38import junit.spring.SomeControllerImpl;39import junit.spring.SomeWebApp;40import junit.spring.SomeWebAppImpl;41{42 private SomeComponent someComponent;43 private SomeService someService;44 private SomeDao someDao;45 private SomeController someController;46 public SomeWebAppImpl()47 {48 this.someComponent = new SomeComponentImpl();49 this.someService = new SomeServiceImpl();50 this.someDao = new SomeDaoImpl();51 this.someController = new SomeControllerImpl();52 }53 public void doSomething()54 {55 someComponent.doSomething();56 someService.doSomething();57 someDao.doSomething();58 someController.doSomething();59 }60}61import junit.spring.SomeComponent;62import junit.spring.SomeComponentImpl;63import junit.spring.SomeService;64import junit.spring.SomeServiceImpl;65import junit.spring.SomeDao;66import junit.spring.SomeDaoImpl;67import junit.spring.SomeController;68import junit.spring.SomeControllerImpl;69import junit.spring.SomeWebApp;
SomeComponent
Using AI Code Generation
1import junit.spring.SomeComponent;2public class TestClass {3 public static void main(String[] args) {4 SomeComponent component = new SomeComponent();5 component.doSomething();6 }7}8package junit.spring;9public class SomeComponent {10 public void doSomething() {11 System.out.println("Doing something");12 }13}
SomeComponent
Using AI Code Generation
1import junit.spring.SomeComponent;2import junit.framework.TestCase;3public class SomeComponentTests extends TestCase {4 public void testSomeComponent() {5 SomeComponent component = new SomeComponent();6 component.doSomething();7 }8}9import junit.spring.SomeComponent;10import junit.framework.TestCase;11public class SomeComponentTests extends TestCase {12 public void testSomeComponent() {13 SomeComponent component = new SomeComponent();14 component.doSomething();15 }16}17import junit.spring.SomeComponent;18import junit.framework.TestCase;19public class SomeComponentTests extends TestCase {20 public void testSomeComponent() {21 SomeComponent component = new SomeComponent();22 component.doSomething();23 }24}25import junit.spring.SomeComponent;26import junit.framework.TestCase;27public class SomeComponentTests extends TestCase {28 public void testSomeComponent() {29 SomeComponent component = new SomeComponent();30 component.doSomething();31 }32}33import junit.spring.SomeComponent;34import junit.framework.TestCase;35public class SomeComponentTests extends TestCase {36 public void testSomeComponent() {37 SomeComponent component = new SomeComponent();38 component.doSomething();39 }40}41import junit.spring.SomeComponent;42import junit.framework.TestCase;43public class SomeComponentTests extends TestCase {44 public void testSomeComponent() {45 SomeComponent component = new SomeComponent();46 component.doSomething();47 }48}49import junit.spring.SomeComponent;50import junit.framework.TestCase;51public class SomeComponentTests extends TestCase {52 public void testSomeComponent() {53 SomeComponent component = new SomeComponent();54 component.doSomething();55 }56}57import junit.spring.SomeComponent;58import junit.framework.TestCase;59public class SomeComponentTests extends TestCase {60 public void testSomeComponent() {61 SomeComponent component = new SomeComponent();62 component.doSomething();63 }64}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!