下面列出了org.springframework.test.context.junit.jupiter.comics.Dog#org.springframework.test.context.junit.jupiter.comics.Person 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@GetMapping("/person/{id}")
Person getPerson(@PathVariable long id) {
if (id == 42) {
return new Person("Dilbert");
}
return new Person("Wally");
}
@Autowired
SpringJUnitJupiterAutowiredConstructorInjectionTests(ApplicationContext applicationContext, Person dilbert, Dog dog,
@Value("${enigma}") Integer enigma) {
this.applicationContext = applicationContext;
this.dilbert = dilbert;
this.dog = dog;
this.enigma = enigma;
}
TestConstructorAnnotationIntegrationTests(ApplicationContext applicationContext, Person dilbert, Dog dog,
@Value("${enigma}") Integer enigma) {
this.applicationContext = applicationContext;
this.dilbert = dilbert;
this.dog = dog;
this.enigma = enigma;
}
@Test
void genericApplicationContextInjectedIntoMethod(
GenericApplicationContext applicationContext) {
assertNotNull(applicationContext,
"GenericApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
/**
* NOTE: Test code must be compiled with "-g" (debug symbols) or "-parameters" in
* order for the parameter name to be used as the qualifier; otherwise, use
* {@code @Qualifier("wally")}.
*/
@Test
void autowiredParameterWithImplicitQualifierBasedOnParameterName(
@Autowired Person wally) {
assertNotNull(wally, "Wally should have been @Autowired by Spring");
assertEquals("Wally", wally.getName(), "Person's name");
}
SpringJUnitJupiterConstructorInjectionTests(ApplicationContext applicationContext, @Autowired Person dilbert,
@Autowired Dog dog, @Value("${enigma}") Integer enigma, TestInfo testInfo) {
this.applicationContext = applicationContext;
this.dilbert = dilbert;
this.dog = dog;
this.enigma = enigma;
this.testInfo = testInfo;
}
@GetMapping("/person/{id}")
Person getPerson(@PathVariable long id) {
if (id == 42) {
return new Person("Dilbert");
}
return new Person("Wally");
}
@Autowired
SpringJUnitJupiterAutowiredConstructorInjectionTests(ApplicationContext applicationContext, Person dilbert, Dog dog,
@Value("${enigma}") Integer enigma) {
this.applicationContext = applicationContext;
this.dilbert = dilbert;
this.dog = dog;
this.enigma = enigma;
}
@Test
void genericApplicationContextInjectedIntoMethod(
GenericApplicationContext applicationContext) {
assertNotNull(applicationContext,
"GenericApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
/**
* NOTE: Test code must be compiled with "-g" (debug symbols) or "-parameters" in
* order for the parameter name to be used as the qualifier; otherwise, use
* {@code @Qualifier("wally")}.
*/
@Test
void autowiredParameterWithImplicitQualifierBasedOnParameterName(
@Autowired Person wally) {
assertNotNull(wally, "Wally should have been @Autowired by Spring");
assertEquals("Wally", wally.getName(), "Person's name");
}
SpringJUnitJupiterConstructorInjectionTests(ApplicationContext applicationContext, @Autowired Person dilbert,
@Autowired Dog dog, @Value("${enigma}") Integer enigma, TestInfo testInfo) {
this.applicationContext = applicationContext;
this.dilbert = dilbert;
this.dog = dog;
this.enigma = enigma;
this.testInfo = testInfo;
}
@GetMapping("/person/{id}")
Person getPerson(@PathVariable long id) {
if (id == 42) {
return new Person("Dilbert");
}
return new Person("Wally");
}
@Autowired
SpringJUnit5AutowiredConstructorInjectionTests(ApplicationContext applicationContext, Person dilbert, Dog dog,
@Value("${enigma}") Integer enigma) {
this.applicationContext = applicationContext;
this.dilbert = dilbert;
this.dog = dog;
this.enigma = enigma;
}
SpringJUnit5ConstructorInjectionTests(ApplicationContext applicationContext, @Autowired Person dilbert,
@Autowired Dog dog, @Value("${enigma}") Integer enigma, TestInfo testInfo) {
this.applicationContext = applicationContext;
this.dilbert = dilbert;
this.dog = dog;
this.enigma = enigma;
this.testInfo = testInfo;
}
@Bean
Person dilbert() {
return new Person("Dilbert");
}
@Bean
Person wally() {
return new Person("Wally");
}
@Test
void applicationContextInjectedIntoMethod(ApplicationContext applicationContext) {
assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
@Test
void genericApplicationContextInjectedIntoMethod(GenericApplicationContext applicationContext) {
assertNotNull(applicationContext, "GenericApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
@Test
void autowiredParameterWithExplicitQualifier(@Qualifier("wally") Person person) {
assertNotNull(person, "Wally should have been @Autowired by Spring");
assertEquals("Wally", person.getName(), "Person's name");
}
/**
* NOTE: Test code must be compiled with "-g" (debug symbols) or "-parameters" in order
* for the parameter name to be used as the qualifier; otherwise, use
* {@code @Qualifier("wally")}.
*/
@Test
void autowiredParameterWithImplicitQualifierBasedOnParameterName(@Autowired Person wally) {
assertNotNull(wally, "Wally should have been @Autowired by Spring");
assertEquals("Wally", wally.getName(), "Person's name");
}
@Test
void autowiredParameterOfList(@Autowired List<Person> peopleParam) {
assertNotNull(peopleParam, "list of people should have been @Autowired by Spring");
assertEquals(2, peopleParam.size(), "Number of people in context");
}
@Test
@DisplayName("ApplicationContext injected into method")
void applicationContextInjected(ApplicationContext applicationContext) {
assertNotNull(applicationContext, "ApplicationContext should have been injected into method by Spring");
assertEquals(dilbert, applicationContext.getBean("dilbert", Person.class));
}
@Test
void applicationContextInjected() {
assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
@Test
void applicationContextInjected() {
assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
@Test
void applicationContextInjectedIntoMethod(ApplicationContext applicationContext) {
assertNotNull(applicationContext,
"ApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
@Test
void autowiredParameterWithExplicitQualifier(@Qualifier("wally") Person person) {
assertNotNull(person, "Wally should have been @Autowired by Spring");
assertEquals("Wally", person.getName(), "Person's name");
}
@Test
void autowiredParameterOfList(@Autowired List<Person> peopleParam) {
assertNotNull(peopleParam,
"list of people should have been @Autowired by Spring");
assertEquals(2, peopleParam.size(), "Number of people in context");
}
@Test
void applicationContextInjected() {
assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
@Bean
Person dilbert() {
return new Person("Dilbert");
}
@Bean
Person wally() {
return new Person("Wally");
}