Java tutorial
/* * #%L * unidle * %% * Copyright (C) 2013 Martin Lau * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ package org.unidle.service; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextHierarchy; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import org.unidle.config.CacheConfiguration; import org.unidle.config.DataConfiguration; import org.unidle.config.I18NConfiguration; import org.unidle.config.ServiceConfiguration; import org.unidle.domain.Attachment; import org.unidle.repository.AttachmentRepository; import static org.fest.assertions.Assertions.assertThat; @ContextHierarchy({ @ContextConfiguration(classes = CacheConfiguration.class), @ContextConfiguration(classes = DataConfiguration.class), @ContextConfiguration(classes = I18NConfiguration.class), @ContextConfiguration(classes = ServiceConfiguration.class) }) @RunWith(SpringJUnit4ClassRunner.class) @Transactional public class AttachmentServiceImplTest { @Autowired private AttachmentRepository attachmentRepository; @Autowired private AttachmentService subject; @Test public void testCreateAttachment() throws Exception { final Attachment result = subject.createAttachment( new MockMultipartFile("test.txt", "test.txt", "text/plain", "this is a text file".getBytes())); assertThat(attachmentRepository.count()).isEqualTo(1L); assertThat(result.getContent()).isEqualTo("this is a text file".getBytes()); assertThat(result.getContentType()).isEqualTo("text/plain"); assertThat(result.getTitle()).isEqualTo("test.txt"); } }