Spring Junit Mockito

14. Mai 2019

Interessantes Setup für die kombination aus Spring, Junit und Mockito:

https://stackoverflow.com/q/13525106

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:springTestContext.xml", "classpath:springTestContext-dao.xml"})
@TransactionConfiguration(transactionManager = "txManager")
public class MyManagerTest extends AbstractTransactionalJUnit4SpringContextTests {
    @Mock
    private ProductDao productDao;

    @InjectMocks
    MyManager myManager = new MyManagerImpl();

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testUnParsableXml() {
        String xml = "adlsfas";
        Response response = myManager.processXMLContent(xml);
        assertFalse(response.isSuccess());
        System.out.println(response.getResponse());
    }

}
@Service("myManager")
public class MyManagerImpl extends BaseManager implements MyManager {
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public Response processXMLContent(String xml) {
       /* NB. Extremly simplified version.... */
       Response response = new Response();
       try {
            parseXml(); // just dummy sample. Its actually parsing xml
            response.setSuccess(true)
       catch(SAXException e) {
           TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
           response.setSuccess(false);
       }
       return response;
    }
}

No Comments

Comments are closed.