Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

42%
+1 −2
Q&A How to create an object, call one of it's methods and pass it as an argument as a oneliner?

Assume I have this class Foo class Foo { private int x; void setX(int x) { this.x = x; } } And I have a Junit test like this: Bar bar; @BeforeEach void setu...

3 answers  ·  posted 2y ago by klutt‭  ·  last activity 2y ago by Alexei‭

Question java junit
#4: Post edited by user avatar klutt‭ · 2022-02-23T22:53:47Z (over 2 years ago)
  • Assume I have this class Foo
  • class Foo {
  • private int x;
  • void setX(int x) {
  • this.x = x;
  • }
  • }
  • And I have a Junit test like this:
  • Bar bar;
  • @BeforeEach
  • void setup() {
  • bar = new Bar();
  • }
  • @Test
  • void test() {
  • Foo foo = new Foo();
  • foo.setX(42);
  • assertEquals(13, bar.fun(foo).getY(), "Test failed because blabla");
  • }
  • Now, I would like to make the test function to a oneliner without modifying either the class `Foo` or `Bar`. I have tried `assertTrue(bar.fun(new Foo().setX(42)))` but then I would need to change the return type of `Foo::setX`.
  • The closest working solution I have so far is to write a wrapper like this:
  • void wrapper(int expected, int x, String msg) {
  • Foo foo = new Foo();
  • foo.setX(x);
  • assertEquals(expected, bar.fun(foo).getY(), msg);
  • }
  • But I want to avoid it if possible. Mainly because it's likely that if I go that route, I would have to write many wrappers that would clutter the code. And the reason I want oneliners is to get a better overview of all testcases without the need for scrolling.
  • ### EDIT
  • The comment section made me want to clarify a few things. I completely understand that this is not the best thing to do in most situations. My question is for those cases where it actually makes sense.
  • I do changes to code to make it testable, but that's mostly restricted to making fields an methods protected instead of private, plus extracting methods.
  • I think InfiniteDissent‭ had a genius answer. Simple but straight to the point. meriton‭ pointed out that code duplication should be avoided, and while I do agree in general, there are always exceptions to every rule. Look at this example with some slightly modified code from InfiniteDissent's answer. I know that one shouldn't answer in the question, but this is the best way to explain why I wanted this.
  • ```
  • @Test
  • void test() {
  • Foo a;
  • // X & Y - When enemy comes from north and has low health
  • a = new Foo(); a.setX(42); assertEquals(13, bar.fun(foo).getY());
  • a = new Foo(); a.setX(43); assertEquals(10, bar.fun(foo).getY());
  • a = new Foo(); a.setX(44); assertEquals(16, bar.fun(foo).getY());
  • // X & Z - enemy comes from north, but has high health
  • a = new Foo(); a.setX(47); assertEquals(133, bar.fun(foo).getZ());
  • a = new Foo(); a.setX(3); assertEquals(1, bar.fun(foo).getZ());
  • a = new Foo(); a.setX(2); assertEquals(16, bar.fun(foo).getZ());
  • // A & Y - Enemy comes from south and low health
  • a = new Foo(); a.setA(17); assertEquals(133, bar.fun(foo).getY());
  • a = new Foo(); a.setA(113); assertEquals(122, bar.fun(foo).getY());
  • a = new Foo(); a.setA(2); assertEquals(16, bar.fun(foo).getY());
  • // A & Z - Enemy comes from south and high health
  • a = new Foo(); a.setA(11); assertEquals(33, bar.fun(foo).getZ());
  • a = new Foo(); a.setA(133); assertEquals(126, bar.fun(foo).getZ());
  • a = new Foo(); a.setA(23); assertEquals(16, bar.fun(foo).getZ());
  • }
  • ```
  • Sure, it's unorthodox, and it's A LOT of code duplication, but given the context, it's not really a problem. The above code is VERY clear, VERY well structured, and ALL of it easily fits on one screen, and there are NO abstractions whatsoever to keep track of. Anomalies from the pattern is easily spotted with a pure glance, given proper formatting. Adding new tests are very easily done by just copying a row and change a few characters.
  • And yes, this is not always a good idea, but sometimes it is. This question was for those cases where it does make sense.
  • Assume I have this class Foo
  • class Foo {
  • private int x;
  • void setX(int x) {
  • this.x = x;
  • }
  • }
  • And I have a Junit test like this:
  • Bar bar;
  • @BeforeEach
  • void setup() {
  • bar = new Bar();
  • }
  • @Test
  • void test() {
  • Foo foo = new Foo();
  • foo.setX(42);
  • assertEquals(13, bar.fun(foo).getY(), "Test failed because blabla");
  • }
  • Now, I would like to make the test function to a oneliner without modifying either the class `Foo` or `Bar`. I have tried `assertTrue(bar.fun(new Foo().setX(42)))` but then I would need to change the return type of `Foo::setX`.
  • The closest working solution I have so far is to write a wrapper like this:
  • void wrapper(int expected, int x, String msg) {
  • Foo foo = new Foo();
  • foo.setX(x);
  • assertEquals(expected, bar.fun(foo).getY(), msg);
  • }
  • But I want to avoid it if possible. Mainly because it's likely that if I go that route, I would have to write many wrappers that would clutter the code. And the reason I want oneliners is to get a better overview of all testcases without the need for scrolling.
  • ### EDIT
  • The comment section made me want to clarify a few things. I completely understand that this is not the best thing to do in most situations. My question is for those cases where it actually makes sense.
  • I do changes to code to make it testable, but that's mostly restricted to making fields an methods protected instead of private, plus extracting methods.
  • I think InfiniteDissent‭ had a genius answer. Simple but straight to the point. meriton‭ pointed out that code duplication should be avoided, and while I do agree in general, there are always exceptions to every rule. Look at this example with some slightly modified code from InfiniteDissent's answer. I know that one shouldn't answer in the question, but this is the best way to explain why I wanted this.
  • ```
  • @Test
  • void test() {
  • Foo a;
  • // X & Y - When enemy comes from north and has low health
  • a = new Foo(); a.setX(42); assertEquals(13, bar.fun(foo).getY());
  • a = new Foo(); a.setX(43); assertEquals(10, bar.fun(foo).getY());
  • a = new Foo(); a.setX(44); assertEquals(16, bar.fun(foo).getY());
  • // X & Z - enemy comes from north, but has high health
  • a = new Foo(); a.setX(47); assertEquals(133, bar.fun(foo).getZ());
  • a = new Foo(); a.setX(3); assertEquals(1, bar.fun(foo).getZ());
  • a = new Foo(); a.setX(2); assertEquals(16, bar.fun(foo).getZ());
  • // A & Y - Enemy comes from south and low health
  • a = new Foo(); a.setA(17); assertEquals(133, bar.fun(foo).getY());
  • a = new Foo(); a.setA(113); assertEquals(122, bar.fun(foo).getY());
  • a = new Foo(); a.setA(2); assertEquals(16, bar.fun(foo).getY());
  • // A & Z - Enemy comes from south and high health
  • a = new Foo(); a.setA(11); assertEquals(33, bar.fun(foo).getZ());
  • a = new Foo(); a.setA(133); assertEquals(126, bar.fun(foo).getZ());
  • a = new Foo(); a.setA(23); assertEquals(16, bar.fun(foo).getZ());
  • }
  • ```
  • Sure, it's unorthodox, and it's A LOT of code duplication, but given the context, it's not really a problem. The above code is VERY clear, VERY well structured, and ALL of it easily fits on one screen, and there are NO abstractions whatsoever to keep track of. Anomalies from the pattern is easily spotted with a pure glance, given proper formatting. Adding new tests are very easily done by just copying a row and change a few characters. It's not perfect. It's not foolproof. But it does have its benefits.
  • And yes, this is not always a good idea, but sometimes it is. This question was for those cases where it does make sense.
