2015-11-30 13 views
9

Bu kod, Spring 3.1 ve junit4 ve spring-test 3.1 kullanır. Bu kodu junit3.8.x kullanarak ve yüklemek istiyorum. Bu eski bir yapı sistemi kaynaklanmaktadır. Bunu nasıl yapabilirim? Bahar için çevrimiçi belgelerin çoğu, aşağıdaki yaklaşımın etrafındadır. “Bahar derslerini yükleyebilmem lazım”. Bu durumda bir XML dosyanız var, rest-servlet.xml ve 'services' sınıfları açıklamalı. Her bir testten önce bu rest-servlet yayı konfigürasyon dosyasını ve ayar yayını yükleyebilmek istiyorum.SpringJUnit4ClassRunner ile bu 'spring 3.1' odaklı junit4 testini yaylı junit3.8 tabanlı bir test haline nasıl dönüştürebilirim?

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd 
          http://www.springframework.org/schema/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc.xsd">   
     <context:component-scan base-package="com.ca.services.rest.*,com.ca.services.test.*" /> 
     <mvc:annotation-driven /> 
    </beans> 

TestActivityLog Bu SpringJUnitRunner taklit ederek elde edilebilir

import org.junit.Assert; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.context.ApplicationContext; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

import com.ca.services.rest.activity.services.ActivityDaoRepository; 
import com.ca.services.rest.activity.services.ActivityService; 
import com.ca.services.rest.activity.services.impl.ActivityServiceImpl; 
import com.ca.services.test.mock.MockActivityDaoRepository; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"file:**/WEB-INF/rest-servlet.xml"}) 
public class TestActivityLog { 

    @Autowired 
    @Qualifier("mockActivityDaoRepository") 
    private MockActivityDaoRepository repository; 

    @Autowired 
    private ApplicationContext applicationContext; 

    @Autowired 
    public TestActivityLog() { 
     super(); 
    } 

    @Before 
    public void setup() throws Exception {  
    } 

    @Test 
    public void testOne() {  
     Assert.assertEquals("abc", "abc"); 
    }  

    public void testService2() { 
     final ActivityDaoRepository repo = repository; 
     final String chk1 = "[POL.ActivityAPI:as1.0.0]"; 
     final String chk2 = String.valueOf(repo.getVersion()); 
     Assert.assertEquals(chk1, chk2); 
    } 


    public void testService3() { 
     final ActivityService service = new ActivityServiceImpl(repository);   
    } 

} 

cevap

6

. Bu sınıf, uygulama içeriğini sağlanan yapılandırma konumlarından (sınıf yolunda) yükler ve test durumunda alanları otomatik olarak yükler.

Test etmek istediğimiz bir kontrolör fasulyesi (beans.xml dosyasında tanımlanmış) olduğunu varsayalım.

public class Controller { 

    public String message() { 
     return "Hello"; 
    } 

} 

Test Case:

public class Spring38TestCase extends TestCase { 

    private Controller controller; 
    private ApplicationContext context; 

    @Override 
    protected void setUp() throws Exception { 
     //Initializing spring application context. 
     context = new ClassPathXmlApplicationContext("beans.xml"); 
     //Setting fields in test case explicitly in case of auto wiring 
     controller = context.getBean(Controller.class); 
    } 

    public void testController() { 
     assertEquals("Hello", controller.message()); 
    } 
} 
0

Bahar ile JUnit 3.8 kullanmak için, test durumları yazmak için aşağıdaki şablonu kullanabilirsiniz. bahar içinde bazı 3,8 sınıf vardı düşünüyordum,

public class MyServiceTestCase 
     extends AbstractDependencyInjectionSpringContextTests { 

    private myService MyService; 

    @Test 
    public void testAddService() { 
     // a test case 
    } 

    /** 
    * The spring context configuration 
    */ 
    @Override 
    protected String[] getConfigLocations() { 
     return new String[] { "rest-servlet.xml" }; 
    } 
} 
İlgili konular