L'instanciation d'objets lors de l'utilisation de Printemps, pour les essais vs production

Suis correct dans la compréhension que lors de l'utilisation de Printemps, vous devez utiliser le Printemps de configuration xml pour instancier des objets pour la production, et directement instancier des objets lors de l'essai?

Par exemple.

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://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

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

Sorties: (toutes mes excuses pour le manque d'espace)

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!

Maintenant pour tester ceci:

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));
    }

}

Assez simple.

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());
    }

}

Suis-je correct dans l'instanciation de mes objets de test de cette façon?

Ce n'est pas correct. Vous devez injecter des instances de l'objet même des fins de test. Voir @Waheed la réponse ci-dessous. Si vous voulez différent (ou différemment initialisé) les instances d'un objet d'essai et de production, vous devez utiliser Printemps des Profils.
Voici une question:stackoverflow.com/questions/29224579/...
Si vous êtes à la création d'objets à l'aide new, alors comment pouvez-vous gérer les dépendances, si votre Room objet a une dépendance sur Door objet, alors vous avez besoin Door ainsi de tester Room correctement sinon vous allez courir dans des NPE. Si vous essayez de créer toutes les dépendances à l'aide de new, alors vous avez besoin pour créer l'ensemble de la chaîne de dépendances par vous-même ce qui n'est pas un moyen idéal de le faire. Mais plutôt que de tracas, de l'utilisation Mockito est mock() pour obtenir une procuration de vos dépendances et puis faire usage de when-thenReturn

OriginalL'auteur dwjohnston | 2015-03-23