How do i pass the browser from test to test with Arquillian Drone and Graphene - user-interface

I'm trying out Arquillian Drone and Graphene and I have the following 2 tests what i want to do is have the second test use the browser from the first tests. but the browser closes after the first test finishes - is there a way to pass the browser to the second test?
#RunAsClient
public class Test1 extends Arquillian{
private final String subscriptionName = "subName";
private final String subscriptionDescription = "description";
#Test(dataProvider = Arquillian.ARQUILLIAN_DATA_PROVIDER)
public void enterSubscriptionName(#InitialPage SubscriptionPage subscriptionPage) {
subscriptionPage.enterName(subscriptionName);
assertEquals(subscriptionName, subscriptionPage.getNameFieldValue());
}
#Test(dataProvider = Arquillian.ARQUILLIAN_DATA_PROVIDER)
public void enterSubscriptionDescription( SubscriptionPage subscriptionPage) {
subscriptionPage.enterDescription(subscriptionDescription);
assertEquals(subscriptionDescription, subscriptionPage.getDescriptionFieldValue());
}
}

I would refactor the above test as shown here to use the SubscriptionPage in the second test.
#RunAsClient
public class Test1 extends Arquillian {
private final String subscriptionName = "subName";
private final String subscriptionDescription = "description";
#Page
SubscriptionPage subscriptionPage;
#Test(dataProvider = Arquillian.ARQUILLIAN_DATA_PROVIDER)
public void enterSubscriptionName() {
Graphene.goTo(SubscriptionPage.class);
subscriptionPage.enterName(subscriptionName);
assertEquals(subscriptionName, subscriptionPage.getNameFieldValue());
}
#Test(dataProvider = Arquillian.ARQUILLIAN_DATA_PROVIDER)
public void enterSubscriptionDescription() {
subscriptionPage.enterDescription(subscriptionDescription);
assertEquals(subscriptionDescription, subscriptionPage.getDescriptionFieldValue());
}
}

Related

How to test GET request with body in Spring RestController?

I have a rest controller like this;
#RestController
#RequiredArgsConstructor
#RequestMapping(PO)
public class PoController {
private final PoService service;
#GetMapping(value = FILTER, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<List<PoDTO>> filter(PoFilterCriteria poFilterCriteria) {
return ok().body(service.getPos(poFilterCriteria));
}
}
And I want to write an unit test for it but I couldn't achieve to mock the service to return list.
This is my poFilterCriteria model;
#Data
public class PoFilterCriteria {
private double hp;
private FilterOperationType hpOperationType;
private double attack;
private FilterOperationType attackOperationType;
private double defense;
private FilterOperationType defenseOperationType;
}
And this is my test;
#WebMvcTest(value = PoController.class)
class PoControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private PoService service;
private PoDTO poDTO;
private List<PoDTO> poDTOList;
#BeforeEach
void setUp() {
poDTOList = new ArrayList<>();
poDTO = new Po();
poDTOList.add(poDTO);
}
#Test
public void filter_success() throws Exception {
PoFilterCriteria poFilterCriteria= new PoFilterCriteria ();
poFilterCriteria.setAttack(40);
poFilterCriteria.setAttackOperationType(GT);
poFilterCriteria.setHp(49);
poFilterCriteria.setHpOperationType(EQ);
poFilterCriteria.setDefense(60);
poFilterCriteria.setDefenseOperationType(LT);
when(service.getPos(poFilterCriteria)).thenReturn(poDTOList);
mockMvc.perform(get(PO + FILTER)
.param("hp", String.valueOf(40))
.param("hpOperationType", String.valueOf(GT))
.param("attack", String.valueOf(49))
.param("attackOperationType", String.valueOf(EQ))
.param("defense", String.valueOf(60))
.param("defenseOperationType", String.valueOf(LT))
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(new ObjectMapper().writeValueAsString(poDTOList)));
}
}
But the list that should return with size of 1 is returning empty.
What did I do wrong?
org.mockito.ArgumentMatchers#any(java.lang.Class)
when(service.getPos(any(PoFilterCriteria.class))).thenReturn(poDTOList);
or
org.mockito.ArgumentMatchers#same
when(service.getPos(same(poFilterCriteria))).thenReturn(poDTOList);

How to handle objects created within the method under test

