0

我有类 CandidateService 标记为 @Transactional

@Transactional
@Service("candidateService")
public class CandidateService {

    @Autowired
    private CandidateDao candidateDao;

    ....

    public void add(Candidate candidate) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String login = auth.getName();
        User user =  utilService.getOrSaveUser(login);
        candidate.setAuthor(user);
        candidateDao.add(candidate);
    }
   ...
}

道实现:

@Override
    public Integer add(Candidate candidate) throws HibernateException{
        Session session = sessionFactory.getCurrentSession();
        if (candidate == null) {
            return null;
        }
        Integer id = (Integer) session.save(candidate);
        return id;

    }

如果我在@controller 类中编写:

@RequestMapping("/submitFormAdd")
    public ModelAndView submitFormAdd(
            Model model,
            @ModelAttribute("myCandidate") @Valid Candidate myCandidate,
            BindingResult result,
            RedirectAttributes redirectAttributes) {
        if (result.hasErrors()) {
            return new ModelAndView("candidateDetailsAdd");
        }

        myCandidate.setDate(new Date());
        candidateService.add(myCandidate);

    .....
    }

执行此方法后将数据放入数据库!

如果我写测试:

@ContextConfiguration(locations = {"classpath:/test/BeanConfig.xml"})
public class CandidateServiceTest extends AbstractTransactionalJUnit4SpringContextTests{

    @Autowired
    CandidateService candidateService;

    @BeforeClass
    public static void initialize() throws Exception{

        UtilMethods.createTestDb();

    }
    @Before
    public void setup() {
        TestingAuthenticationToken testToken = new TestingAuthenticationToken("testUser", "");
        SecurityContextHolder.getContext().setAuthentication(testToken);
    }

    @After
    public void cleanUp() {
        SecurityContextHolder.clearContext();
    }
    @Test
    public void add(){
        Candidate candidate = new Candidate();
        candidate.setName("testUser");
        candidate.setPhone("88888");
        candidateService.add(candidate);
        List<Candidate> candidates = candidateService.findByName(candidate.getName());
        Assert.assertNotNull(candidates);
        Assert.assertEquals("88888", candidates.get(0).getPhone());
    }
}

测试是绿色的,但执行后我没有在我的数据库中看到数据。

你能解释一下为什么以及如何解决它吗?

更新

configuration:
<tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Менеджер транзакций -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
4

3 回答 3

1
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)

以上你的测试类不会影响到数据库。

设置或添加defaultRollback = false以查看表中保留的数据。

于 2013-09-24T09:47:32.337 回答
0

测试方法完成后,更改将回滚。测试方法数据库更改被还原,您可以使用新数据进行其他测试。您无需担心不同测试用例中具有相同 id 的模型对象。

如果您需要公共数据,您可以使用 setup() 方法进行更改,这些更改也不会保存在数据库中。

在执行测试方法之前,将调用 setup() 方法。如果您有 3 个测试方法,则将调用 3 次 setup() 方法。

对不起,我的英语不好.......

于 2013-09-24T09:42:28.690 回答
0

我认为您应该使用参数 defaultRollback = false 添加 TransactionConfiguration 注释

于 2013-09-24T09:37:49.423 回答