Spring Data Repository doesn't delete item on tests












0















we're trying to write tests for our spring boot application, and had encountered a very specific and weird problem. We need to test our repositories and using spring's @DataJpaTest annotation to quickly build-up an h2 database on memory, and get the related beans. But, on one of our tests, when we try to delete an entity with spring repository, it doesn't delete it. When we run the application normally and try to delete it with a REST request to our controllers, it works like a charm, but in tests, it doesn't.



When we remove the @DataJpaTest annotation and replace it with couple of other annotations, it works. This is not working set-up



@RunWith(SpringRunner.class)
@DataJpaTest
public class NodeInfoServiceIT {
//Some tests here
@Test
public void deleteNode_deleteNode_returnSuccessMessage() {
//We create our Service annotated class in here by hand, and call delete
//node function and check whether it is deleted or not, but it isn't
}


deleteNode does some edge-case checking and just calls delete on our spring repository. We managed to do this by changing the annotations a bit, but it is wonky, and not optimal here is working annotations:



@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
@TestPropertySource(
locations = "classpath:integrationtest-application.properties")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)


and the said application.properties is a pretty simple one:



spring.datasource.url = jdbc:h2:mem:test
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect


If we put @Transactional on top of our test class, it works like in fir annotated state.
If we don't put @DirtiesContext annotation, it doesn't work since we put the same data for all of our tests, and when it tries to inject the data, it fails with constraint not satisfactory exception.
My question is what is the problem with first annotation method and how can I fix it.



Thank you.










share|improve this question

























  • Why don't you post the actual code for the different variants instead of comments and prose? The way it is written right now the question is rather confusing.

    – Jens Schauder
    Nov 15 '18 at 14:26
















0















we're trying to write tests for our spring boot application, and had encountered a very specific and weird problem. We need to test our repositories and using spring's @DataJpaTest annotation to quickly build-up an h2 database on memory, and get the related beans. But, on one of our tests, when we try to delete an entity with spring repository, it doesn't delete it. When we run the application normally and try to delete it with a REST request to our controllers, it works like a charm, but in tests, it doesn't.



When we remove the @DataJpaTest annotation and replace it with couple of other annotations, it works. This is not working set-up



@RunWith(SpringRunner.class)
@DataJpaTest
public class NodeInfoServiceIT {
//Some tests here
@Test
public void deleteNode_deleteNode_returnSuccessMessage() {
//We create our Service annotated class in here by hand, and call delete
//node function and check whether it is deleted or not, but it isn't
}


deleteNode does some edge-case checking and just calls delete on our spring repository. We managed to do this by changing the annotations a bit, but it is wonky, and not optimal here is working annotations:



@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
@TestPropertySource(
locations = "classpath:integrationtest-application.properties")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)


and the said application.properties is a pretty simple one:



spring.datasource.url = jdbc:h2:mem:test
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect


If we put @Transactional on top of our test class, it works like in fir annotated state.
If we don't put @DirtiesContext annotation, it doesn't work since we put the same data for all of our tests, and when it tries to inject the data, it fails with constraint not satisfactory exception.
My question is what is the problem with first annotation method and how can I fix it.



Thank you.










share|improve this question

























  • Why don't you post the actual code for the different variants instead of comments and prose? The way it is written right now the question is rather confusing.

    – Jens Schauder
    Nov 15 '18 at 14:26














0












0








0








we're trying to write tests for our spring boot application, and had encountered a very specific and weird problem. We need to test our repositories and using spring's @DataJpaTest annotation to quickly build-up an h2 database on memory, and get the related beans. But, on one of our tests, when we try to delete an entity with spring repository, it doesn't delete it. When we run the application normally and try to delete it with a REST request to our controllers, it works like a charm, but in tests, it doesn't.



When we remove the @DataJpaTest annotation and replace it with couple of other annotations, it works. This is not working set-up



@RunWith(SpringRunner.class)
@DataJpaTest
public class NodeInfoServiceIT {
//Some tests here
@Test
public void deleteNode_deleteNode_returnSuccessMessage() {
//We create our Service annotated class in here by hand, and call delete
//node function and check whether it is deleted or not, but it isn't
}


deleteNode does some edge-case checking and just calls delete on our spring repository. We managed to do this by changing the annotations a bit, but it is wonky, and not optimal here is working annotations:



@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
@TestPropertySource(
locations = "classpath:integrationtest-application.properties")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)


and the said application.properties is a pretty simple one:



spring.datasource.url = jdbc:h2:mem:test
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect


If we put @Transactional on top of our test class, it works like in fir annotated state.
If we don't put @DirtiesContext annotation, it doesn't work since we put the same data for all of our tests, and when it tries to inject the data, it fails with constraint not satisfactory exception.
My question is what is the problem with first annotation method and how can I fix it.



Thank you.










share|improve this question
















we're trying to write tests for our spring boot application, and had encountered a very specific and weird problem. We need to test our repositories and using spring's @DataJpaTest annotation to quickly build-up an h2 database on memory, and get the related beans. But, on one of our tests, when we try to delete an entity with spring repository, it doesn't delete it. When we run the application normally and try to delete it with a REST request to our controllers, it works like a charm, but in tests, it doesn't.



When we remove the @DataJpaTest annotation and replace it with couple of other annotations, it works. This is not working set-up



@RunWith(SpringRunner.class)
@DataJpaTest
public class NodeInfoServiceIT {
//Some tests here
@Test
public void deleteNode_deleteNode_returnSuccessMessage() {
//We create our Service annotated class in here by hand, and call delete
//node function and check whether it is deleted or not, but it isn't
}


deleteNode does some edge-case checking and just calls delete on our spring repository. We managed to do this by changing the annotations a bit, but it is wonky, and not optimal here is working annotations:



@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
@TestPropertySource(
locations = "classpath:integrationtest-application.properties")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)


and the said application.properties is a pretty simple one:



spring.datasource.url = jdbc:h2:mem:test
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect


If we put @Transactional on top of our test class, it works like in fir annotated state.
If we don't put @DirtiesContext annotation, it doesn't work since we put the same data for all of our tests, and when it tries to inject the data, it fails with constraint not satisfactory exception.
My question is what is the problem with first annotation method and how can I fix it.



Thank you.







spring hibernate spring-boot spring-data-jpa h2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 5:36









Alien

5,27931127




5,27931127










asked Nov 15 '18 at 13:11









Fatih DoğmuşFatih Doğmuş

1




1













  • Why don't you post the actual code for the different variants instead of comments and prose? The way it is written right now the question is rather confusing.

    – Jens Schauder
    Nov 15 '18 at 14:26



















  • Why don't you post the actual code for the different variants instead of comments and prose? The way it is written right now the question is rather confusing.

    – Jens Schauder
    Nov 15 '18 at 14:26

















Why don't you post the actual code for the different variants instead of comments and prose? The way it is written right now the question is rather confusing.

– Jens Schauder
Nov 15 '18 at 14:26





Why don't you post the actual code for the different variants instead of comments and prose? The way it is written right now the question is rather confusing.

– Jens Schauder
Nov 15 '18 at 14:26












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320269%2fspring-data-repository-doesnt-delete-item-on-tests%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320269%2fspring-data-repository-doesnt-delete-item-on-tests%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma