开发者

Need help with developing a class for my JUnit test

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 tim开发者_C百科e 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?


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?
    }
}


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..


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 :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