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.
Related
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
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();
}
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());
}
}
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 5 years ago.
Improve this question
I have to use ArrayList to pop up data in the table
My code is
public class Search {
public String url;
public String txt;
public Search(String url, String txt) {
this.url = url;
this.txt = txt;
}
public String geturl() {
return url;
}
public void seturl(String url) {
this.url = url;
}
public String gettxt() {
return txt;
}
public void settxt(String txt) {
this.txt = txt;
}
...
Search o1 = new Search(doc.get("uri"),txt);
lst.add(o1);
...
Result is [com.jcasey.IndexImpl$Search#507c0719] but should be [https://www.experts-exchange.com, Welcome to Experts Exchange]
What I'm doing wrong? Please advise
Please override the toString() method in your Search class.
public String toString(){
return url + ", " + txt;
}
You didn't override toString() for Search class
#Override
public String toString() {
return url + ", " + txt;
}
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()