How to parameterize exceptions in JUnit5 with hemcrest? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i want to test all different exceptions with one parameterized test using hemcrest for one method. So that means that Exception1.class, Exception2.class should be parameters. How do i parameterize them, and do it by using hemcrest?

Suppose your method under test returns distinct exceptions according to scenarios, you should parameterize both fixture (for scenarios) and expected (for exceptions).
With a Foo.foo(String input) method to test such as :
import java.io.FileNotFoundException;
public class Foo {
public void foo(String input) throws FileNotFoundException {
if ("a bad bar".equals(input)){
throw new IllegalArgumentException("bar value is incorrect");
}
if ("inexisting-bar-file".equals(input)){
throw new FileNotFoundException("bar file doesn't exit");
}
}
}
it could look like :
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.api.Assertions;
public class FooTest {
#ParameterizedTest
#MethodSource("fooFixture")
void foo(String input, Class<Exception> expectedExceptionClass, String expectedExceptionMessage) {
Assertions.assertThrows(
expectedExceptionClass,
() -> new Foo().foo(input),
expectedExceptionMessage
);
}
private static Stream<Arguments> fooFixture() {
return Stream.of(
Arguments.of("a bad bar", IllegalArgumentException.class, "bar value is incorrect"), Arguments.of("inexisting-bar-file", FileNotFoundException.class, "bar file doesn't exit"));
}
}

Related

