Testing an Interface 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 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());
}
}

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

Trouble calling methods from a class/ [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 11 months ago.
Improve this question
Can someone tell me whats wrong with these codes ?
public class Student {
String name;
int roll_no;
public void getDetails(String Name, int roll) {
name = Name;
roll_no = roll;
}
}
and this
public class StudentRun {
Student student = new Student();
String n = "John";
int r = 2;
student.getDetails(n, r);
}
It shows the error:
Multiple markers at this line
on the line where i call the student.getDetails(n,r)
You cannot call a method in a class without it being wrapped in a method.
Your Student class also lacks a constructor (a method that is called when the class is instantiated) and lacks the context of attribute visibility (public/protected/private).
The constructor must call itself as the class, in your case:
public class Student {
protected String name;
protected int roll_no;
public Student(String Name, int roll) {
this.name = Name;
this.roll_no = roll;
}
public String getName() {
return this.name
}
....
}
Once you have structured the class correctly, you need to do the following to instantiate it:
class OtherClass {
public static void main (String[] args) {
student = new Student("John", 42);
System.out.println(student.getName());
}
}
it looks to me you are calling the method without wraping it in a method in your class StudentRun. try using a constructor or some other method to call student.GetDetails
like
void callStudentRun
{
student.Getdetails();
}

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.

Unable to test a method 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 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()

Instantiating two instances of a class [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 7 years ago.
Improve this question
I have a quick question
Suppose I were to create a data definition class named Campaign
and then I want to make an implementation class
Let's say in this implementation class I have a few variables(campaignName, campaignType, campaignGoal) and some methods (getCampName, getCampType, getCampGoal).
I want to create two different campaigns classes, and then compare their information at the end of the program with a print method.
Would this be a proper way of declaring information for the first campaign:
Campaign campaign1 = new Campaign();
which is in the main method, and then let's say I want to get the name of the first campaign, which is just hardcoded for now
public static String campaign1.getCampName(){
campaign1.setCampName("IT Student Scholarship Fund");
}
I just wanted to know how to do this. Thank you.
getCampName() should look something like:
public String getCampName() { return this.campaignName; }
a then simply
campaign1.getName();
You should stop the practice of putting all of your code in a main method. Instead, instantiate your Campaign instances and call methods on each one using a driver method in your primary class. In addition, you can override the equals() method to do the comparison (or implement Comparator).
public class CampaignTest{
public void runTest(){
Campaign c1 = new Campaign("First Campaign");
Campaign c2 = new Campaign("Second Campaign");
Campaign c11 = new Campaign("First Campaign");
System.out.println("c1==c2: " + c1.equals(c2));
System.out.println("c2==c11: " + c2.equals(c11));
System.out.println("c1==c11: " + c1.equals(c11));
}
public static void main(String... args){
CampaignTest test = new CampaignTest();
test.runTest();
}
private class Campaign{
private String name;
public Campaign(String n){
this.name = n;
}
public String getName(){
return name;
}
#Override
public boolean equals(Object other){
if(other instanceof Campaign && ((Campaign)other).getName().equals(name)){
return true;
}
return false;
}
}
}

Categories