foobar :
For example, I have a graph below which shows me testing a depth first. I want to try and make my tests where I simply have a graph already made, then I make different test cases for that graph. But at the moment I seem to be making a new graph in each test method I make
@Test
void willItDepthFirst(){
DepthFirstTraversal b = new DepthFirstTraversal();
b.add(1);//add node
b.add(0);
b.add(2);
b.add(3);
b.add(4);
b.add(0,1);//connect nodes with edges
b.add(1,2);
b.add(2,3);
b.add(0,4);
List<Integer> result = b.traverse();
List<Integer> expected = Arrays.asList(0,1,4,2,3);
assertEquals(expected, result);
}
This is kind of the idea of what I want:
@Test
void graphToBeTested(){
DepthFirstTraversal b = new DepthFirstTraversal();
b.add(1);//add node
b.add(0);
b.add(2);
b.add(3);
b.add(4);
b.add(0,1);//connect nodes with edges
b.add(1,2);
b.add(2,3);
b.add(0,4);
}
@Test
void testCase1(){
List<Integer> result = graphToBeTested().traverse();
List<Integer> expected = Arrays.asList(0,1,4,2,3);
assertEquals(expected, result);
}
Mureinik :
Assuming your tests don't need to modify the graph, you can save it in a static member and set it up in a @BeforeAll
method:
private static DepthFirstTraversal graph;
@BeforeAll
public static void setUpGraph() {
graph = new DepthFirstTraversal();
graph.add(1); //add node
graph.add(0);
graph.add(2);
graph.add(3);
graph.add(4);
graph.add(0,1); //connect nodes with edges
graph.add(1,2);
graph.add(2,3);
graph.add(0,4);
}
@Test
void testCase1(){
List<Integer> result = graph.traverse();
List<Integer> expected = Arrays.asList(0,1,4,2,3);
assertEquals(expected, result);
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=357888&siteId=1