I have the following model classes:
#Data
public class Address {
private String street;
private int number;
}
#Data
public class Person {
private String name;
private Address address;
}
and the following services:
#Service
public class MyService {
private final OtherService otherService;
public MyService(OtherService otherService) {
this.otherService = otherService;
}
public void create() {
Person myPerson = new Person();
myPerson.setName("John");
otherService.synchronize(myPerson);
myPerson.getAddress().setNumber(12);
}
}
#Service
public class OtherService {
public void synchronize(Person person) {
Address address = new Address();
address.setStreet("sample street");
address.setNumber(123);
person.setAddress(address);
}
}
I want to write a unit test for MyService. This is the not working version of the test:
#ExtendWith(SpringExtension.class)
class MyServiceTest {
#Mock OtherService otherService;
#InjectMocks MyService myService;
#Test
void test_create() {
// GIVEN
doNothing().when(otherService).synchronize(any(Person.class));
// WHEN
myService.create();
// THEN
verify(otherService).synchronize(any());
}
}
This fails because the myPerson object is created within the method being tested and therefore I get a NullPointerException when running the test. How could I deal with this issue? should I capture the value passed to the otherService?
There's a little complexity but it's not bad. Replace your doNothing call with something like this:
Mockito.doAnswer(
new Answer<Void>() {
public Void answer(InvocationOnMock invocation) throws Exception {
Person arg = invocation.getArgument(0);
arg.setAddress(new Address());
return;
}
}).when(otherService).synchronize(any(Person.class));

Error testing with Spring Cloud Stream Test

We are using spring-cloud-stream to manage messages between our applications.
We have custom bindings:
public interface InboundChannels {
String TASKS = "domainTasksInboundChannel";
String EVENTS = "eventsInboundChannel";
#Input(TASKS)
SubscribableChannel tasks();
#Input(EVENTS)
SubscribableChannel events();
}
public interface OutboundChannels {
String TASKS = "domainTasksOutboundChannel";
String EVENTS = "eventsOutboundChannel";
#Output(TASKS)
MessageChannel tasks();
#Output(EVENTS)
MessageChannel events();
}
There are processors that consumes tasks and generate events:
#EnableBinding({InboundChannels.class, OutboundChannels.class})
public class TasksProcessor {
public TasksProcessor(
UserService userService,
#Qualifier(OutboundChannels.EVENTS) MessageChannel eventsChannel
) {
this.userService = userService;
this.eventsChannel = eventsChannel;
}
#StreamListener(value = TASKS, condition = "headers['" + TYPE + "']=='" + CREATE_USER + "'")
public void createUser(Message<User> message) {
final User user = message.getPayload();
userService.save(user)
.subscribe(created -> {
Message<User> successMessage = fromMessage(message, Events.USER_CREATED, created).build();
eventsChannel.send(successMessage);
});
}
}
Now we wanted to test it using spring-cloud-stream-test-support and its amazing features:
#DirtiesContext
#SpringBootTest
#RunWith(SpringRunner.class)
public class TasksProcessorTest {
private User user;
#Autowired
private InboundChannels inboundChannels;
#Autowired
private OutboundChannels outboundChannels;
#Autowired
private MessageCollector collector;
#Before
public void setup() {
user = new User(BigInteger.ONE, "test#teste.com");
}
#Test
public void createUserTest() {
final Message<User> msg = create(CREATE_USER, user).build();
outboundChannels.tasks().send(msg);
final Message<?> incomingEvent = collector.forChannel(inboundChannels.events()).poll();
final String type = (String) incomingEvent.getHeaders().get(TYPE);
assertThat(type).isEqualToIgnoringCase(USER_CREATED);
}
}
application.properties
##
# Spring AMQP configuration
##
spring.rabbitmq.host=rabbitmq
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
# Events channels
spring.cloud.stream.bindings.eventsOutboundChannel.destination=events
spring.cloud.stream.bindings.eventsInboundChannel.destination=events
spring.cloud.stream.bindings.domainTasksOutboundChannel.destination=domainTasks
spring.cloud.stream.bindings.domainTasksInboundChannel.destination=domainTasks
spring.cloud.stream.bindings.userTasksInboundChannel.group=domainServiceInstances
spring.cloud.stream.bindings.eventsInboundChannel.group=domainServiceInstances
But then we get this error:
java.lang.IllegalArgumentException: Channel [eventsInboundChannel] was not bound by class org.springframework.cloud.stream.test.binder.TestSupportBinder
What are we doing wrong?
In the .subscribe() you do eventsChannel.send(successMessage);, where that eventsChannel is from the OutboundChannels.EVENTS, but what you try to do in the test-case is like inboundChannels.events(). And it doesn't look like you really bind this channel anywhere.
I'm sure if you would use outboundChannels.events() instead, that would work for you.

TestNG listener, how to get the singleton use by the tests

