Announcement Announcement Module
Collapse
No announcement yet.
JAVA + JUnit + Spring = HEEEELP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JAVA + JUnit + Spring = HEEEELP

    Hi.. can someone help me?? i use Junit to run my test, something like this:

    Code:
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.lucene.queryParser.ParseException;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.TestExecutionListeners;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
    
    import br.com.dnasolution.integrabrasil.entity.TbCartorio;
    import br.com.dnasolution.integrabrasil.entity.TbMensagem;
    import br.com.dnasolution.integrabrasil.entity.TbMensagemTipo;
    import br.com.dnasolution.integrabrasil.entity.TbServico;
    import br.com.dnasolution.integrabrasil.entity.TbUsuario;
    import br.com.dnasolution.integrabrasil.exception.EntityValidationException;
    import br.com.dnasolution.integrabrasil.facade.MensagemAmfFacade;
    import br.com.dnasolution.integrabrasil.service.CartorioService;
    import br.com.dnasolution.integrabrasil.service.MensagemTipoService;
    import br.com.dnasolution.integrabrasil.service.ServicoService;
    import br.com.dnasolution.integrabrasil.service.UsuarioService;
    import br.com.dnasolution.integrabrasil.vo.MensagemItemRespostaVO;
    import br.com.dnasolution.integrabrasil.vo.MensagemItemVO;
    import br.com.dnasolution.integrabrasil.vo.MensagemVO;
    import br.com.dnasolution.integrabrasil.vo.RetornoMensagemRespostaVO;
    import br.com.dnasolution.integrabrasil.vo.RetornoMensagemResultadoVO;
    import br.com.dnasolution.integrabrasil.vo.StatusMensagemVO;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
    @ContextConfiguration(locations = "classpath:applicationContext.xml")
    @SuppressWarnings("unused")
    public class MensagemAmfFacadeTest {
    
    	@Autowired
    	private MensagemAmfFacade mensagemAmfFacade;
    
    @Test
    public void test(){
      //test
    }
    
    }

    so i need to use @BeforeClass, but when i try to run my class, he run the @BeforeClass before start to load my applicationContext.. have any way to run @BeforeClass after load my applicationContext? because i need to run that method only one time (and need to be the first method to run)

    tks

  • #2
    @BeforeClass methos are static so I supose that you only want to get a reference to the ApplicationContext. Still there isn't Test instance to configure

    You could declare a static field on Test class and set it with ApplicationContext in a TestExecutionListener. For example:

    Code:
    public class ExposeApplicationContextTestExecutionListener  extends AbstractTestExecutionListener {
    
    	@Override
    	public void beforeTestClass(TestContext testContext) throws Exception {
    		Field field = testContext.getTestClass().getDeclaredField("applicationContext");
    		ReflectionUtils.makeAccessible(field);
    		field.set(null, testContext.getApplicationContext());
    	}
    }
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners(listeners={ExposeApplicationContextTestExecutionListener.class})
    @ContextConfiguration(locations="classpath:applicationContext.xml")
    public class ExposeApplicationContextTest  {
    
    	private static ApplicationContext applicationContext;
    	
    	@BeforeClass
    	public static void before() {
    		Assert.assertNotNull(applicationContext);
    	}
    	
    	@Test
    	public void doNothing() {
    		
    	}
    }

    Comment

    Working...
    X