#3: Post edited by user avatar klutt‭ · 2022-02-23T22:20:00Z (over 2 years ago)
  • Assume I have this class Foo
  • class Foo {
  • private int x;
  • void setX(int x) {
  • this.x = x;
  • }
  • }
  • And I have a Junit test like this:
  • Bar bar;
  • @BeforeEach
  • void setup() {
  • bar = new Bar();
  • }
  • @Test
  • void test() {
  • Foo foo = new Foo();
  • foo.setX(42);
  • assertEquals(13, bar.fun(foo).getY(), "Test failed because blabla");
  • }
  • Now, I would like to make the test function to a oneliner without modifying either the class `Foo` or `Bar`. I have tried `assertTrue(bar.fun(new Foo().setX(42)))` but then I would need to change the return type of `Foo::setX`.
  • The closest working solution I have so far is to write a wrapper like this:
  • void wrapper(int expected, int x, String msg) {
  • Foo foo = new Foo();
  • foo.setX(x);
  • assertEquals(expected, bar.fun(foo).getY(), msg);
  • }
  • But I want to avoid it if possible. Mainly because it's likely that if I go that route, I would have to write many wrappers that would clutter the code. And the reason I want oneliners is to get a better overview of all testcases without the need for scrolling.
  • Assume I have this class Foo
  • class Foo {
  • private int x;
  • void setX(int x) {
  • this.x = x;
  • }
  • }
  • And I have a Junit test like this:
  • Bar bar;
  • @BeforeEach
  • void setup() {
  • bar = new Bar();
  • }
  • @Test
  • void test() {
  • Foo foo = new Foo();
  • foo.setX(42);
  • assertEquals(13, bar.fun(foo).getY(), "Test failed because blabla");
  • }
  • Now, I would like to make the test function to a oneliner without modifying either the class `Foo` or `Bar`. I have tried `assertTrue(bar.fun(new Foo().setX(42)))` but then I would need to change the return type of `Foo::setX`.
  • The closest working solution I have so far is to write a wrapper like this:
  • void wrapper(int expected, int x, String msg) {
  • Foo foo = new Foo();
  • foo.setX(x);
  • assertEquals(expected, bar.fun(foo).getY(), msg);
  • }
  • But I want to avoid it if possible. Mainly because it's likely that if I go that route, I would have to write many wrappers that would clutter the code. And the reason I want oneliners is to get a better overview of all testcases without the need for scrolling.
  • ### EDIT
  • The comment section made me want to clarify a few things. I completely understand that this is not the best thing to do in most situations. My question is for those cases where it actually makes sense.
  • I do changes to code to make it testable, but that's mostly restricted to making fields an methods protected instead of private, plus extracting methods.
  • I think InfiniteDissent‭ had a genius answer. Simple but straight to the point. meriton‭ pointed out that code duplication should be avoided, and while I do agree in general, there are always exceptions to every rule. Look at this example with some slightly modified code from InfiniteDissent's answer. I know that one shouldn't answer in the question, but this is the best way to explain why I wanted this.
  • ```
  • @Test
  • void test() {
  • Foo a;
  • // X & Y - When enemy comes from north and has low health
  • a = new Foo(); a.setX(42); assertEquals(13, bar.fun(foo).getY());
  • a = new Foo(); a.setX(43); assertEquals(10, bar.fun(foo).getY());
  • a = new Foo(); a.setX(44); assertEquals(16, bar.fun(foo).getY());
  • // X & Z - enemy comes from north, but has high health
  • a = new Foo(); a.setX(47); assertEquals(133, bar.fun(foo).getZ());
  • a = new Foo(); a.setX(3); assertEquals(1, bar.fun(foo).getZ());
  • a = new Foo(); a.setX(2); assertEquals(16, bar.fun(foo).getZ());
  • // A & Y - Enemy comes from south and low health
  • a = new Foo(); a.setA(17); assertEquals(133, bar.fun(foo).getY());
  • a = new Foo(); a.setA(113); assertEquals(122, bar.fun(foo).getY());
  • a = new Foo(); a.setA(2); assertEquals(16, bar.fun(foo).getY());
  • // A & Z - Enemy comes from south and high health
  • a = new Foo(); a.setA(11); assertEquals(33, bar.fun(foo).getZ());
  • a = new Foo(); a.setA(133); assertEquals(126, bar.fun(foo).getZ());
  • a = new Foo(); a.setA(23); assertEquals(16, bar.fun(foo).getZ());
  • }
  • ```
  • Sure, it's unorthodox, and it's A LOT of code duplication, but given the context, it's not really a problem. The above code is VERY clear, VERY well structured, and ALL of it easily fits on one screen, and there are NO abstractions whatsoever to keep track of. Anomalies from the pattern is easily spotted with a pure glance, given proper formatting. Adding new tests are very easily done by just copying a row and change a few characters.
  • And yes, this is not always a good idea, but sometimes it is. This question was for those cases where it does make sense.