Google Firebase real time database updateChildren() method is not working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
In my application, I am using google firebase real-time database with admin-sdk for Java. When I run a sample program from my machine it works perfectly and updates the given data into firebase db. But, when I try to run the same program from another public server, it's not working. It doesn't throw any exceptions at all. updateChildren() was executed, and then nothing. I tried with onCompletionListener too.There is nothing in there. What might be the problem?
FBManager.java
import com.nexge.firebase.Firebase;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import java.util.HashMap;
class FBManager {
//FIREBASE
public static Firebase fireBase;
public static DatabaseReference dbRef = null;
public static String filePath="key/siva-6d0aa-firebase-adminsdk-1bo0t-e98c5af5d5.json";
public static String databaseUrl="https://siva-6d0aa.firebaseio.com";
public static String projectName="ABES";
public static String databaseChildPath;
public static void main(String[] args) {
try {
databaseChildPath="calls/";
System.out.println("Inside iniDB !");
fireBase = new Firebase(filePath, databaseUrl, projectName, databaseChildPath);
System.out.println("FireBase db created!");
dbRef = fireBase.getDatabaseReference();
System.out.println("Got reference!");
T t = new T();
new Thread(t).start();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
T.java
class T implements Runnable {
public void run() {
try {
String path = "191963944/5e0e44e2-3442-4acd-977a-fea7232c0f0a";
HashMap hm1 = new HashMap();
hm1.put("caller_Recv_Media_IpPort","xxx.xxx.xx.xxx:9811");
HashMap hm2 = new HashMap();
hm2.put("yServer_src_Media_IpPort","xxx.xx.xx.xx:10000");
FBManager.dbRef.child(path).updateChildren(hm1);
FBManager.dbRef.child(path).updateChildren(hm2);
Thread.sleep(10000);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
It seems this is to do with server network issue. Working fine in another server. Closing this question. thanks

A good way to proof to my teacher that java streams can have worse performance than a simple for [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
My teacher is convinced that it is convenient to use streams when looping over a list because operations can be parallelized.
I understand that this is true in some ways, but I think that we could always implement a faster code writing it by ourselves.
We are talking here of use cases where we want to optimize as much as we can.
Suppose that we start with the following code:
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class HelloWorld{
public static void main (String args[]) {
List<Something> ls = new ArrayList<Something>();
ls.add(new Something(true));
ls.add(new Something(false));
System.out.println("Active things: " + getActive(ls).size());
}
public static List<Something> getActive(List<Something> listOfThings){
return listOfThings != null? listOfThings.stream().filter(t -> t.isActive()).collect(Collectors.toList()): null;
}
public static class Something {
public Something(boolean active) {
this.active = active;
}
private boolean active;
public boolean isActive() {
return active;
}
public void justDoIt() {
System.out.println("done");
}
}
}
Isn't true that, the method getActive() can be optimized avoiding the use of streams ?
I understand that it is easier to use streams, but because they have to be general purpose, they will never be faster than well written and optimized code.
For example, if the list is very big and we know that it would be convenient to parallelize the loop on three cores, couldn't we just execute in three different threads the loop with a standard iterator?

An error ocurred when executing GradeBook [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public class test{
public void mosleh(String coursName)
{
System.out.printf("Welocm to grade bok for\n%s!\n,coursName");
}
}
--
import java.util.Scanner;
public class GradeBookTest {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
GradeBook myGradeBook = new GradeBook ();
System.out.println("please enter the cours name");
String nameOfCourse = input.nextLine();
System.out.println();
myGradeBook.mosleh(nameOfCourse);
}
}
Its unclear what youre asking but you need to rename your test class to avail of the mosleh method
public class GradeBook {
You need to declare the mosleh() method as static
public static void mosleh(String coursName);
In order to use it in this way:
myGradeBook.mosleh(nameOfCourse);
Or create an object from myGradeBook then call the method, like this:
new myGradeBook().mosleh(nameOfCource);
Consider that you need to change the class name to myGradeBook instead of test

Getting Compile time error in java funciton body [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have created below code in java but getting error during compilation i have gone through with complete code but not able debug the problem
package csaAutomation;
import com.thoughtworks.selenium.SeleneseTestCase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.util.Date;
import java.util.Date.*;
import java.text.SimpleDateFormat;
public class GuiAutomation extends SeleneseTestCase {
#BeforeTest
public void setUp() throws Exception {
WebDriver driver = new FirefoxDriver();
String baseUrl = "url";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
}
#Test
public void testGUI_Automation() throws Exception {
String VAR_1="30000";
public void GUI_Login_MR_SIT() throws Exception {
selenium.open("/CSAlogin");
selenium.type("//input[#id=\"username\"]", "Administrator");
selenium.type("//input[#id=\"password\"]", "Ari_123");
selenium.click("//img[#src=\"/csaweb/resources/images/buttons/bf_login.gif\"]");
System.out.println("-----------GUI Login Successful-----------");
}
}
#AfterTest
public void tearDown() throws Exception {
selenium.stop();
}
}
Below is the error message :
C:\CSA_GUI_Automation_0.4\src\csaAutomation\GuiAutomation.java:34: illegal start
of expression
public void GUI_Login_MR_SIT();
^
Please assist
You've declared a method inside another method. I'm not sure of the intent of your code but I suspect it will work better if you have:
#Test
public void testGUI_Automation() throws Exception {
String VAR_1="30000";
GUI_Login_MR_SIT();
}
public void GUI_Login_MR_SIT() throws Exception {
selenium.open("/CSAlogin");
selenium.type("//input[#id=\"username\"]", "Administrator");
selenium.type("//input[#id=\"password\"]", "Ari_123");
selenium.click("//img[#src=\"/csaweb/resources/images/buttons/bf_login.gif\"]");
System.out.println("-----------GUI Login Successful-----------");
}
}
public void testGUI_Automation() throws Exception {
String VAR_1="30000";
public void GUI_Login_MR_SIT();
You are trying to declare a method within a method body. This syntax:
is unsupported in Java;
generally has no apparent meaning which could be assigned to it.

What to do with classes within classes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently creating a events recorder GUI program. Yet I am encountering a very difficult problem.
How should I store objects within an object within an object within an object?
For example,
I have an event.
This event has 4 category.
In the first category (Category A), there are 30 exhibition shows.
Within each show, there are 20 - 30 representatives. (Let's say 30 reps for the first show).
...
How can I store all these information in an arraylist? OR is there any other better idea?
Should I also apply Polymorphism to this one too?
Event --> Category A (first one out of the four) --> First Show out of 30 --> 1 rep out of 30 reps --> ... etc.
Thanks.
My confusion is that I would like to treat every single of these as an object. For example, category is an object. The show is an object. The reps is an object. My question is how can I store an object within an object within an object and so on? Thanks.
try this
Test.java
import java.util.List;
public class Test {
public List<Category> category;
}
import java.util.List;
Category.java
public class Category {
public List<Exhibition> exhibitionShow;
public void setExhibitionShow(List<Exhibition> exhibitionShow) {
this.exhibitionShow = exhibitionShow;
}
public List<Exhibition> getExhibitionShow() {
return exhibitionShow;
}
}
Exhibition.java
import java.util.List;
public class Exhibition {
public List<Representative> representative;
public void setRepresentative(List<Representative> representative) {
this.representative = representative;
}
public List<Representative> getRepresentative() {
return representative;
}
}
Representative .java
public class Representative {
//add method
}
I am not sure what is your confusion, you need to do something like this
class Event{
Category[] categories;
}
class Category{
ArrayList<Show> shows;
}
class Show{
ArrayList<Representative> reps;
}
//.. and so on.
I think you have the idea. My idea is create one public method.

Categories