How to write JUnit test for this?

2014-07-20 8:50 pm
Given:

private List<Segment> segments;

segments = new ArrayList<Segment>()

/**
* Determines if route has segments
*
* @return true if route has segments, false otherwise
*/
public boolean hasSegments() {
if (segments.size () <= 0);
return false;
}

-----------------------------------------------------------------------------------------------------

@Test
public void testhasSegments() {

}

回答 (1)

2014-07-20 9:22 pm
Where the name of your object is 'YourObjectClassName'

@Test
public void testSegments(){

'YourObjectClassName' testObject = new 'YourObjectClassName'();
assertEquals(false, testObject.hasSegments();

testObject.add(new Segment()); //I assume you have an add method for this
assertEquals(true, testObject.hasSegments();
}


收錄日期: 2021-05-01 00:52:06
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20140720125042AAOmyn7

檢視 Wayback Machine 備份