How can I receive a new bean between each @Test
?
Example:
public class Person(){
private String name = "bob";
//getters //setters
}
public Class MyTest(){
@Autowired
private Person person;
@Test
public void firstName(){
person.setName = "Peter";
system.out.println(person.getName());
(Should print out Peter)
}
@Test
public void firstNameTest2(){
system.out.println(person.getName());
(Should be reset and print bob)
}
I've been using @DirtiesContext(classMode = ClassMode.ON_TEST_METHOD)
As a TestClass Annotation, however, this is horrible.. my other frameworks re-init between each test... and slows everything down by a full minute.
I've tried to configure custom scope, and I implemented the Spring Listeners... I reset my Person Bean after each test method... However, that only works at the class level, it doesn't give me a new Person object between @Test
scopes.
Thank you, let me know if you need more code to clarify.
Depends on the framework you're using. But in any case you need a bean of scope prototype
, not a singleton
. A new prototype
bean is created for each dependency injection:
<bean id="obj" class="java.lang.Object" scope="prototype"/>
JUnit and its direvatives . The feature of JUnit is that it re-creates test class for every test method for isolation purposes. This means its fields are getting re-initialized every time, so you don't need to do anything to get prototype
bean work - just @Autowire
that field.
TestNG . This framework doesn't re-create test class, so it's a bit more involved to get prototype
working for each method:
@Autowired Object obj;
@Autowired ApplicationContext appContext;
@BeforeMethod
public void injectSpringBeans() {
obj = appContext.getBean("obj");
}
Test Data can be created with factories . In your example you create an instance of Person
which seems like test data. Spring is used to init a graph of cross-dependent beans. If the object doesn't have dependencies and is not shared between multiple other beans it's more natural to create it in the target classes or use Factories.
Webdriver is shared with Page Objects . Question title says about webdriver
though. If you're using Page Objects-like frameworks that also have a link to webdriver
then you'd be using a different webdriver than the rest of your code. Which simply won't work. Moreover webdriver
is rarely used in the tests themselves - it's usually hidden by Business Layer or Page Objects layer completely. Read this article for details. If you need a new webdriver instance, prepare to re-create a lot of other objects as well.
You can implement your own Spring scope or use something like thread
scope for all your test-beans and run every method in its own thread (TestNG allows that). But that's going to be complicated and will perform poorly as creating new webdriver
session means starting a new browser which is very expensive.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.