lundi 23 mars 2015

Instantiating objects when using Spring, for testing vs production

Am correct in understanding that when using Spring, you should use the Spring configuration xml to instantiate your objects for production, and directly instantiate objects when testing?


Eg.


MyMain.java



package org.world.hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyMain {

private Room room;


public static void speak(String str)
{
System.out.println(str);
}

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Room room = (Room) context.getBean("myRoom");

speak(room.generatePoem());


}

}


Room.java



package org.world.hello;

public class Room {

private BottleCounter bottleCounter;
private int numBottles;

public String generatePoem()
{
String str = "";
for (int i = numBottles; i>=0; i--)
{
str = str + bottleCounter.countBottle(i) + "\n";

}
return str;
}

public BottleCounter getBottleCounter() {
return bottleCounter;
}

public void setBottleCounter(BottleCounter bottleCounter) {
this.bottleCounter = bottleCounter;
}

public int getNumBottles() {
return numBottles;
}

public void setNumBottles(int numBottles) {
this.numBottles = numBottles;
}

}


BottleCounter.java



package org.world.hello;

public class BottleCounter {

public String countBottle(int i)
{
return i + " bottles of beer on the wall" + i + " bottles of beer!";
}

}


Beans.xml:



<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/QEDs1e">

<bean id="myRoom" class="org.world.hello.Room">
<property name="bottleCounter">
<bean id = "myBottleCounter" class = "org.world.hello.BottleCounter"/>
</property>
<property name = "numBottles" value = "10"></property>

</bean>

</beans>


Outputs: (my apologies for the missing space)



10 bottles of beer on the wall10 bottles of beer!
9 bottles of beer on the wall9 bottles of beer!
8 bottles of beer on the wall8 bottles of beer!
7 bottles of beer on the wall7 bottles of beer!
6 bottles of beer on the wall6 bottles of beer!
5 bottles of beer on the wall5 bottles of beer!
4 bottles of beer on the wall4 bottles of beer!
3 bottles of beer on the wall3 bottles of beer!
2 bottles of beer on the wall2 bottles of beer!
1 bottles of beer on the wall1 bottles of beer!
0 bottles of beer on the wall0 bottles of beer!


Now for testing this:


BottleCounterTest.java:



package org.world.hello;

import static org.junit.Assert.*;

import org.junit.Test;

public class BottleCounterTest {

@Test
public void testOneBottle() {
BottleCounter b = new BottleCounter();
assertEquals("1 bottles of beer on the wall1 bottles of beer!", b.countBottle(1));
}

}


Pretty straight forward.


RoomTest.java:



package org.world.hello;

import static org.junit.Assert.*;
import org.mockito.Mockito;
import org.junit.Test;

public class RoomTest {

@Test
public void testThreeBottlesAreSeperatedByNewLines()
{
Room r = new Room();
BottleCounter b = Mockito.mock(BottleCounter.class);
Mockito.when(b.countBottle(Mockito.anyInt())).thenReturn("a");
r.setBottleCounter(b);
r.setNumBottles(3);
assertEquals("a\na\na\na\n", r.generatePoem());
}

}


Am I correct in instantiating my test objects this way?


Aucun commentaire:

Enregistrer un commentaire