#2: Post edited by user avatar klutt‭ · 2022-02-23T12:57:50Z (over 2 years ago)
  • Assume I have this class Foo
  • class Foo {
  • private int x;
  • void setX(int x) {
  • this.x = x;
  • }
  • }
  • And I have a Junit test like this:
  • Bar bar;
  • @BeforeEach
  • void setup() {
  • bar = new Bar();
  • }
  • @Test
  • void test() {
  • Foo foo = new Foo();
  • foo.setX(42);
  • assertEquals(13, bar.fun(foo), "Test failed because blabla");
  • }
  • Now, I would like to make the test function to a oneliner without modifying either the class `Foo` or `Bar`. I have tried `assertTrue(bar.fun(new Foo().setX(42)))` but then I would need to change the return type of `Foo::setX`.
  • The closest working solution I have so far is to write a wrapper like this:
  • void wrapper(int expected, int x, String msg) {
  • Foo foo = new Foo();
  • foo.setX(x);
  • assertEquals(expected, bar.fun(foo), msg);
  • }
  • But I want to avoid it if possible. Mainly because it's likely that if I go that route, I would have to write many wrappers that would clutter the code. And the reason I want oneliners is to get a better overview of all testcases without the need for scrolling.
  • Assume I have this class Foo
  • class Foo {
  • private int x;
  • void setX(int x) {
  • this.x = x;
  • }
  • }
  • And I have a Junit test like this:
  • Bar bar;
  • @BeforeEach
  • void setup() {
  • bar = new Bar();
  • }
  • @Test
  • void test() {
  • Foo foo = new Foo();
  • foo.setX(42);
  • assertEquals(13, bar.fun(foo).getY(), "Test failed because blabla");
  • }
  • Now, I would like to make the test function to a oneliner without modifying either the class `Foo` or `Bar`. I have tried `assertTrue(bar.fun(new Foo().setX(42)))` but then I would need to change the return type of `Foo::setX`.
  • The closest working solution I have so far is to write a wrapper like this:
  • void wrapper(int expected, int x, String msg) {
  • Foo foo = new Foo();
  • foo.setX(x);
  • assertEquals(expected, bar.fun(foo).getY(), msg);
  • }
  • But I want to avoid it if possible. Mainly because it's likely that if I go that route, I would have to write many wrappers that would clutter the code. And the reason I want oneliners is to get a better overview of all testcases without the need for scrolling.
#1: Initial revision by user avatar klutt‭ · 2022-02-23T12:51:06Z (over 2 years ago)
How to create an object, call one of it's methods and pass it as an argument as a oneliner?
Assume I have this class Foo

    class Foo {
        private int x;
        
        void setX(int x) {
            this.x = x;
        }
    }

And I have a Junit test like this:

    Bar bar;

    @BeforeEach 
    void setup() {
        bar = new Bar();
    }

    @Test
    void test() {
        Foo foo = new Foo();
        foo.setX(42);
        assertEquals(13, bar.fun(foo), "Test failed because blabla");
    }

Now, I would like to make the test function to a oneliner without modifying either the class `Foo` or `Bar`. I have tried `assertTrue(bar.fun(new Foo().setX(42)))` but then I would need to change the return type of `Foo::setX`.

The closest working solution I have so far is to write a wrapper like this:

    void wrapper(int expected, int x, String msg) {
        Foo foo = new Foo();
        foo.setX(x);
        assertEquals(expected, bar.fun(foo), msg);
    }

But I want to avoid it if possible. Mainly because it's likely that if I go that route, I would have to write many wrappers that would clutter the code. And the reason I want oneliners is to get a better overview of all testcases without the need for scrolling.