We use the TestNG listener feature to report the results of our tests in an external tool. It works fine.
But now, we want to add an information contained by a singleton (a ThreadLocal webdriver). Our test campaign is run in parallel, so we have multiple instance of our singleton.
How can we in the method onSuccess for instance get the correct singleton and so report the correct information?
Edit: code sample
public class QTestWrapper implements ISuiteListener, ITestListener {
...
#Override
public void onTestSuccess(ITestResult result) {
String sessionId = MyDriver.getSessionId();
// do my job with this session id
}
...
}
public final class MyDriver {
private static final ThreadLocal<MyDriver> MY_DRIVER =
ThreadLocal.withInitial(MyDriver::buildDriver);
private WebDriver driver;
private MyDriver(WebDriver driver) {
this.driver = driver;
}
public static MyDriver getDriver() {
return MY_DRIVER.get();
}
private static MyDriver buildDriver() {
URL remoteAddress = getHubAddress();
DesiredCapabilities caps = buildCapabilities();
WebDriver driver = new RemoteWebDriver(remoteAddress, caps);
return new MyDriver(driver);
}
public static String getSessionId() {
String sessionId = null;
MyDriver driver = MY_DRIVER.get();
if (driver != null && driver.getWrappedDriver() != null) {
WebDriver wrappedDriver = driver.getWrappedDriver();
sessionId = ((RemoteWebDriver) wrappedDriver).getSessionId().toString();
}
return sessionId;
}
public WebDriver getWrappedDriver() {
return driver;
}
}
public class TestLogin {
#AfterMethod(alwaysRun = true)
public void die() {
StickyDriver.quit();
}
#Test(description = "Check that a user can login with a Manager profile.")
public void loginAsManager() {
Actor actor = new Actor("foo");
String userName = actor.openBrowserAndLogin().getUserName();
assertThat(userName).isEqualTo("foo");
}
}
public class Actor {
private static final Logger LOGGER = LogManager.getLogger();
private final MyDriver driver;
private User user;
public Actor(String userName) {
user = User.getUser(userName);
driver = MyDriver.getDriver();
}
public Actor(User user) {
this.user = Objects.requireNonNull(user);
driver = MyDriver.getDriver();
}
public HomePage openBrowserAndLogin() {
openBrowser();
return login();
}
public HomePage login() {
LoginPage loginPage = new LoginPage(driver);
loginPage.getUserNameField().setValue(user.getUsername());
loginPage.getPasswordField().setValue(user.getPassword());
return loginPage.login();
}
private void openBrowser() {
String url = EnvironmentHelper.getUrl();
WebDriver webDriver = driver.getWrappedDriver();
webDriver.get(url);
try {
new WebDriverWait(webDriver, 60).until(AjaxExpectedConditions.callsHaveCompleted());
} catch (TimeoutException ex) {
closeBrowser();
throw ex;
}
}
public void closeBrowser() {
MyDriver.quit();
}
}
TestNG does not guarantee that the #Test and onSuccess() will be executed in the same thread.
That assurance is only provided when you work with org.testng.IInvokedMethodListener implementation.
So please change your implementation to do the following :
Have your class QTestWrapper implement org.testng.IInvokedMethodListener
within the afterInvocation() of org.testng.IInvokedMethodListener check if the method that got executed was a #Test method and if its status was a pass, and if it passed you move your logic within onSuccess() into it.

PowerMockito verifyNew and verifyPrivate are mutually exclusive?

I have a class like this
#Component
public class TestClass {
public void testMethod(){
FinalClass f = new FinalClass("string");
somePrivateMethod(f.getSomeString());
}
private void somePrivateMethod(String s){
}
}
As you can see it has a public method and private method. In public method it is instantiating an instance of FinalClass, which is a class in some third party library and it is final. Lets say it is like this
public final class FinalClass {
private final String someString;
public FinalClass(final String s) {
someString = s;
}
public String getSomeString() {
return someString;
}
}
And Now I am writing unit test for my test class. Since I have to verify final classes and private methods, I am using powermockito. And this is how my test class looks like
#RunWith(PowerMockRunner.class)
#PrepareForTest({TestClass.class, FinalClass.class})
public class TestClassTest {
private TestClass testClass;
private FinalClass finalClass;
#Before
public void setUp() {
finalClass = PowerMockito.mock(FinalClass.class);
testClass = spy(new TestClass());
}
#Test
public void testSomething() throws Exception {
whenNew(FinalClass.class).withAnyArguments().thenReturn(finalClass);
testClass.testMethod();
verifyNew(FinalClass.class);
//verifyPrivate(testClass).invoke("testMethod");
}
}
It works fine. But the problem is the last two statements verifyNew and verifyPrivate are working mutually exclusively. I mean when I comment one of those(doesn't matter which), the test passes. But when both are enabled, the test fails
Does anyone have any idea why this is happening?

Resources