views:

71

answers:

4

I have this JUnit test that I need help developing a Interface and Class for, here is the test:

Box b1 = new DefaultBox( "abc" ); Box b2 = new DefaultBox( "def" ); 
Box b3 = new DefaultBox( "" ); 

assertEquals("abc", b1.contents()); 
assertEquals("[abc]", b1.toString()); 
assertTrue(b1.equals(b1)); assertFalse(b1.equals(b2)); 
assertFalse(b1.equals(null)); 
assertEquals("cba", b1.flip().contents()); 
assertEquals("", b3.flip().contents()); 

can anyone help me in developing a Default box class and a box interface to make these test pass? Any help would be most appreciated.

Updates

Ok I am trying to start a constuctor but i keep getting a run time error saying "Implicit super constructor Box() is undefined. Must explicitly invoke another constructor" Here is my class:

import javax.swing.Box;
public class DefaultBox extends Box{
       public DefaultBox(String string) {

    }
}

my Junit test is:

import static org.junit.Assert.*;
import javax.swing.Box;



public class question3_test {

    Box b1 = new DefaultBox( "abc" );
    Box b2 = new DefaultBox( "def" ); 
    Box b3 = new DefaultBox( "" );

    public void testquestion3(){
    assertEquals("abc", b1.contents()); 
    assertEquals("[abc]", b1.toString()); 
    assertTrue(b1.equals(b1)); assertFalse(b1.equals(b2)); 
    assertFalse(b1.equals(null)); 
    assertEquals("cba", b1.flip().contents()); 
    assertEquals("", b3.flip().contents()); 
    }

}

I have tried to remove the "extends Box" but then that gives me a run time error on the Junit test. Can anyone guide me on how to remove this implicit super constructor error?

A: 

What kind of help you need? Your question doesn't seem so complex.

  • contents should return the string contained
  • toString should put it in brackets
  • equals should check if contents are equal
  • flip should return a new DefaultBox with reversed content

I suppose this is homework, but the class that you have to implement it's quite trivial..

Jack
This is homework and I basically need help on how to construct this class. I am struggling on how to use TDD to construct actual classes, any help would be much appreciated
A: 

Implicit super constructor Box() is undefined. Must explicitly invoke another constructor". It is because DefaultBox calls a constructor that is not compatible with Box. In this case DefaultBox has a String constructor which because it is empty, JVM will try to create a Box with no-arg constructor which it cant find. Try,

import javax.swing.Box;
public class DefaultBox extends Box{
       public DefaultBox(String string) {
            //call some version of the Box constructor that is suitable. Javax swing has only a int arg constructor so super(5); perhaps?
       }
}

Quite why you would have a homework on Swing Boxes is puzzling to me though :)

Calm Storm
actually eclipse wanted me to put in the javax.swing.Box, without it my Junit test varibales get highlighted in red
My guess is that your assignment wants you to create an interface/abstract class Box on your own and also write a DefaultBox that implements/extends Box.
Calm Storm
alpdog14: your test variables are SUPPOSED to get highlighted in red. This is test-driven development. Of course Eclipse can't find the class; you haven't written it yet. Remove the import, you do not want to use javax.swing.Box, the teacher expects you to write an interface Box.
Ricket
+1  A: 

Here is one possible skeleton. I choose not to provide full implementations since this is a homework problem

interface Box {
  //put content and flip methods
}

public class DefaultBox implements Box {
    public DefaultBox(String str) {
        //Find out how to store this str as an internal field variable?
    }
}
Calm Storm
I do appreciate everyone's help but I had a side questions about Eclipse. I created my classes and JUnit test but the JUnit test file is not letting me run it as a test, all it says is Run Configuration. I have used this before and was able to select Run As JUnit test. Does anyone know how to activate this option.
alpdog14: with the JUnit file open, just click the green play button in the toolbar. It should recognize it as a JUnit test and launch the correct configuration, and from then on you can use the Run As menu if you'd prefer.
Ricket
A: 

I'm pretty sure that you don't want to use javax.swing.Box, but rather a custom interface that you either got together with your assignment or have to write yourself. So remove the import of javax.swing.Box or replace it with the correct import.

Also, I'd like to suggest a different way to write your JUnit test:

import static org.junit.Assert.*;

public class DefaultBoxTest {

     Box b1 = new DefaultBox( "abc" );
     Box b2 = new DefaultBox( "def" ); 
     Box b3 = new DefaultBox( "" );

     public void testContents(){
         assertEquals("abc", b1.contents()); 
     }

     public void testToString(){
         assertEquals("[abc]", b1.toString()); 
     }

     public void testEqualsItSelf(){
         assertTrue(b1.equals(b1));
     }

     public void testNotEqualsOther(){
         assertFalse(b1.equals(b2)); 
     }

     public void testNotEqualsNull(){
         assertFalse(b1.equals(null)); 
     }

     public void testFlip(){
         assertEquals("cba", b1.flip().contents()); 
     }

     public void testFlipEmpty(){
         assertEquals("", b3.flip().contents()); 
     }
}

This tests effectively the same thing, but it will tell you more precisely which parts work and which ones don't. Because with your test, it will only tell you that the entire test failed and you'll have to find out which one failed. If you write it this way it will tell you exactly which assert failed.

Joachim Sauer