Unable to test a method with JUnit [closed] - java

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 6 years ago.
Improve this question
I'm unable to test the hasNext() method in the IterableIntegerArrays class. If I call this method in IterableIntegerArraysTestCases, it says "cannot find symbol". How should I resolve this error?
public class IterableIntegerArrays extends IntegerRelation implements Iterable<IntPair> {
public IterableIntegerArrays(int n) {
super(n);
}
#Override
public Iterator<IntPair> iterator() {
return new Iterator<IntPair>() {
#Override
public boolean hasNext() {
//implementation
return false;
}
#Override
public IntPair next() {
//implementation
}
};
}
}
-
public abstract class IterableIntegerArraysTestCases extends IntegerRelationTestCases {
protected Iterable<IntPair> iterable;
protected void setIterable(final int n) {
setInstance(n);
iterable = ((Iterable<IntPair>) instance);
}
private void checkHasNext(int a, int b, boolean expResult) {
boolean result = iterable.hasNext(); //It says "cannot find symbol" here
assertEquals("hasNext(" + a + ", " + b + ")", expResult, result);
}
}

Iterable does not have hasNext method . Its Iterator which has hasNext method. You should do
iterable.iterator().hasNext()

Related

Write a constructor in class Doctor to initialize the private field doctor_name [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 3 months ago.
Improve this question
Write a constructor in class Doctor to initialize the private field doctor_name. How can I write this?
class Doctor{
public Doctor(private String doc_name){
}
}
Here is an example class Doctor with a constructor.
class Doctor{
private String doc_name;
Doctor(String doc_name){
this.doc_name = doc_name;
}
}
You can also add a setter and getter to apply encapsulation.
class Doctor{
private String doc_name;
Doctor(String doc_name) {
this.doc_name = doc_name;
}
public void setDoctorName(String doc_name) {
this.doc_name = doc_name;
}
public String getDoctorName() {
return this.doc_name;
}
}
I've made a simple implementation of the class Doctor.
class Main {
public static void main(String[] args) {
Doctor doctor1 = new Doctor("Someone");
System.out.println(doctor1.getDoctorName());
doctor1.setDoctorName("Someone Else");
System.out.println(doctor1.getDoctorName());
}
}
Output:
Someone
Someone Else

Testing an Interface with jUnit [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
I have an Interface and a class which implements it.
I #Override the method and I want to test it using JUnit4.
I don't know if I have an error on my method or if I'm doing something wrong in my test class.
Is it correct to have an instance of the interface itself, or should I make it an instance of the myCustomString Class which implements the Interface.
if anyone could explain how the objects are calling each other that would be awesome.
My JUnit test returns a null pointer exception.
public interface MyCustomStringInterface {
int countNumbers();
}
public class MyCustomString implements MyCustomStringInterface {
private String string;
#Override
public int countNumbers() {
while (string.length() != 0 ) {
int count = 0;
for (int i = 0; i < string.length(); i++) {
if(Character.isDigit(string.charAt(i))) count++;
}
return count;
}
return 0;
}
public class MyCustomStringTest {
private MyCustomStringInterface mycustomstring;
#Test
public void testCountNumbers1() {
mycustomstring.setString("th1s strin8 shou1d hav3 numb3rs,
righ7?");
assertEquals(6, mycustomstring.countNumbers());
}
}
Well, according to the code you posted, you didn't create a MyCustomString object. That's probably the source of your NullPointerException.
Try adding mycustomstring = new MyCustomString() right above your setString method, like this:
public class MyCustomStringTest {
private MyCustomStringInterface mycustomstring;
#Test
public void testCountNumbers1() {
mycustomstring = new MyCustomString();
mycustomstring.setString("th1s strin8 shou1d hav3 numb3rs, righ7?");
assertEquals(6, mycustomstring.countNumbers());
}
}

print method for Generic Stack in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a problem finding a solution for this peculiar problem.
I have this class of a generic Stack.
class Pilita<T> {
private int tam;
private T[] arregloPila;
private int tope;
#SuppressWarnings("unchecked")
public Pilita(int tam) {
this.tam=tam;
arregloPila=(T[])new Object[tam];
tope=-1;
}
public void push(T valor){
if(pilaLlena()){
throw new StackFullException("No se puede meter el: "+"["+valor+"]"+" La pila esta llena");
}
arregloPila[++tope]=valor;
}
public T pop() {
if(pilaVacia()){
throw new StackEmptyException("La pila esta vacia");
}
return arregloPila[tope--];
}
public boolean pilaVacia(){
return (tope == -1);
}
public boolean pilaLlena(){
return (tope==tam-1);
}
public int contar(){
return(tope+1);
}
And I want to know how to implement a method to print the stack, because i have a method for normal stacks, but it doesn't seems to work with Generic Stack.
Any help would be Helpful.
To print the all the stack, you only need to check if the stack is not empty
public static void main(String[] args) {
Pilita pila = new Pilita(3);
pila.push("1");
pila.push("2");
pila.push("3");
while (!pila.pilaVacia()) {
System.out.println("Pil pop " + pila.pop());
}
}

Must implement abstract method error, but it's there [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Keep getting the error, I can't seem to figure out why, the abstract method is implemented. I tried adding the keyword public, but that didn't help at all. I've read examples on the internet about abstract method, but mostl of them are void.
The abstract class:
public abstract class Osoba {
private String ime_prezime;
private char pol;
public Osoba(String ime_prezime, char pol) {
this.ime_prezime=ime_prezime;
this.pol=pol;
}
public String getImePrezime() { return ime_prezime; }
public void setImePrezime(String ime_prezime) { this.ime_prezime=ime_prezime; }
public char getPol() { return pol; }
public void setPol(char pol) { this.pol=pol; }
abstract int brojGodinaOsobe(Datum danasnji_datum);
}
And the subclass:
public class OsobaDatum extends Osoba{
private Datum datum_rodjenja;
public OsobaDatum(String ime_prezime, char pol, final Datum datum_rodjenja) {
super(ime_prezime, pol);
this.datum_rodjenja=datum_rodjenja;
}
public String toString() {
return "OsobaDatum: \n" + "Ime i prezime: " + getImePrezime() +
"\n" + "Pol: " + getPol() + "\nDatum rodjenja: "
+ datum_rodjenja.toString();
}
int brojGodina(Datum danasnji_datum) {
if ( datum_rodjenja.getMesec() < danasnji_datum.getMesec() ) {
return danasnji_datum.getGodina() - datum_rodjenja.getGodina();
}
else if (datum_rodjenja.getDan() <= danasnji_datum.getDan() &&
datum_rodjenja.getMesec() == danasnji_datum.getMesec()) {
return danasnji_datum.getGodina() - datum_rodjenja.getGodina();
}
else
return danasnji_datum.getGodina() - datum_rodjenja.getGodina() -1 ;
}
}
The abstract method's name is brojGodinaOsobe() and it looks like you only implemented a method called brojGodina(). Add the Osobe and you should be OK.
It looks like you are not overriding the method brojGodinaOsobe(Datum danasnji_datum); from Super class.
Change brojGodina(Datum danasnji_datum) to brojGodinaOsobe(Datum danasnji_datum) in the OsobaDatum class.
Regards.

ClassCastException on enum toString [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
Can anyone explain why the following JUnit test will fail with a ClassCastException: Message cannot be cast to java.lang.String in the Assert statement below?
public enum Message{
NULL_REQUEST("Null Request"),
INVALID_NUMBER_OF_REQUESTS("Invalid number of requests");
Message(String msg){
this.msg = msg;
}
#Override
public String toString(){
return msg;
}
private final String msg;
}
Then the test class has:
#Test
public void test(){
String x = Message.INVALID_NUMBER_OF_REQUESTS.toString();
Assert.assertEquals(x, "actual value" );
}
cxfquest,
your code has (had) some errors, please correct it.
The following code works for me:
public class MyTestUnit {
public enum Message{
NULL_REQUEST("Null Request"),
INVALID_NUMBER_OF_REQUESTS("Invalid number of requests");
Message(String msg){
this.msg = msg;
}
private final String msg;
public String getMsg() {
return msg;
}
#Override
public String toString() {
return msg;
}
}
#Test
public void test(){
String x = Message.INVALID_NUMBER_OF_REQUESTS.toString();
Assert.assertEquals(x, "actual value" );
}
}
I think you are not compiling and you are running an old version (maybe).
Result is:
org.junit.ComparisonFailure: expected:<[Invalid number of requests]>
but was:<[actual value]>
as expected.

Categories