Skip to content

Conversation

jgriff
Copy link

@jgriff jgriff commented Oct 15, 2018

Add args property to the @SpringBootTest annotation so tests
can easily supply application arguments to pass to their app under test.

For example,

@SpringBootTest(args = {"--some.option=some-value", "other.arg=other-value"})
public class SpringBootTestArgsTest {
	@Autowired
	private ApplicationArguments args;

	@Test
	public void applicationArgumentsPopulated() {
		assertThat(args.getOptionNames()).contains("some.option");
		assertThat(args.getNonOptionArgs()).contains("other.arg=other-value");
	}
}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 15, 2018
@jgriff jgriff changed the title Support expressing app args in @SpringBootTest annotated tests Support expressing app args in @SpringBootTest annotated tests Oct 15, 2018
@wilkinsona
Copy link
Member

Thanks for the PR. I think there’s a risk that this will give a false sense of security if a mistake has been made in the application’s main method and the args aren’t passed to SpringApplication. That said, I can see the benefit in terms of ApplicationArguments.

My feeling at the moment is that it may be better to unit test anything that uses ApplicationArguments. Let’s see what the rest of the team thinks.

@wilkinsona wilkinsona added type: enhancement A general enhancement for: team-attention An issue we'd like other members of the team to review labels Oct 15, 2018
@snicoll
Copy link
Member

snicoll commented Oct 15, 2018

My feeling at the moment is that it may be better to unit test anything that uses ApplicationArguments.

+1

@jgriff
Copy link
Author

jgriff commented Oct 15, 2018

Yes, I see what you mean on the potential to overlook the responsibility of the app's main() method to forward the args to SpringApplication. Also agree on the strategy to unit test consumers of ApplicationArguments in isolation.

The primary motivation for this is to support integration tests driven by @SpringBootTest. In particular, those with anonymous ApplicationRunner beans.

@Bean
public ApplicationRunner doOnStartup() {
        return args -> ... 
    }
}

Add `args` property to the `@SpringBootTest` annotation so tests
can easily supply application arguments to pass to their app under test.

For example,
```
@SpringBootTest(args = {"--some.option=some-value", "other.arg=other-value"})
public class SpringBootTestArgsTest {
	@Autowired
	private ApplicationArguments args;

	@test
	public void applicationArgumentsPopulated() {
		assertThat(args.getOptionNames()).contains("some.option");
		assertThat(args.getNonOptionArgs()).contains("other.arg=other-value");
	}
}
```
@wilkinsona
Copy link
Member

We've discussed this today and we think you've convinced us. Tentatively assigning to 2.x.

@wilkinsona wilkinsona removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Oct 17, 2018
@wilkinsona wilkinsona added this to the 2.x milestone Oct 17, 2018
@jgriff
Copy link
Author

jgriff commented Oct 17, 2018

:)

@rfelgent

This comment has been minimized.

@snicoll

This comment has been minimized.

@rfelgent

This comment has been minimized.

@snicoll

This comment has been minimized.

@snicoll snicoll self-assigned this Feb 13, 2019
@snicoll snicoll changed the title Support expressing app args in @SpringBootTest annotated tests Support expressing application args in @SpringBootTest Feb 13, 2019
@snicoll snicoll modified the milestones: 2.x, 2.2.0.M1 Feb 13, 2019
snicoll pushed a commit that referenced this pull request Feb 13, 2019
Add `args` property to the `@SpringBootTest` annotation so tests
can easily supply application arguments to pass to their app under test.

See gh-14823
@snicoll snicoll closed this in 7413584 Feb 13, 2019
snicoll added a commit that referenced this pull request Feb 13, 2019
* pr/14823:
  Polish "Support expressing application `args` in `@SpringBootTest`"
  Support expressing application `args` in `@SpringBootTest`
@snicoll
Copy link
Member

snicoll commented Feb 13, 2019

@jgriff thank you for making your first contribution to Spring Boot. This is now merged in master with a polish commit.

@jgriff jgriff deleted the spring-boot-test-args branch February 13, 2019 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants