AbstractTestNGSpringContextTests with prototype scope
Does AbstractTestNGSpringContextTests supports spring scopes ?
I am trying to set a prototype scope to my class but I keep getting the same instance from different threads (as "singleton")
My Configuration class:
@Configuration
@ComponentScan
@PropertySource("classpath:/sut/${env}")
public class TestConfig {
@Autowired
private Environment env;
@Bean
public ZookeeperDriver zookeeper() throws Exception {
return new ZookeeperDriver(env.getProperty("zookeeper.host"), env.getProperty("zookeeper.internalIp"));
}
@Bean
public UserAgent userAgent() throws Exception {
return new UserAgent(env.getProperty("userAgent.europeAgentIp"),
env.getProperty("userAgent.europeAgentIp2"),
env.getProperty("userAgent.apacAgentIp"),
env.getProperty("userAgent.useastAgentIp"),
env.getProperty("userAgent.uswestAgentIp"),
env.getProperty("userAgent.stabilityAgentIp"),
env.getProperty("userAgent.parisAgentIp"));
}
@Bean
public IpSecUserAgent ipSecUserAgent() throws Exception {
return new IpSecUserAgent(env.getProperty("ipSecUserAgent.amsAgentIp"), env.getProperty("ipSecUserAgent.amsAgentIntIp"),
env.getProperty("ipSecUserAgent.svAgentIp"), env.getProperty("ipSecUserAgent.svAgentIntIp"));
}
@Bean()
public IpSecGateway ipSecGateway() throws Exception {
return new IpSecGateway(env.getProperty("ipSecGateway.amsGatewayIp"),
env.getProperty("ipSecGateway.amsGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp"),
env.getProperty("ipSecGateway.svGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp2"),
env.getProperty("ipSecGateway.svGatewayIntMask2"));
}
}
My class
@Component
@Scope("prototype")
public class IpSecGateway extends UserAgent {
private GatewayHost amsGw;
private GatewayHost svGw;
private GatewayHost svGw2;
private List<GatewayHost> gatewayHosts = new ArrayList<>();
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public IpSecGateway(String amsGatewayIp, String amsGatewayIntMask,
String svGatewayIp, String svGatewayIntMask, String svGatewayIp2,
String svGatewayIntMask2 ) throws Exception {
super("");
this.amsGw = new GatewayHost("ams", amsGatewayIp, amsGatewayIntMask);
this.svGw = new GatewayHost("sv", svGatewayIp, svGatewayIntMask);
this.svGw2 = new GatewayHost("sv2", svGatewayIp2, svGatewayIntMask2);
gatewayHosts.add(amsGw);
gatewayHosts.add(svGw);
gatewayHosts.add(svGw2);
}
public GatewayHost getAmsGw() {
return amsGw;
}
public void setAmsGw(GatewayHost amsGw) {
this.amsGw = amsGw;
}
public GatewayHost getSvGw() {
return svGw;
}
public void setSvGw(GatewayHost svGw) {
this.svGw = svGw;
}
public GatewayHost getSvGw2() {
return svGw2;
}
public void setSvGw2(GatewayHost svGw2) {
this.svGw2 = svGw2;
}
public List<GatewayHost> getGatewayHosts() {
return gatewayHosts;
}
public void setGatewayHosts(List<GatewayHost> gatewayHosts) {
this.gatewayHosts = gatewayHosts;
}
}
Injection in base class:
@ContextConfiguration(classes = TestConfig.class)
@Listeners({HtmlReporter.class, SlackDriver.class})
public class BaseTest extends AbstractTestNGSpringContextTests {
@Autowired
protected IpSecUserAgent ipSecUserAgent;
@Autowired
protected IpSecGateway ipSecGateway;
}
When I try to call the ipsecGateway instance from different threads - I get the same instance each time (e.g. getMessage)
spring testng
add a comment |
Does AbstractTestNGSpringContextTests supports spring scopes ?
I am trying to set a prototype scope to my class but I keep getting the same instance from different threads (as "singleton")
My Configuration class:
@Configuration
@ComponentScan
@PropertySource("classpath:/sut/${env}")
public class TestConfig {
@Autowired
private Environment env;
@Bean
public ZookeeperDriver zookeeper() throws Exception {
return new ZookeeperDriver(env.getProperty("zookeeper.host"), env.getProperty("zookeeper.internalIp"));
}
@Bean
public UserAgent userAgent() throws Exception {
return new UserAgent(env.getProperty("userAgent.europeAgentIp"),
env.getProperty("userAgent.europeAgentIp2"),
env.getProperty("userAgent.apacAgentIp"),
env.getProperty("userAgent.useastAgentIp"),
env.getProperty("userAgent.uswestAgentIp"),
env.getProperty("userAgent.stabilityAgentIp"),
env.getProperty("userAgent.parisAgentIp"));
}
@Bean
public IpSecUserAgent ipSecUserAgent() throws Exception {
return new IpSecUserAgent(env.getProperty("ipSecUserAgent.amsAgentIp"), env.getProperty("ipSecUserAgent.amsAgentIntIp"),
env.getProperty("ipSecUserAgent.svAgentIp"), env.getProperty("ipSecUserAgent.svAgentIntIp"));
}
@Bean()
public IpSecGateway ipSecGateway() throws Exception {
return new IpSecGateway(env.getProperty("ipSecGateway.amsGatewayIp"),
env.getProperty("ipSecGateway.amsGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp"),
env.getProperty("ipSecGateway.svGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp2"),
env.getProperty("ipSecGateway.svGatewayIntMask2"));
}
}
My class
@Component
@Scope("prototype")
public class IpSecGateway extends UserAgent {
private GatewayHost amsGw;
private GatewayHost svGw;
private GatewayHost svGw2;
private List<GatewayHost> gatewayHosts = new ArrayList<>();
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public IpSecGateway(String amsGatewayIp, String amsGatewayIntMask,
String svGatewayIp, String svGatewayIntMask, String svGatewayIp2,
String svGatewayIntMask2 ) throws Exception {
super("");
this.amsGw = new GatewayHost("ams", amsGatewayIp, amsGatewayIntMask);
this.svGw = new GatewayHost("sv", svGatewayIp, svGatewayIntMask);
this.svGw2 = new GatewayHost("sv2", svGatewayIp2, svGatewayIntMask2);
gatewayHosts.add(amsGw);
gatewayHosts.add(svGw);
gatewayHosts.add(svGw2);
}
public GatewayHost getAmsGw() {
return amsGw;
}
public void setAmsGw(GatewayHost amsGw) {
this.amsGw = amsGw;
}
public GatewayHost getSvGw() {
return svGw;
}
public void setSvGw(GatewayHost svGw) {
this.svGw = svGw;
}
public GatewayHost getSvGw2() {
return svGw2;
}
public void setSvGw2(GatewayHost svGw2) {
this.svGw2 = svGw2;
}
public List<GatewayHost> getGatewayHosts() {
return gatewayHosts;
}
public void setGatewayHosts(List<GatewayHost> gatewayHosts) {
this.gatewayHosts = gatewayHosts;
}
}
Injection in base class:
@ContextConfiguration(classes = TestConfig.class)
@Listeners({HtmlReporter.class, SlackDriver.class})
public class BaseTest extends AbstractTestNGSpringContextTests {
@Autowired
protected IpSecUserAgent ipSecUserAgent;
@Autowired
protected IpSecGateway ipSecGateway;
}
When I try to call the ipsecGateway instance from different threads - I get the same instance each time (e.g. getMessage)
spring testng
add a comment |
Does AbstractTestNGSpringContextTests supports spring scopes ?
I am trying to set a prototype scope to my class but I keep getting the same instance from different threads (as "singleton")
My Configuration class:
@Configuration
@ComponentScan
@PropertySource("classpath:/sut/${env}")
public class TestConfig {
@Autowired
private Environment env;
@Bean
public ZookeeperDriver zookeeper() throws Exception {
return new ZookeeperDriver(env.getProperty("zookeeper.host"), env.getProperty("zookeeper.internalIp"));
}
@Bean
public UserAgent userAgent() throws Exception {
return new UserAgent(env.getProperty("userAgent.europeAgentIp"),
env.getProperty("userAgent.europeAgentIp2"),
env.getProperty("userAgent.apacAgentIp"),
env.getProperty("userAgent.useastAgentIp"),
env.getProperty("userAgent.uswestAgentIp"),
env.getProperty("userAgent.stabilityAgentIp"),
env.getProperty("userAgent.parisAgentIp"));
}
@Bean
public IpSecUserAgent ipSecUserAgent() throws Exception {
return new IpSecUserAgent(env.getProperty("ipSecUserAgent.amsAgentIp"), env.getProperty("ipSecUserAgent.amsAgentIntIp"),
env.getProperty("ipSecUserAgent.svAgentIp"), env.getProperty("ipSecUserAgent.svAgentIntIp"));
}
@Bean()
public IpSecGateway ipSecGateway() throws Exception {
return new IpSecGateway(env.getProperty("ipSecGateway.amsGatewayIp"),
env.getProperty("ipSecGateway.amsGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp"),
env.getProperty("ipSecGateway.svGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp2"),
env.getProperty("ipSecGateway.svGatewayIntMask2"));
}
}
My class
@Component
@Scope("prototype")
public class IpSecGateway extends UserAgent {
private GatewayHost amsGw;
private GatewayHost svGw;
private GatewayHost svGw2;
private List<GatewayHost> gatewayHosts = new ArrayList<>();
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public IpSecGateway(String amsGatewayIp, String amsGatewayIntMask,
String svGatewayIp, String svGatewayIntMask, String svGatewayIp2,
String svGatewayIntMask2 ) throws Exception {
super("");
this.amsGw = new GatewayHost("ams", amsGatewayIp, amsGatewayIntMask);
this.svGw = new GatewayHost("sv", svGatewayIp, svGatewayIntMask);
this.svGw2 = new GatewayHost("sv2", svGatewayIp2, svGatewayIntMask2);
gatewayHosts.add(amsGw);
gatewayHosts.add(svGw);
gatewayHosts.add(svGw2);
}
public GatewayHost getAmsGw() {
return amsGw;
}
public void setAmsGw(GatewayHost amsGw) {
this.amsGw = amsGw;
}
public GatewayHost getSvGw() {
return svGw;
}
public void setSvGw(GatewayHost svGw) {
this.svGw = svGw;
}
public GatewayHost getSvGw2() {
return svGw2;
}
public void setSvGw2(GatewayHost svGw2) {
this.svGw2 = svGw2;
}
public List<GatewayHost> getGatewayHosts() {
return gatewayHosts;
}
public void setGatewayHosts(List<GatewayHost> gatewayHosts) {
this.gatewayHosts = gatewayHosts;
}
}
Injection in base class:
@ContextConfiguration(classes = TestConfig.class)
@Listeners({HtmlReporter.class, SlackDriver.class})
public class BaseTest extends AbstractTestNGSpringContextTests {
@Autowired
protected IpSecUserAgent ipSecUserAgent;
@Autowired
protected IpSecGateway ipSecGateway;
}
When I try to call the ipsecGateway instance from different threads - I get the same instance each time (e.g. getMessage)
spring testng
Does AbstractTestNGSpringContextTests supports spring scopes ?
I am trying to set a prototype scope to my class but I keep getting the same instance from different threads (as "singleton")
My Configuration class:
@Configuration
@ComponentScan
@PropertySource("classpath:/sut/${env}")
public class TestConfig {
@Autowired
private Environment env;
@Bean
public ZookeeperDriver zookeeper() throws Exception {
return new ZookeeperDriver(env.getProperty("zookeeper.host"), env.getProperty("zookeeper.internalIp"));
}
@Bean
public UserAgent userAgent() throws Exception {
return new UserAgent(env.getProperty("userAgent.europeAgentIp"),
env.getProperty("userAgent.europeAgentIp2"),
env.getProperty("userAgent.apacAgentIp"),
env.getProperty("userAgent.useastAgentIp"),
env.getProperty("userAgent.uswestAgentIp"),
env.getProperty("userAgent.stabilityAgentIp"),
env.getProperty("userAgent.parisAgentIp"));
}
@Bean
public IpSecUserAgent ipSecUserAgent() throws Exception {
return new IpSecUserAgent(env.getProperty("ipSecUserAgent.amsAgentIp"), env.getProperty("ipSecUserAgent.amsAgentIntIp"),
env.getProperty("ipSecUserAgent.svAgentIp"), env.getProperty("ipSecUserAgent.svAgentIntIp"));
}
@Bean()
public IpSecGateway ipSecGateway() throws Exception {
return new IpSecGateway(env.getProperty("ipSecGateway.amsGatewayIp"),
env.getProperty("ipSecGateway.amsGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp"),
env.getProperty("ipSecGateway.svGatewayIntMask"),
env.getProperty("ipSecGateway.svGatewayIp2"),
env.getProperty("ipSecGateway.svGatewayIntMask2"));
}
}
My class
@Component
@Scope("prototype")
public class IpSecGateway extends UserAgent {
private GatewayHost amsGw;
private GatewayHost svGw;
private GatewayHost svGw2;
private List<GatewayHost> gatewayHosts = new ArrayList<>();
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public IpSecGateway(String amsGatewayIp, String amsGatewayIntMask,
String svGatewayIp, String svGatewayIntMask, String svGatewayIp2,
String svGatewayIntMask2 ) throws Exception {
super("");
this.amsGw = new GatewayHost("ams", amsGatewayIp, amsGatewayIntMask);
this.svGw = new GatewayHost("sv", svGatewayIp, svGatewayIntMask);
this.svGw2 = new GatewayHost("sv2", svGatewayIp2, svGatewayIntMask2);
gatewayHosts.add(amsGw);
gatewayHosts.add(svGw);
gatewayHosts.add(svGw2);
}
public GatewayHost getAmsGw() {
return amsGw;
}
public void setAmsGw(GatewayHost amsGw) {
this.amsGw = amsGw;
}
public GatewayHost getSvGw() {
return svGw;
}
public void setSvGw(GatewayHost svGw) {
this.svGw = svGw;
}
public GatewayHost getSvGw2() {
return svGw2;
}
public void setSvGw2(GatewayHost svGw2) {
this.svGw2 = svGw2;
}
public List<GatewayHost> getGatewayHosts() {
return gatewayHosts;
}
public void setGatewayHosts(List<GatewayHost> gatewayHosts) {
this.gatewayHosts = gatewayHosts;
}
}
Injection in base class:
@ContextConfiguration(classes = TestConfig.class)
@Listeners({HtmlReporter.class, SlackDriver.class})
public class BaseTest extends AbstractTestNGSpringContextTests {
@Autowired
protected IpSecUserAgent ipSecUserAgent;
@Autowired
protected IpSecGateway ipSecGateway;
}
When I try to call the ipsecGateway instance from different threads - I get the same instance each time (e.g. getMessage)
spring testng
spring testng
asked Nov 25 '18 at 15:30
OrenBOrenB
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Add ActiveProfiles to TestConfig
with your relevant profile
@ActiveProfiles(profiles = { "testing" })
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469016%2fabstracttestngspringcontexttests-with-prototype-scope%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add ActiveProfiles to TestConfig
with your relevant profile
@ActiveProfiles(profiles = { "testing" })
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.
add a comment |
Add ActiveProfiles to TestConfig
with your relevant profile
@ActiveProfiles(profiles = { "testing" })
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.
add a comment |
Add ActiveProfiles to TestConfig
with your relevant profile
@ActiveProfiles(profiles = { "testing" })
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.
Add ActiveProfiles to TestConfig
with your relevant profile
@ActiveProfiles(profiles = { "testing" })
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.
answered Nov 25 '18 at 15:36
user7294900user7294900
23.2k113363
23.2k113363
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469016%2fabstracttestngspringcontexttests-with-prototype-scope%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown